From f55efbb017f47c1577a6734a7202c281fbdb78be Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 06:56:47 +0200 Subject: [PATCH 1/5] Refactor inbox deleting to use request.js --- .../retrospring/features/inbox/delete.ts | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/app/javascript/retrospring/features/inbox/delete.ts b/app/javascript/retrospring/features/inbox/delete.ts index 4f698656..6595af0b 100644 --- a/app/javascript/retrospring/features/inbox/delete.ts +++ b/app/javascript/retrospring/features/inbox/delete.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'; @@ -42,22 +42,20 @@ export function deleteAllQuestionsHandler(event: Event): void { if (returnValue === false) { return; } - - Rails.ajax({ - url: '/ajax/delete_all_inbox', - type: 'POST', - dataType: 'json', - success: (data) => { + + post('/ajax/delete_all_inbox') + .then(async response => { + const data = await response.json; + if (!data.success) return false; updateDeleteButton(false); document.querySelector('#entries').innerHTML = 'Nothing to see here!'; - }, - error: (data, status, xhr) => { - console.log(data, status, xhr); + }) + .catch(err => { + console.log(err); showErrorNotification(I18n.translate('frontend.error.message')); - } - }); + }); }); } @@ -76,21 +74,19 @@ export function deleteAllAuthorQuestionsHandler(event: Event): void { closeOnConfirm: true }, (returnValue) => { if (returnValue === null) return false; - - Rails.ajax({ - url: `/ajax/delete_all_inbox/${location.pathname.split('/')[2]}`, - type: 'POST', - dataType: 'json', - success: (data) => { + + post(`/ajax/delete_all_inbox/${location.pathname.split('/')[2]}`) + .then(async response => { + const data = await response.json; + if (!data.success) return false; updateDeleteButton(false); document.querySelector('#entries').innerHTML = 'Nothing to see here!'; - }, - 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 From a893d11aa106f0dea7cb2d467e643eeb5b4b0d15 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 06:57:15 +0200 Subject: [PATCH 2/5] Refactor inbox answering to use request.js --- .../features/inbox/entry/answer.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/app/javascript/retrospring/features/inbox/entry/answer.ts b/app/javascript/retrospring/features/inbox/entry/answer.ts index 38105735..14b6b25a 100644 --- a/app/javascript/retrospring/features/inbox/entry/answer.ts +++ b/app/javascript/retrospring/features/inbox/entry/answer.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import { updateDeleteButton } from '../delete'; import { showNotification, showErrorNotification } from 'utilities/notifications'; @@ -22,11 +22,13 @@ export function answerEntryHandler(event: Event): void { inbox: 'true' }; - 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); element.disabled = false; @@ -35,12 +37,11 @@ export function answerEntryHandler(event: Event): void { updateDeleteButton(false); showNotification(data.message); (inboxEntry as HTMLElement).remove(); - }, - error: (data, status, xhr) => { - console.log(data, status, xhr); + }) + .catch(err => { + console.log(err); element.disabled = false; - } - }); + }); } export function answerEntryInputHandler(event: KeyboardEvent): void { From 969f7e80f8e2ef06d39b28c5ce9497329a407dff Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 06:57:32 +0200 Subject: [PATCH 3/5] Refactor anon blocking to use request.js --- .../features/inbox/entry/blockAnon.ts | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/app/javascript/retrospring/features/inbox/entry/blockAnon.ts b/app/javascript/retrospring/features/inbox/entry/blockAnon.ts index 185489a7..f0d184b7 100644 --- a/app/javascript/retrospring/features/inbox/entry/blockAnon.ts +++ b/app/javascript/retrospring/features/inbox/entry/blockAnon.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"; @@ -7,24 +7,25 @@ export function blockAnonEventHandler(event: Event): void { const element: HTMLAnchorElement = event.target as HTMLAnchorElement; const data = { - question: element.getAttribute('data-q-id'), + question: element.getAttribute('data-q-id'), }; - Rails.ajax({ - url: '/ajax/block_anon', - type: 'POST', - data: new URLSearchParams(data).toString(), - success: (data) => { - if (!data.success) return false; - const inboxEntry: Node = element.closest('.inbox-entry'); + post('/ajax/block_anon', { + body: data, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; - showNotification(data.message); + if (!data.success) return false; + const inboxEntry: Node = element.closest('.inbox-entry'); - (inboxEntry as HTMLElement).remove(); - }, - error: (data, status, xhr) => { - console.log(data, status, xhr); - showErrorNotification(I18n.translate('frontend.error.message')); - } - }); + showNotification(data.message); + + (inboxEntry as HTMLElement).remove(); + }) + .catch(err => { + console.log(err); + showErrorNotification(I18n.translate('frontend.error.message')); + }); } \ No newline at end of file From 90f33dd76cf795538c8cb1fa35aab21f2977efb5 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 06:57:52 +0200 Subject: [PATCH 4/5] Refactor inbox entry deleting to use request.js --- .../features/inbox/entry/delete.ts | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/javascript/retrospring/features/inbox/entry/delete.ts b/app/javascript/retrospring/features/inbox/entry/delete.ts index 39927050..a03b3914 100644 --- a/app/javascript/retrospring/features/inbox/entry/delete.ts +++ b/app/javascript/retrospring/features/inbox/entry/delete.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'; @@ -27,12 +27,14 @@ export function deleteEntryHandler(event: Event): void { element.disabled = false; return; } - - Rails.ajax({ - url: '/ajax/delete_inbox', - type: 'POST', - data: new URLSearchParams(data).toString(), - success: (data) => { + + post('/ajax/delete_inbox', { + body: data, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (!data.success) return false; const inboxEntry: Node = element.closest('.inbox-entry'); @@ -40,11 +42,10 @@ export function deleteEntryHandler(event: Event): void { showNotification(data.message); (inboxEntry as HTMLElement).remove(); - }, - 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 From 3b6ade483f8cdb2d39be0bbfd70e7844220b25c7 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 06:58:08 +0200 Subject: [PATCH 5/5] Refactor question generating to use request.js --- .../retrospring/features/inbox/generate.ts | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/app/javascript/retrospring/features/inbox/generate.ts b/app/javascript/retrospring/features/inbox/generate.ts index 5cdd5e5c..abbc4d83 100644 --- a/app/javascript/retrospring/features/inbox/generate.ts +++ b/app/javascript/retrospring/features/inbox/generate.ts @@ -1,23 +1,21 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import I18n from 'retrospring/i18n'; import { updateDeleteButton } from './delete'; import { showErrorNotification } from 'utilities/notifications'; export function generateQuestionHandler(): void { - Rails.ajax({ - url: '/ajax/generate_question', - type: 'POST', - dataType: 'json', - success: (data) => { + post('/ajax/generate_question') + .then(async response => { + const data = await response.json; + if (!data.success) return false; document.querySelector('#entries').insertAdjacentHTML('afterbegin', data.render); updateDeleteButton(); - }, - error: (data, status, xhr) => { - console.log(data, status, xhr); - showErrorNotification(I18n.translate('frontend.error.message')); - } - }); + }) + .catch(err => { + console.log(err); + showErrorNotification(I18n.translate('frontend.error.message')); + }); } \ No newline at end of file