2022-01-03 13:21:00 -08:00
|
|
|
import Rails from '@rails/ujs';
|
|
|
|
import { showErrorNotification, showNotification } from 'utilities/notifications';
|
|
|
|
import I18n from '../../../legacy/i18n';
|
|
|
|
|
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";
|
|
|
|
const anonymousQuestion = document.querySelector<HTMLInputElement>('input[name=qb-anonymous]')?.checked;
|
|
|
|
|
|
|
|
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').readOnly = true;
|
|
|
|
button.disabled = true;
|
|
|
|
|
|
|
|
Rails.ajax({
|
|
|
|
url: '/ajax/ask',
|
|
|
|
type: 'POST',
|
|
|
|
data: new URLSearchParams({
|
|
|
|
rcpt: document.querySelector<HTMLInputElement>('input[name=qb-to]').value,
|
|
|
|
question: document.querySelector<HTMLInputElement>('textarea[name=qb-question]').value,
|
|
|
|
anonymousQuestion: String(anonymousQuestion)
|
|
|
|
}).toString(),
|
|
|
|
success: (data) => {
|
|
|
|
if (data.success) {
|
|
|
|
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').value = '';
|
|
|
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
error: (data, status, xhr) => {
|
|
|
|
console.log(data, status, xhr);
|
|
|
|
showErrorNotification(I18n.translate('frontend.error.message'));
|
|
|
|
},
|
|
|
|
complete: () => {
|
|
|
|
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').readOnly = false;
|
|
|
|
button.disabled = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|