From 5ea28e1830a10c32b59918dbc69bbfc93e660acf Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 05:04:01 +0200 Subject: [PATCH] Refactor comment smiling to use request.js --- .../features/answerbox/comment/smile.ts | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app/javascript/retrospring/features/answerbox/comment/smile.ts b/app/javascript/retrospring/features/answerbox/comment/smile.ts index f78c8747..b80d5bd5 100644 --- a/app/javascript/retrospring/features/answerbox/comment/smile.ts +++ b/app/javascript/retrospring/features/answerbox/comment/smile.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import I18n from 'retrospring/i18n'; import { showNotification, showErrorNotification } from 'utilities/notifications'; @@ -22,25 +22,27 @@ export function commentSmileHandler(event: Event): void { button.disabled = true; - Rails.ajax({ - url: targetUrl, - type: 'POST', - data: new URLSearchParams({ + post(targetUrl, { + body: { id: id - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + success = data.success; if (success) { document.querySelector(`#ab-comment-smile-count-${id}`).innerHTML = String(count); } 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; if (success) { @@ -53,6 +55,5 @@ export function commentSmileHandler(event: Event): void { break; } } - } - }); + }); } \ No newline at end of file