From 128d2142da967f71fcda3191a447a3d80660f71e Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Mon, 3 Jan 2022 03:26:14 +0100 Subject: [PATCH] Port inbox entry answer functionality to TypeScript --- .../features/inbox/entry/answer.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 app/javascript/retrospring/features/inbox/entry/answer.ts diff --git a/app/javascript/retrospring/features/inbox/entry/answer.ts b/app/javascript/retrospring/features/inbox/entry/answer.ts new file mode 100644 index 00000000..57c983de --- /dev/null +++ b/app/javascript/retrospring/features/inbox/entry/answer.ts @@ -0,0 +1,49 @@ +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('.inbox-entry'); + + 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('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); + return false; + } + updateDeleteButton(false); + showNotification(data.message); + (inboxEntry as HTMLElement).remove(); + }, + error: (data, status, xhr) => { + console.log(data, status, xhr); + } + }); +} + +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(`button[name=ib-answer][data-ib-id=${inboxId}]`).click(); + } +} \ No newline at end of file