Refactor list creation to use request.js

This commit is contained in:
Andreas Nedbal 2022-09-03 07:50:16 +02:00
parent eb0271210b
commit af0258c8aa
1 changed files with 13 additions and 12 deletions

View File

@ -1,4 +1,4 @@
import Rails from '@rails/ujs'; import { post } from '@rails/request.js';
import { showNotification, showErrorNotification } from 'utilities/notifications'; import { showNotification, showErrorNotification } from 'utilities/notifications';
import I18n from 'retrospring/i18n'; import I18n from 'retrospring/i18n';
@ -6,25 +6,26 @@ export function createListHandler(event: Event): void {
const button = event.target as HTMLButtonElement; const button = event.target as HTMLButtonElement;
const input = document.querySelector<HTMLInputElement>('input#new-list-name'); const input = document.querySelector<HTMLInputElement>('input#new-list-name');
Rails.ajax({ post('/ajax/create_list', {
url: '/ajax/create_list', body: {
type: 'POST',
data: new URLSearchParams({
name: input.value, name: input.value,
user: button.dataset.user user: button.dataset.user
}).toString(), },
success: (data) => { contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) { if (data.success) {
document.querySelector('#lists-list ul.list-group').insertAdjacentHTML('beforeend', data.render); document.querySelector('#lists-list ul.list-group').insertAdjacentHTML('beforeend', data.render);
} }
showNotification(data.message, data.success); showNotification(data.message, data.success);
}, })
error: (data, status, xhr) => { .catch(err => {
console.log(data, status, xhr); console.log(err);
showErrorNotification(I18n.translate('frontend.error.message')); showErrorNotification(I18n.translate('frontend.error.message'));
} });
});
} }
export function createListInputHandler(event: KeyboardEvent): void { export function createListInputHandler(event: KeyboardEvent): void {