Retrospring/app/javascript/retrospring/features/questionbox/user.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
2.6 KiB
TypeScript
Raw Normal View History

import { post } from '@rails/request.js';
import { showErrorNotification, showNotification } from 'utilities/notifications';
import I18n from 'retrospring/i18n';
2022-01-03 13:29:09 -08:00
export function questionboxUserHandler(event: Event): void {
const button = event.target as HTMLButtonElement;
const promote = button.dataset.promote === "true";
const anonymousInput = document.querySelector<HTMLInputElement>('input[name=qb-anonymous]');
let anonymousQuestion = true;
if (anonymousInput) {
anonymousQuestion = anonymousInput.checked;
}
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').readOnly = true;
button.disabled = true;
post('/ajax/ask', {
body: {
rcpt: document.querySelector<HTMLInputElement>('input[name=qb-to]').value,
question: document.querySelector<HTMLInputElement>('textarea[name=qb-question]').value,
anonymousQuestion: String(anonymousQuestion)
},
contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) {
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').value = '';
// FIXME: also solve this using a Stimulus controller
const questionBox = document.getElementById('question-box');
if ('characterCountMaxValue' in questionBox.dataset) {
questionBox.querySelector<HTMLElement>('[data-character-count-target="counter"]').innerHTML = questionBox.dataset.characterCountMaxValue;
}
if (promote) {
const questionbox = document.querySelector('#question-box');
questionbox.classList.toggle('d-none');
const promote = document.querySelector('#question-box-promote');
promote.classList.toggle('d-none');
}
}
showNotification(data.message, data.success);
})
.catch(err => {
console.log(err);
showErrorNotification(I18n.translate('frontend.error.message'));
})
.finally(() => {
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').readOnly = false;
button.disabled = false;
});
}
2022-01-03 13:29:09 -08:00
export function questionboxPromoteHandler(): void {
const questionbox = document.querySelector('#question-box');
questionbox.classList.toggle('d-none');
const promote = document.querySelector('#question-box-promote');
promote.classList.toggle('d-none');
}
2022-01-03 13:29:09 -08:00
export function questionboxUserInputHandler(event: KeyboardEvent): void {
if (event.keyCode == 13 && (event.ctrlKey || event.metaKey)) {
document.querySelector<HTMLButtonElement>(`button[name=qb-ask]`).click();
}
}