diff --git a/app/javascript/retrospring/features/lists/create.ts b/app/javascript/retrospring/features/lists/create.ts index 12ff95ec..52eb9d1f 100644 --- a/app/javascript/retrospring/features/lists/create.ts +++ b/app/javascript/retrospring/features/lists/create.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'; @@ -6,25 +6,26 @@ export function createListHandler(event: Event): void { const button = event.target as HTMLButtonElement; const input = document.querySelector('input#new-list-name'); - Rails.ajax({ - url: '/ajax/create_list', - type: 'POST', - data: new URLSearchParams({ + post('/ajax/create_list', { + body: { name: input.value, user: button.dataset.user - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { document.querySelector('#lists-list ul.list-group').insertAdjacentHTML('beforeend', data.render); } 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')); - } - }); + }); } export function createListInputHandler(event: KeyboardEvent): void { diff --git a/app/javascript/retrospring/features/lists/destroy.ts b/app/javascript/retrospring/features/lists/destroy.ts index 1386b7fb..203ce04c 100644 --- a/app/javascript/retrospring/features/lists/destroy.ts +++ b/app/javascript/retrospring/features/lists/destroy.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import swal from 'sweetalert'; import { showNotification, showErrorNotification } from 'utilities/notifications'; import I18n from 'retrospring/i18n'; @@ -18,13 +18,15 @@ export function destroyListHandler(event: Event): void { cancelButtonText: I18n.translate('voc.cancel'), closeOnConfirm: true }, () => { - Rails.ajax({ - url: '/ajax/destroy_list', - type: 'POST', - data: new URLSearchParams({ + post('/ajax/destroy_list', { + body: { list: list - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { const element = document.querySelector(`li.list-group-item#list-${list}`); @@ -34,11 +36,10 @@ export function destroyListHandler(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/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