From d116e338a357db117aeafda88f8cb2bf0c5fc4cf Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 16:20:07 +0200 Subject: [PATCH] Move destroying and deleting questions to question/ --- .../{questionbox => question}/destroy.ts | 29 ++++++++++--------- .../retrospring/features/question/index.ts | 4 +++ .../{questionbox => question}/report.ts | 2 +- .../retrospring/features/questionbox/index.ts | 4 --- 4 files changed, 20 insertions(+), 19 deletions(-) rename app/javascript/retrospring/features/{questionbox => question}/destroy.ts (78%) rename app/javascript/retrospring/features/{questionbox => question}/report.ts (77%) diff --git a/app/javascript/retrospring/features/questionbox/destroy.ts b/app/javascript/retrospring/features/question/destroy.ts similarity index 78% rename from app/javascript/retrospring/features/questionbox/destroy.ts rename to app/javascript/retrospring/features/question/destroy.ts index 2a839e81..6bec276f 100644 --- a/app/javascript/retrospring/features/questionbox/destroy.ts +++ b/app/javascript/retrospring/features/question/destroy.ts @@ -1,10 +1,10 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import { showErrorNotification, showNotification } from 'utilities/notifications'; import swal from 'sweetalert'; import I18n from 'retrospring/i18n'; -export function questionboxDestroyHandler(event: Event): void { +export function questionDestroyHandler(event: Event): void { event.preventDefault(); const button = event.target as HTMLButtonElement; const questionId = button.dataset.qId; @@ -23,14 +23,16 @@ export function questionboxDestroyHandler(event: Event): void { button.disabled = false; return; } - - Rails.ajax({ - url: '/ajax/destroy_question', - type: 'POST', - data: new URLSearchParams({ + + post('/ajax/destroy_question', { + body: { question: questionId - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { if (button.dataset.redirect !== undefined) { window.location.pathname = button.dataset.redirect; @@ -43,11 +45,10 @@ export function questionboxDestroyHandler(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')); - } - }); + }); }); } \ No newline at end of file diff --git a/app/javascript/retrospring/features/question/index.ts b/app/javascript/retrospring/features/question/index.ts index bb177794..9e88b2a6 100644 --- a/app/javascript/retrospring/features/question/index.ts +++ b/app/javascript/retrospring/features/question/index.ts @@ -1,8 +1,12 @@ import registerEvents from 'utilities/registerEvents'; import { questionAnswerHandler, questionAnswerInputHandler } from './answer'; +import { questionDestroyHandler } from './destroy'; +import { questionReportHandler } from './report'; export default (): void => { registerEvents([ + { type: 'click', target: '[data-action=ab-question-report]', handler: questionReportHandler, global: true }, + { type: 'click', target: '[data-action=ab-question-destroy]', handler: questionDestroyHandler, global: true }, { type: 'click', target: '#q-answer-btn', handler: questionAnswerHandler, global: true }, { type: 'keydown', target: '#q-answer-text', handler: questionAnswerInputHandler, global: true } ]); diff --git a/app/javascript/retrospring/features/questionbox/report.ts b/app/javascript/retrospring/features/question/report.ts similarity index 77% rename from app/javascript/retrospring/features/questionbox/report.ts rename to app/javascript/retrospring/features/question/report.ts index cef41617..487132b5 100644 --- a/app/javascript/retrospring/features/questionbox/report.ts +++ b/app/javascript/retrospring/features/question/report.ts @@ -1,6 +1,6 @@ import { reportDialog } from 'utilities/reportDialog'; -export function questionboxReportHandler(event: Event): void { +export function questionReportHandler(event: Event): void { event.preventDefault(); const button = event.target as HTMLButtonElement; const questionId = button.dataset.qId; diff --git a/app/javascript/retrospring/features/questionbox/index.ts b/app/javascript/retrospring/features/questionbox/index.ts index cdb6a500..86225f48 100644 --- a/app/javascript/retrospring/features/questionbox/index.ts +++ b/app/javascript/retrospring/features/questionbox/index.ts @@ -1,15 +1,11 @@ import registerEvents from 'utilities/registerEvents'; import { questionboxAllHandler, questionboxAllInputHandler } from './all'; -import { questionboxDestroyHandler } from './destroy'; -import { questionboxReportHandler } from './report'; import { questionboxPromoteHandler, questionboxUserHandler, questionboxUserInputHandler } from './user'; export default (): void => { registerEvents([ - { type: 'click', target: '[data-action=ab-question-report]', handler: questionboxReportHandler, global: true }, { type: 'click', target: '[name=qb-ask]', handler: questionboxUserHandler, global: true }, { type: 'click', target: '#new-question', handler: questionboxPromoteHandler, global: true }, - { type: 'click', target: '[data-action=ab-question-destroy]', handler: questionboxDestroyHandler, global: true }, { type: 'click', target: '[name=qb-all-ask]', handler: questionboxAllHandler, global: true }, { type: 'keydown', target: '[name=qb-question]', handler: questionboxUserInputHandler, global: true }, { type: 'keydown', target: '[name=qb-all-question]', handler: questionboxAllInputHandler, global: true }