2022-01-02 18:26:14 -08:00
|
|
|
import Rails from '@rails/ujs';
|
|
|
|
|
|
|
|
import { updateDeleteButton } from '../delete';
|
|
|
|
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
|
|
|
|
|
|
|
export function answerEntryHandler(event: Event): void {
|
|
|
|
const element: HTMLButtonElement = event.target as HTMLButtonElement;
|
|
|
|
const inboxEntry: HTMLElement = element.closest<HTMLElement>('.inbox-entry');
|
|
|
|
|
2022-01-03 06:03:26 -08:00
|
|
|
element.disabled = true;
|
|
|
|
|
2022-01-02 18:26:14 -08:00
|
|
|
const shareTo = [];
|
|
|
|
inboxEntry.querySelectorAll('input[type=checkbox][name=ib-share]:checked')
|
|
|
|
.forEach((element: HTMLInputElement) => {
|
|
|
|
shareTo.push(element.getAttribute('data-service'));
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
id: element.getAttribute('data-ib-id'),
|
|
|
|
answer: inboxEntry.querySelector<HTMLInputElement>('textarea[name=ib-answer]')?.value,
|
|
|
|
share: JSON.stringify(shareTo),
|
|
|
|
inbox: 'true'
|
|
|
|
};
|
|
|
|
|
|
|
|
Rails.ajax({
|
|
|
|
url: '/ajax/answer',
|
|
|
|
type: 'POST',
|
|
|
|
data: new URLSearchParams(data).toString(),
|
|
|
|
success: (data) => {
|
|
|
|
if (!data.success) {
|
|
|
|
showErrorNotification(data.message);
|
2022-01-03 06:03:26 -08:00
|
|
|
element.disabled = false;
|
2022-01-02 18:26:14 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
updateDeleteButton(false);
|
|
|
|
showNotification(data.message);
|
|
|
|
(inboxEntry as HTMLElement).remove();
|
|
|
|
},
|
|
|
|
error: (data, status, xhr) => {
|
|
|
|
console.log(data, status, xhr);
|
2022-01-03 06:03:26 -08:00
|
|
|
element.disabled = false;
|
2022-01-02 18:26:14 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function answerEntryInputHandler(event: KeyboardEvent): void {
|
|
|
|
const input = event.target as HTMLInputElement;
|
|
|
|
const inboxId = input.dataset.id;
|
|
|
|
|
|
|
|
if (event.keyCode == 13 && (event.ctrlKey || event.metaKey)) {
|
|
|
|
document.querySelector<HTMLButtonElement>(`button[name=ib-answer][data-ib-id=${inboxId}]`).click();
|
|
|
|
}
|
|
|
|
}
|