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