2022-09-02 21:57:15 -07:00
|
|
|
import { post } from '@rails/request.js';
|
2022-01-02 18:26:14 -08:00
|
|
|
|
|
|
|
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 data = {
|
|
|
|
id: element.getAttribute('data-ib-id'),
|
|
|
|
answer: inboxEntry.querySelector<HTMLInputElement>('textarea[name=ib-answer]')?.value,
|
|
|
|
inbox: 'true'
|
|
|
|
};
|
|
|
|
|
2022-09-02 21:57:15 -07:00
|
|
|
post('/ajax/answer', {
|
|
|
|
body: data,
|
|
|
|
contentType: 'application/json'
|
|
|
|
})
|
|
|
|
.then(async response => {
|
|
|
|
const data = await response.json;
|
|
|
|
|
2022-01-02 18:26:14 -08:00
|
|
|
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);
|
2023-02-05 10:36:28 -08:00
|
|
|
|
2023-10-16 06:02:57 -07:00
|
|
|
const shareButton = inboxEntry.querySelector<HTMLButtonElement>('[data-controller="share"]');
|
|
|
|
if (shareButton != null) {
|
2023-10-18 11:01:59 -07:00
|
|
|
shareButton.dataset.shareUrlValue = data.sharing.url;
|
|
|
|
shareButton.dataset.shareTextValue = data.sharing.text;
|
2023-10-16 06:02:57 -07:00
|
|
|
}
|
2023-10-16 10:38:20 -07:00
|
|
|
|
|
|
|
const sharing = inboxEntry.querySelector<HTMLElement>('.inbox-entry__sharing');
|
|
|
|
if (sharing != null) {
|
|
|
|
sharing.dataset.inboxSharingConfigValue = JSON.stringify(data.sharing);
|
|
|
|
}
|
2023-02-05 10:36:28 -08:00
|
|
|
else {
|
|
|
|
(inboxEntry as HTMLElement).remove();
|
|
|
|
}
|
2022-09-02 21:57:15 -07:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
2022-01-03 06:03:26 -08:00
|
|
|
element.disabled = false;
|
2022-09-02 21:57:15 -07:00
|
|
|
});
|
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)) {
|
2022-01-11 13:32:43 -08:00
|
|
|
document.querySelector<HTMLButtonElement>(`button[name="ib-answer"][data-ib-id="${inboxId}"]`).click();
|
2022-01-02 18:26:14 -08:00
|
|
|
}
|
2023-02-05 10:36:28 -08:00
|
|
|
}
|