2022-09-02 20:03:25 -07:00
|
|
|
import { post } from '@rails/request.js';
|
2022-01-08 15:56:46 -08:00
|
|
|
|
2022-01-13 14:25:46 -08:00
|
|
|
import I18n from 'retrospring/i18n';
|
2022-01-08 15:56:46 -08:00
|
|
|
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
|
|
|
|
2022-01-08 17:21:42 -08:00
|
|
|
export function commentCreateHandler(event: KeyboardEvent): boolean {
|
2022-01-08 15:56:46 -08:00
|
|
|
const input = event.target as HTMLInputElement;
|
|
|
|
const id = input.dataset.aId;
|
|
|
|
const counter = document.querySelector(`#ab-comment-charcount-${id}`);
|
|
|
|
const group = document.querySelector(`[name=ab-comment-new-group][data-a-id="${id}"]`);
|
|
|
|
|
|
|
|
if (event.which === 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (input.value.length > 160) {
|
|
|
|
group.classList.add('has-error');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
input.disabled = true;
|
|
|
|
|
2022-09-02 20:03:25 -07:00
|
|
|
post('/ajax/create_comment', {
|
|
|
|
body: {
|
2022-01-08 15:56:46 -08:00
|
|
|
answer: id,
|
|
|
|
comment: input.value
|
2022-09-02 20:03:25 -07:00
|
|
|
},
|
|
|
|
contentType: 'application/json'
|
|
|
|
})
|
|
|
|
.then(async response => {
|
|
|
|
const data = await response.json;
|
|
|
|
|
2022-01-08 15:56:46 -08:00
|
|
|
if (data.success) {
|
|
|
|
document.querySelector(`#ab-comments-${id}`).innerHTML = data.render;
|
|
|
|
document.querySelector(`#ab-comment-count-${id}`).innerHTML = data.count;
|
|
|
|
input.value = '';
|
|
|
|
counter.innerHTML = String(160);
|
|
|
|
|
|
|
|
const sub = document.querySelector<HTMLElement>(`[data-action=ab-submarine][data-a-id="${id}"]`);
|
|
|
|
sub.dataset.torpedo = "no"
|
2022-01-09 06:51:29 -08:00
|
|
|
sub.children[0].nextSibling.textContent = ' ' + I18n.translate('views.actions.unsubscribe');
|
2022-01-08 15:56:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
showNotification(data.message, data.success);
|
2022-09-02 20:03:25 -07:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
2022-01-08 15:56:46 -08:00
|
|
|
showErrorNotification(I18n.translate('frontend.error.message'));
|
2022-09-02 20:03:25 -07:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2022-01-08 15:56:46 -08:00
|
|
|
input.disabled = false;
|
2022-09-02 20:03:25 -07:00
|
|
|
});
|
2022-01-08 15:56:46 -08:00
|
|
|
}
|
|
|
|
}
|