From a6bc27e884ce840615461c11ca2feaa52c14be9a Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Tue, 4 Jan 2022 18:13:14 +0100 Subject: [PATCH] Port question functionality to TypeScript --- app/javascript/packs/application.ts | 4 +- .../retrospring/features/question/answer.ts | 52 +++++++++++++++++++ .../retrospring/features/question/index.ts | 9 ++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 app/javascript/retrospring/features/question/answer.ts create mode 100644 app/javascript/retrospring/features/question/index.ts diff --git a/app/javascript/packs/application.ts b/app/javascript/packs/application.ts index e82936b3..29407973 100644 --- a/app/javascript/packs/application.ts +++ b/app/javascript/packs/application.ts @@ -5,6 +5,7 @@ import initUser from 'retrospring/features/user'; import initSettings from 'retrospring/features/settings/index'; import initLists from 'retrospring/features/lists'; import initQuestionbox from 'retrospring/features/questionbox'; +import initQuestion from 'retrospring/features/questionbox'; start(); document.addEventListener('turbolinks:load', initAnswerbox); @@ -12,4 +13,5 @@ document.addEventListener('DOMContentLoaded', initInbox); document.addEventListener('DOMContentLoaded', initUser); document.addEventListener('turbolinks:load', initSettings); document.addEventListener('DOMContentLoaded', initLists); -document.addEventListener('DOMContentLoaded', initQuestionbox); \ No newline at end of file +document.addEventListener('DOMContentLoaded', initQuestionbox); +document.addEventListener('DOMContentLoaded', initQuestion); \ No newline at end of file diff --git a/app/javascript/retrospring/features/question/answer.ts b/app/javascript/retrospring/features/question/answer.ts new file mode 100644 index 00000000..98a5ce58 --- /dev/null +++ b/app/javascript/retrospring/features/question/answer.ts @@ -0,0 +1,52 @@ +import Rails from '@rails/ujs'; + +import { showNotification, showErrorNotification } from 'utilities/notifications'; + +export function questionAnswerHandler(event: Event): void { + const button = event.target as HTMLButtonElement; + const questionId = button.dataset.qId; + + button.disabled = true; + document.querySelector('textarea#q-answer-text').readOnly = true; + + const shareTo = []; + Array.from(document.querySelectorAll(`input[type=checkbox][name=share][data-q-id=${questionId}]:checked`)) + .forEach((element: HTMLInputElement) => { + shareTo.push(element.dataset.service); + }); + + const data = { + id: questionId, + answer: document.querySelector('textarea#q-answer-text').value, + share: JSON.stringify(shareTo), + inbox: String(false) + }; + + Rails.ajax({ + url: '/ajax/answer', + type: 'POST', + data: new URLSearchParams(data).toString(), + success: (data) => { + if (!data.success) { + showErrorNotification(data.message); + button.disabled = false; + document.querySelector('textarea#q-answer-text').readOnly = false; + return false; + } + + showNotification(data.message); + document.querySelector('div#q-answer-box').remove(); + }, + error: (data, status, xhr) => { + console.log(data, status, xhr); + button.disabled = false; + document.querySelector('textarea#q-answer-text').readOnly = false; + } + }); +} + +export function questionAnswerInputHandler(event: KeyboardEvent): void { + if (event.keyCode == 13 && (event.ctrlKey || event.metaKey)) { + document.querySelector('button#q-answer-btn').click(); + } +} \ No newline at end of file diff --git a/app/javascript/retrospring/features/question/index.ts b/app/javascript/retrospring/features/question/index.ts new file mode 100644 index 00000000..bb177794 --- /dev/null +++ b/app/javascript/retrospring/features/question/index.ts @@ -0,0 +1,9 @@ +import registerEvents from 'utilities/registerEvents'; +import { questionAnswerHandler, questionAnswerInputHandler } from './answer'; + +export default (): void => { + registerEvents([ + { type: 'click', target: '#q-answer-btn', handler: questionAnswerHandler, global: true }, + { type: 'keydown', target: '#q-answer-text', handler: questionAnswerInputHandler, global: true } + ]); +} \ No newline at end of file