From 2c5da2bc5c19d23b3b1ce9d95fb2b293a70a259d Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 05:03:04 +0200 Subject: [PATCH 1/7] 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 From 40637c5f6d51627e968f4f8a94a79d551a7541b2 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 05:03:25 +0200 Subject: [PATCH 2/7] Refactor comment creation to use request.js --- .../features/answerbox/comment/new.ts | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app/javascript/retrospring/features/answerbox/comment/new.ts b/app/javascript/retrospring/features/answerbox/comment/new.ts index 1dca489b..6f729165 100644 --- a/app/javascript/retrospring/features/answerbox/comment/new.ts +++ b/app/javascript/retrospring/features/answerbox/comment/new.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'; @@ -19,14 +19,16 @@ export function commentCreateHandler(event: KeyboardEvent): boolean { input.disabled = true; - Rails.ajax({ - url: '/ajax/create_comment', - type: 'POST', - data: new URLSearchParams({ + post('/ajax/create_comment', { + body: { answer: id, comment: input.value - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { document.querySelector(`#ab-comments-${id}`).innerHTML = data.render; document.querySelector(`#ab-comment-count-${id}`).innerHTML = data.count; @@ -39,14 +41,13 @@ export function commentCreateHandler(event: KeyboardEvent): boolean { } 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(() => { input.disabled = false; - } - }); + }); } } \ No newline at end of file From 5ea28e1830a10c32b59918dbc69bbfc93e660acf Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 05:04:01 +0200 Subject: [PATCH 3/7] 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 From 508c7e844b96c62e2994d9e9a9b64b5e8007b5df Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 05:05:06 +0200 Subject: [PATCH 4/7] Refactor answer destroy to use request.js --- .../retrospring/features/answerbox/destroy.ts | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/app/javascript/retrospring/features/answerbox/destroy.ts b/app/javascript/retrospring/features/answerbox/destroy.ts index 4baaa39d..0d244cf0 100644 --- a/app/javascript/retrospring/features/answerbox/destroy.ts +++ b/app/javascript/retrospring/features/answerbox/destroy.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import { showErrorNotification, showNotification } from 'utilities/notifications'; import swal from 'sweetalert'; @@ -23,24 +23,25 @@ export function answerboxDestroyHandler(event: Event): void { button.disabled = false; return; } - - Rails.ajax({ - url: '/ajax/destroy_answer', - type: 'POST', - data: new URLSearchParams({ + + post('/ajax/destroy_answer', { + body: { answer: answerId - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { document.querySelector(`.answerbox[data-id="${answerId}"]`).remove(); } 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 From 0aff344581f91ccb1ae21704901c8b63982f6d6c Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 05:05:21 +0200 Subject: [PATCH 5/7] Refactor answer smile to use request.js --- .../retrospring/features/answerbox/smile.ts | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app/javascript/retrospring/features/answerbox/smile.ts b/app/javascript/retrospring/features/answerbox/smile.ts index 9f49144f..fc154b0d 100644 --- a/app/javascript/retrospring/features/answerbox/smile.ts +++ b/app/javascript/retrospring/features/answerbox/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 answerboxSmileHandler(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-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 answerboxSmileHandler(event: Event): void { break; } } - } - }); + }); } \ No newline at end of file From 5295dc83c8ac3893b4a7b85569d04da891874fd0 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 05:05:43 +0200 Subject: [PATCH 6/7] Refactor answer subscribing to use request.js --- .../features/answerbox/subscribe.ts | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/javascript/retrospring/features/answerbox/subscribe.ts b/app/javascript/retrospring/features/answerbox/subscribe.ts index 0229cc9a..977d410e 100644 --- a/app/javascript/retrospring/features/answerbox/subscribe.ts +++ b/app/javascript/retrospring/features/answerbox/subscribe.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'; @@ -20,13 +20,15 @@ export function answerboxSubscribeHandler(event: Event): void { targetUrl = '/ajax/unsubscribe'; } - Rails.ajax({ - url: targetUrl, - type: 'POST', - data: new URLSearchParams({ + post(targetUrl, { + body: { answer: id - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { button.dataset.torpedo = ["yes", "no"][torpedo]; button.children[0].nextSibling.textContent = ' ' + (torpedo ? I18n.translate('voc.unsubscribe') : I18n.translate('voc.subscribe')); @@ -34,10 +36,9 @@ export function answerboxSubscribeHandler(event: Event): void { } else { showErrorNotification(I18n.translate(`frontend.subscription.fail.${torpedo ? 'subscribe' : 'unsubscribe'}`)); } - }, - 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 f091fd4193c12d8ee8a87465f394eab5ed54aa44 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 05:20:52 +0200 Subject: [PATCH 7/7] Remove unused import --- app/javascript/retrospring/features/settings/mute.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/app/javascript/retrospring/features/settings/mute.ts b/app/javascript/retrospring/features/settings/mute.ts index 8456c49d..91fbfe5e 100644 --- a/app/javascript/retrospring/features/settings/mute.ts +++ b/app/javascript/retrospring/features/settings/mute.ts @@ -1,4 +1,3 @@ -import Rails from '@rails/ujs'; import { destroy, post } from '@rails/request.js'; function createSubmitEvent(