Merge pull request #661 from Retrospring/feature/request-js-question

Refactor question(box) TS functionality to use `@rails/request.js`
This commit is contained in:
Karina Kwiatek 2022-09-03 21:44:38 +02:00 committed by GitHub
commit bc9db3e8fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 60 deletions

View file

@ -1,4 +1,4 @@
import Rails from '@rails/ujs'; import { post } from '@rails/request.js';
import { showNotification, showErrorNotification } from 'utilities/notifications'; import { showNotification, showErrorNotification } from 'utilities/notifications';
@ -22,11 +22,13 @@ export function questionAnswerHandler(event: Event): void {
inbox: String(false) inbox: String(false)
}; };
Rails.ajax({ post('/ajax/answer', {
url: '/ajax/answer', body: data,
type: 'POST', contentType: 'application/json'
data: new URLSearchParams(data).toString(), })
success: (data) => { .then(async response => {
const data = await response.json;
if (!data.success) { if (!data.success) {
showErrorNotification(data.message); showErrorNotification(data.message);
button.disabled = false; button.disabled = false;
@ -37,13 +39,12 @@ export function questionAnswerHandler(event: Event): void {
showNotification(data.message); showNotification(data.message);
document.querySelector('div#answers').insertAdjacentHTML('afterbegin', data.render); document.querySelector('div#answers').insertAdjacentHTML('afterbegin', data.render);
document.querySelector('div#q-answer-box').remove(); document.querySelector('div#q-answer-box').remove();
}, })
error: (data, status, xhr) => { .catch(err => {
console.log(data, status, xhr); console.log(err);
button.disabled = false; button.disabled = false;
document.querySelector<HTMLInputElement>('textarea#q-answer-text').readOnly = false; document.querySelector<HTMLInputElement>('textarea#q-answer-text').readOnly = false;
} });
});
} }
export function questionAnswerInputHandler(event: KeyboardEvent): void { export function questionAnswerInputHandler(event: KeyboardEvent): void {

View file

@ -1,10 +1,10 @@
import Rails from '@rails/ujs'; import { post } from '@rails/request.js';
import { showErrorNotification, showNotification } from 'utilities/notifications'; import { showErrorNotification, showNotification } from 'utilities/notifications';
import swal from 'sweetalert'; import swal from 'sweetalert';
import I18n from 'retrospring/i18n'; import I18n from 'retrospring/i18n';
export function questionboxDestroyHandler(event: Event): void { export function questionDestroyHandler(event: Event): void {
event.preventDefault(); event.preventDefault();
const button = event.target as HTMLButtonElement; const button = event.target as HTMLButtonElement;
const questionId = button.dataset.qId; const questionId = button.dataset.qId;
@ -23,14 +23,16 @@ export function questionboxDestroyHandler(event: Event): void {
button.disabled = false; button.disabled = false;
return; return;
} }
Rails.ajax({ post('/ajax/destroy_question', {
url: '/ajax/destroy_question', body: {
type: 'POST',
data: new URLSearchParams({
question: questionId question: questionId
}).toString(), },
success: (data) => { contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) { if (data.success) {
if (button.dataset.redirect !== undefined) { if (button.dataset.redirect !== undefined) {
window.location.pathname = button.dataset.redirect; window.location.pathname = button.dataset.redirect;
@ -43,11 +45,10 @@ export function questionboxDestroyHandler(event: Event): void {
} }
showNotification(data.message, data.success); showNotification(data.message, data.success);
}, })
error: (data, status, xhr) => { .catch(err => {
console.log(data, status, xhr); console.log(err);
showErrorNotification(I18n.translate('frontend.error.message')); showErrorNotification(I18n.translate('frontend.error.message'));
} });
});
}); });
} }

View file

@ -1,8 +1,12 @@
import registerEvents from 'utilities/registerEvents'; import registerEvents from 'utilities/registerEvents';
import { questionAnswerHandler, questionAnswerInputHandler } from './answer'; import { questionAnswerHandler, questionAnswerInputHandler } from './answer';
import { questionDestroyHandler } from './destroy';
import { questionReportHandler } from './report';
export default (): void => { export default (): void => {
registerEvents([ registerEvents([
{ type: 'click', target: '[data-action=ab-question-report]', handler: questionReportHandler, global: true },
{ type: 'click', target: '[data-action=ab-question-destroy]', handler: questionDestroyHandler, global: true },
{ type: 'click', target: '#q-answer-btn', handler: questionAnswerHandler, global: true }, { type: 'click', target: '#q-answer-btn', handler: questionAnswerHandler, global: true },
{ type: 'keydown', target: '#q-answer-text', handler: questionAnswerInputHandler, global: true } { type: 'keydown', target: '#q-answer-text', handler: questionAnswerInputHandler, global: true }
]); ]);

View file

@ -1,6 +1,6 @@
import { reportDialog } from 'utilities/reportDialog'; import { reportDialog } from 'utilities/reportDialog';
export function questionboxReportHandler(event: Event): void { export function questionReportHandler(event: Event): void {
event.preventDefault(); event.preventDefault();
const button = event.target as HTMLButtonElement; const button = event.target as HTMLButtonElement;
const questionId = button.dataset.qId; const questionId = button.dataset.qId;

View file

@ -1,4 +1,4 @@
import Rails from '@rails/ujs'; import { post } from '@rails/request.js';
import { showErrorNotification, showNotification } from 'utilities/notifications'; import { showErrorNotification, showNotification } from 'utilities/notifications';
import I18n from 'retrospring/i18n'; import I18n from 'retrospring/i18n';
@ -8,31 +8,32 @@ export function questionboxAllHandler(event: Event): void {
button.disabled = true; button.disabled = true;
document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').readOnly = true; document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').readOnly = true;
Rails.ajax({ post('/ajax/ask', {
url: '/ajax/ask', body: {
type: 'POST',
data: new URLSearchParams({
rcpt: 'followers', rcpt: 'followers',
question: document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').value, question: document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').value,
anonymousQuestion: 'false' anonymousQuestion: 'false'
}).toString(), },
success: (data) => { contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) { if (data.success) {
document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').value = ''; document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').value = '';
window['$']('#modal-ask-followers').modal('hide'); window['$']('#modal-ask-followers').modal('hide');
} }
showNotification(data.message, data.success); showNotification(data.message, data.success);
}, })
error: (data, status, xhr) => { .catch(err => {
console.log(data, status, xhr); console.log(err);
showErrorNotification(I18n.translate('frontend.error.message')); showErrorNotification(I18n.translate('frontend.error.message'));
}, })
complete: () => { .finally(() => {
button.disabled = false; button.disabled = false;
document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').readOnly = false; document.querySelector<HTMLInputElement>('textarea[name=qb-all-question]').readOnly = false;
} });
});
} }
export function questionboxAllInputHandler(event: KeyboardEvent): void { export function questionboxAllInputHandler(event: KeyboardEvent): void {

View file

@ -1,15 +1,11 @@
import registerEvents from 'utilities/registerEvents'; import registerEvents from 'utilities/registerEvents';
import { questionboxAllHandler, questionboxAllInputHandler } from './all'; import { questionboxAllHandler, questionboxAllInputHandler } from './all';
import { questionboxDestroyHandler } from './destroy';
import { questionboxReportHandler } from './report';
import { questionboxPromoteHandler, questionboxUserHandler, questionboxUserInputHandler } from './user'; import { questionboxPromoteHandler, questionboxUserHandler, questionboxUserInputHandler } from './user';
export default (): void => { export default (): void => {
registerEvents([ registerEvents([
{ type: 'click', target: '[data-action=ab-question-report]', handler: questionboxReportHandler, global: true },
{ type: 'click', target: '[name=qb-ask]', handler: questionboxUserHandler, global: true }, { type: 'click', target: '[name=qb-ask]', handler: questionboxUserHandler, global: true },
{ type: 'click', target: '#new-question', handler: questionboxPromoteHandler, global: true }, { type: 'click', target: '#new-question', handler: questionboxPromoteHandler, global: true },
{ type: 'click', target: '[data-action=ab-question-destroy]', handler: questionboxDestroyHandler, global: true },
{ type: 'click', target: '[name=qb-all-ask]', handler: questionboxAllHandler, global: true }, { type: 'click', target: '[name=qb-all-ask]', handler: questionboxAllHandler, global: true },
{ type: 'keydown', target: '[name=qb-question]', handler: questionboxUserInputHandler, global: true }, { type: 'keydown', target: '[name=qb-question]', handler: questionboxUserInputHandler, global: true },
{ type: 'keydown', target: '[name=qb-all-question]', handler: questionboxAllInputHandler, global: true } { type: 'keydown', target: '[name=qb-all-question]', handler: questionboxAllInputHandler, global: true }

View file

@ -1,4 +1,4 @@
import Rails from '@rails/ujs'; import { post } from '@rails/request.js';
import { showErrorNotification, showNotification } from 'utilities/notifications'; import { showErrorNotification, showNotification } from 'utilities/notifications';
import I18n from 'retrospring/i18n'; import I18n from 'retrospring/i18n';
@ -15,15 +15,17 @@ export function questionboxUserHandler(event: Event): void {
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').readOnly = true; document.querySelector<HTMLInputElement>('textarea[name=qb-question]').readOnly = true;
button.disabled = true; button.disabled = true;
Rails.ajax({ post('/ajax/ask', {
url: '/ajax/ask', body: {
type: 'POST',
data: new URLSearchParams({
rcpt: document.querySelector<HTMLInputElement>('input[name=qb-to]').value, rcpt: document.querySelector<HTMLInputElement>('input[name=qb-to]').value,
question: document.querySelector<HTMLInputElement>('textarea[name=qb-question]').value, question: document.querySelector<HTMLInputElement>('textarea[name=qb-question]').value,
anonymousQuestion: String(anonymousQuestion) anonymousQuestion: String(anonymousQuestion)
}).toString(), },
success: (data) => { contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
if (data.success) { if (data.success) {
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').value = ''; document.querySelector<HTMLInputElement>('textarea[name=qb-question]').value = '';
@ -37,16 +39,15 @@ export function questionboxUserHandler(event: Event): void {
} }
showNotification(data.message, data.success); showNotification(data.message, data.success);
}, })
error: (data, status, xhr) => { .catch(err => {
console.log(data, status, xhr); console.log(err);
showErrorNotification(I18n.translate('frontend.error.message')); showErrorNotification(I18n.translate('frontend.error.message'));
}, })
complete: () => { .finally(() => {
document.querySelector<HTMLInputElement>('textarea[name=qb-question]').readOnly = false; document.querySelector<HTMLInputElement>('textarea[name=qb-question]').readOnly = false;
button.disabled = false; button.disabled = false;
} });
});
} }
export function questionboxPromoteHandler(): void { export function questionboxPromoteHandler(): void {