From 2c5da2bc5c19d23b3b1ce9d95fb2b293a70a259d Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 05:03:04 +0200 Subject: [PATCH] Refactor comment destroy to use request.js --- .../features/answerbox/comment/destroy.ts | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/app/javascript/retrospring/features/answerbox/comment/destroy.ts b/app/javascript/retrospring/features/answerbox/comment/destroy.ts index d6147193..36c29842 100644 --- a/app/javascript/retrospring/features/answerbox/comment/destroy.ts +++ b/app/javascript/retrospring/features/answerbox/comment/destroy.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import swal from 'sweetalert'; import I18n from 'retrospring/i18n'; @@ -25,24 +25,23 @@ export function commentDestroyHandler(event: Event): void { return; } - Rails.ajax({ - url: '/ajax/destroy_comment', - type: 'POST', - data: new URLSearchParams({ + post('/ajax/destroy_comment', { + body: { comment: id - }).toString(), - success: (data) => { - if (!data.success) return false; + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; showNotification(data.message); document.querySelector(`[data-comment-id="${id}"]`).remove(); - }, - error: (data, status, xhr) => { - console.log(data, status, xhr); + }) + .catch(err => { + console.log(err); showErrorNotification(I18n.translate('frontend.error.message')); button.disabled = false; - } - }); + }); }); } \ No newline at end of file