From 5aab54acdb4e23cbd1b40c22032b238d02c2ac8d Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 07:51:30 +0200 Subject: [PATCH] Refactor list membership functionality to use request.js --- .../retrospring/features/lists/membership.ts | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app/javascript/retrospring/features/lists/membership.ts b/app/javascript/retrospring/features/lists/membership.ts index bcc19a9d..482dae9b 100644 --- a/app/javascript/retrospring/features/lists/membership.ts +++ b/app/javascript/retrospring/features/lists/membership.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import { showNotification, showErrorNotification } from 'utilities/notifications'; import I18n from 'retrospring/i18n'; @@ -12,15 +12,17 @@ export function listMembershipHandler(event: Event): void { memberCount += checkbox.checked ? 1 : -1; - Rails.ajax({ - url: '/ajax/list_membership', - type: 'POST', - data: new URLSearchParams({ + post('/ajax/list_membership', { + body: { list: list, user: checkbox.dataset.user, add: String(checkbox.checked) - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { checkbox.checked = data.checked; memberCountElement.innerHTML = I18n.t('frontend.list.item.members', { count: memberCount }); @@ -28,13 +30,12 @@ export function listMembershipHandler(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(() => { checkbox.removeAttribute('disabled'); - } - }); + }); } \ No newline at end of file