2022-09-02 22:50:16 -07:00
|
|
|
import { post } from '@rails/request.js';
|
2022-01-02 19:31:41 -08:00
|
|
|
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
2022-01-13 14:25:46 -08:00
|
|
|
import I18n from 'retrospring/i18n';
|
2021-12-28 12:13:52 -08:00
|
|
|
|
|
|
|
export function createListHandler(event: Event): void {
|
|
|
|
const button = event.target as HTMLButtonElement;
|
|
|
|
const input = document.querySelector<HTMLInputElement>('input#new-list-name');
|
|
|
|
|
2022-09-02 22:50:16 -07:00
|
|
|
post('/ajax/create_list', {
|
|
|
|
body: {
|
2021-12-28 12:13:52 -08:00
|
|
|
name: input.value,
|
|
|
|
user: button.dataset.user
|
2022-09-02 22:50:16 -07:00
|
|
|
},
|
|
|
|
contentType: 'application/json'
|
|
|
|
})
|
|
|
|
.then(async response => {
|
|
|
|
const data = await response.json;
|
|
|
|
|
2021-12-28 12:13:52 -08:00
|
|
|
if (data.success) {
|
|
|
|
document.querySelector('#lists-list ul.list-group').insertAdjacentHTML('beforeend', data.render);
|
|
|
|
}
|
|
|
|
|
2022-01-02 19:31:41 -08:00
|
|
|
showNotification(data.message, data.success);
|
2022-09-02 22:50:16 -07:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
2022-01-02 19:31:41 -08:00
|
|
|
showErrorNotification(I18n.translate('frontend.error.message'));
|
2022-09-02 22:50:16 -07:00
|
|
|
});
|
2021-12-28 12:13:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function createListInputHandler(event: KeyboardEvent): void {
|
2021-12-30 03:23:35 -08:00
|
|
|
// Return key
|
2021-12-28 12:13:52 -08:00
|
|
|
if (event.which === 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
document.querySelector<HTMLButtonElement>('button#create-list').click();
|
|
|
|
}
|
|
|
|
}
|