From dba817c8b6611e0ad1f310d66ee03ae337071e25 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 16:19:32 +0200 Subject: [PATCH] Refactor question asking on profiles to use request.js --- .../retrospring/features/questionbox/user.ts | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/app/javascript/retrospring/features/questionbox/user.ts b/app/javascript/retrospring/features/questionbox/user.ts index 68d7170e..127f0295 100644 --- a/app/javascript/retrospring/features/questionbox/user.ts +++ b/app/javascript/retrospring/features/questionbox/user.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import { showErrorNotification, showNotification } from 'utilities/notifications'; import I18n from 'retrospring/i18n'; @@ -15,15 +15,17 @@ export function questionboxUserHandler(event: Event): void { document.querySelector('textarea[name=qb-question]').readOnly = true; button.disabled = true; - Rails.ajax({ - url: '/ajax/ask', - type: 'POST', - data: new URLSearchParams({ + post('/ajax/ask', { + body: { rcpt: document.querySelector('input[name=qb-to]').value, question: document.querySelector('textarea[name=qb-question]').value, - anonymousQuestion: String(anonymousQuestion) - }).toString(), - success: (data) => { + anonymousQuestion: String(anonymousQuestion) + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { document.querySelector('textarea[name=qb-question]').value = ''; @@ -37,16 +39,15 @@ export function questionboxUserHandler(event: Event): void { } showNotification(data.message, data.success); - }, - error: (data, status, xhr) => { - console.log(data, status, xhr); + }) + .catch(err => { + console.log(err); showErrorNotification(I18n.translate('frontend.error.message')); - }, - complete: () => { + }) + .finally(() => { document.querySelector('textarea[name=qb-question]').readOnly = false; button.disabled = false; - } - }); + }); } export function questionboxPromoteHandler(): void {