2022-09-02 22:51:30 -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:01 -08:00
|
|
|
|
|
|
|
export function listMembershipHandler(event: Event): void {
|
|
|
|
const checkbox = event.target as HTMLInputElement;
|
|
|
|
const list = checkbox.dataset.list;
|
2022-07-26 17:07:57 -07:00
|
|
|
const memberCountElement: HTMLElement = document.querySelector(`span#${list}-members`);
|
|
|
|
let memberCount = Number(memberCountElement.dataset.count);
|
2021-12-28 12:13:01 -08:00
|
|
|
|
|
|
|
checkbox.setAttribute('disabled', 'disabled');
|
|
|
|
|
|
|
|
memberCount += checkbox.checked ? 1 : -1;
|
|
|
|
|
2022-09-02 22:51:30 -07:00
|
|
|
post('/ajax/list_membership', {
|
|
|
|
body: {
|
2021-12-28 12:13:01 -08:00
|
|
|
list: list,
|
|
|
|
user: checkbox.dataset.user,
|
|
|
|
add: String(checkbox.checked)
|
2022-09-02 22:51:30 -07:00
|
|
|
},
|
|
|
|
contentType: 'application/json'
|
|
|
|
})
|
|
|
|
.then(async response => {
|
|
|
|
const data = await response.json;
|
|
|
|
|
2021-12-28 12:13:01 -08:00
|
|
|
if (data.success) {
|
|
|
|
checkbox.checked = data.checked;
|
2022-07-27 14:13:36 -07:00
|
|
|
memberCountElement.innerHTML = I18n.t('frontend.list.item.members', { count: memberCount });
|
2022-07-26 17:07:57 -07:00
|
|
|
memberCountElement.dataset.count = memberCount.toString();
|
2021-12-28 12:13:01 -08:00
|
|
|
}
|
|
|
|
|
2022-01-02 19:31:41 -08:00
|
|
|
showNotification(data.message, data.success);
|
2022-09-02 22:51:30 -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:51:30 -07:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2021-12-28 12:13:01 -08:00
|
|
|
checkbox.removeAttribute('disabled');
|
2022-09-02 22:51:30 -07:00
|
|
|
});
|
2021-12-28 12:13:01 -08:00
|
|
|
}
|