From d3d4644776150941eea9532a25a74cf857bcad17 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Tue, 28 Dec 2021 21:13:01 +0100 Subject: [PATCH] Port list membership actions from CoffeeScript to TypeScript --- .../retrospring/features/lists/membership.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 app/javascript/retrospring/features/lists/membership.ts diff --git a/app/javascript/retrospring/features/lists/membership.ts b/app/javascript/retrospring/features/lists/membership.ts new file mode 100644 index 00000000..d2d63f7e --- /dev/null +++ b/app/javascript/retrospring/features/lists/membership.ts @@ -0,0 +1,37 @@ +import Rails from '@rails/ujs'; +import I18n from '../../../legacy/i18n'; + +export function listMembershipHandler(event: Event): void { + const checkbox = event.target as HTMLInputElement; + const list = checkbox.dataset.list; + let memberCount = Number(document.querySelector(`span#${list}-members`).innerHTML); + + checkbox.setAttribute('disabled', 'disabled'); + + memberCount += checkbox.checked ? 1 : -1; + + Rails.ajax({ + url: '/ajax/list_membership', + type: 'POST', + data: new URLSearchParams({ + list: list, + user: checkbox.dataset.user, + add: String(checkbox.checked) + }).toString(), + success: (data) => { + if (data.success) { + checkbox.checked = data.checked; + document.querySelector(`span#${list}-members`).innerHTML = memberCount.toString(); + } + + window['showNotification'](data.message, data.success); + }, + error: (data, status, xhr) => { + console.log(data, status, xhr); + window['showNotification'](I18n.translate('frontend.error.message'), false); + }, + complete: () => { + checkbox.removeAttribute('disabled'); + } + }); +} \ No newline at end of file