From 2f05708a650dcaeb6f3e95b6653849936b749d2c Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 16:18:31 +0200 Subject: [PATCH 1/4] Refactor question answering to use request.js --- .../retrospring/features/question/answer.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/app/javascript/retrospring/features/question/answer.ts b/app/javascript/retrospring/features/question/answer.ts index 5f643534..dafc5075 100644 --- a/app/javascript/retrospring/features/question/answer.ts +++ b/app/javascript/retrospring/features/question/answer.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import { showNotification, showErrorNotification } from 'utilities/notifications'; @@ -22,11 +22,13 @@ export function questionAnswerHandler(event: Event): void { inbox: String(false) }; - Rails.ajax({ - url: '/ajax/answer', - type: 'POST', - data: new URLSearchParams(data).toString(), - success: (data) => { + post('/ajax/answer', { + body: data, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (!data.success) { showErrorNotification(data.message); button.disabled = false; @@ -37,13 +39,12 @@ export function questionAnswerHandler(event: Event): void { showNotification(data.message); document.querySelector('div#answers').insertAdjacentHTML('afterbegin', data.render); document.querySelector('div#q-answer-box').remove(); - }, - error: (data, status, xhr) => { - console.log(data, status, xhr); + }) + .catch(err => { + console.log(err); button.disabled = false; document.querySelector('textarea#q-answer-text').readOnly = false; - } - }); + }); } export function questionAnswerInputHandler(event: KeyboardEvent): void { From 9c9f56e2319bcd030f46c0934adc850fea00cc65 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 16:19:00 +0200 Subject: [PATCH 2/4] 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 { From dba817c8b6611e0ad1f310d66ee03ae337071e25 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 16:19:32 +0200 Subject: [PATCH 3/4] 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 { From d116e338a357db117aeafda88f8cb2bf0c5fc4cf Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 16:20:07 +0200 Subject: [PATCH 4/4] 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 }