2022-09-02 20:05:21 -07:00
|
|
|
import { post } from '@rails/request.js';
|
2022-01-08 15:54:56 -08:00
|
|
|
|
2022-01-13 14:25:46 -08:00
|
|
|
import I18n from 'retrospring/i18n';
|
2022-01-08 15:54:56 -08:00
|
|
|
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
|
|
|
|
|
|
|
export function answerboxSmileHandler(event: Event): void {
|
|
|
|
const button = event.target as HTMLButtonElement;
|
|
|
|
const id = button.dataset.aId;
|
|
|
|
const action = button.dataset.action;
|
|
|
|
let count = Number(document.querySelector(`#ab-smile-count-${id}`).innerHTML);
|
|
|
|
let success = false;
|
|
|
|
let targetUrl;
|
|
|
|
|
|
|
|
if (action === 'smile') {
|
|
|
|
count++;
|
|
|
|
targetUrl = '/ajax/create_smile';
|
|
|
|
}
|
|
|
|
else if (action === 'unsmile') {
|
|
|
|
count--;
|
|
|
|
targetUrl = '/ajax/destroy_smile';
|
|
|
|
}
|
|
|
|
|
|
|
|
button.disabled = true;
|
|
|
|
|
2022-09-02 20:05:21 -07:00
|
|
|
post(targetUrl, {
|
|
|
|
body: {
|
2022-01-08 15:54:56 -08:00
|
|
|
id: id
|
2022-09-02 20:05:21 -07:00
|
|
|
},
|
|
|
|
contentType: 'application/json'
|
|
|
|
})
|
|
|
|
.then(async response => {
|
|
|
|
const data = await response.json;
|
|
|
|
|
2022-01-08 15:54:56 -08:00
|
|
|
success = data.success;
|
|
|
|
if (success) {
|
|
|
|
document.querySelector(`#ab-smile-count-${id}`).innerHTML = String(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
showNotification(data.message, data.success);
|
2022-09-02 20:05:21 -07:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
2022-01-08 15:54:56 -08:00
|
|
|
showErrorNotification(I18n.translate('frontend.error.message'));
|
2022-09-02 20:05:21 -07:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2022-01-08 15:54:56 -08:00
|
|
|
button.disabled = false;
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
switch(action) {
|
|
|
|
case 'smile':
|
|
|
|
button.dataset.action = 'unsmile';
|
|
|
|
break;
|
|
|
|
case 'unsmile':
|
|
|
|
button.dataset.action = 'smile';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-09-02 20:05:21 -07:00
|
|
|
});
|
2022-01-08 15:54:56 -08:00
|
|
|
}
|