From 9c9f56e2319bcd030f46c0934adc850fea00cc65 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 16:19:00 +0200 Subject: [PATCH] Refactor question asking to use request.js --- .../retrospring/features/questionbox/all.ts | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/app/javascript/retrospring/features/questionbox/all.ts b/app/javascript/retrospring/features/questionbox/all.ts index f99aec51..6b621165 100644 --- a/app/javascript/retrospring/features/questionbox/all.ts +++ b/app/javascript/retrospring/features/questionbox/all.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'; @@ -8,31 +8,32 @@ export function questionboxAllHandler(event: Event): void { button.disabled = true; document.querySelector('textarea[name=qb-all-question]').readOnly = true; - Rails.ajax({ - url: '/ajax/ask', - type: 'POST', - data: new URLSearchParams({ + post('/ajax/ask', { + body: { rcpt: 'followers', question: document.querySelector('textarea[name=qb-all-question]').value, - anonymousQuestion: 'false' - }).toString(), - success: (data) => { + anonymousQuestion: 'false' + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { document.querySelector('textarea[name=qb-all-question]').value = ''; window['$']('#modal-ask-followers').modal('hide'); } 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(() => { button.disabled = false; document.querySelector('textarea[name=qb-all-question]').readOnly = false; - } - }); + }); } export function questionboxAllInputHandler(event: KeyboardEvent): void {