Merge pull request #657 from Retrospring/feature/request-js-answerbox
Refactor answerbox TS functionality to use `@rails/request.js`
This commit is contained in:
commit
904285ae47
|
@ -1,4 +1,4 @@
|
||||||
import Rails from '@rails/ujs';
|
import { post } from '@rails/request.js';
|
||||||
import swal from 'sweetalert';
|
import swal from 'sweetalert';
|
||||||
|
|
||||||
import I18n from 'retrospring/i18n';
|
import I18n from 'retrospring/i18n';
|
||||||
|
@ -25,24 +25,23 @@ export function commentDestroyHandler(event: Event): void {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rails.ajax({
|
post('/ajax/destroy_comment', {
|
||||||
url: '/ajax/destroy_comment',
|
body: {
|
||||||
type: 'POST',
|
|
||||||
data: new URLSearchParams({
|
|
||||||
comment: id
|
comment: id
|
||||||
}).toString(),
|
},
|
||||||
success: (data) => {
|
contentType: 'application/json'
|
||||||
if (!data.success) return false;
|
})
|
||||||
|
.then(async response => {
|
||||||
|
const data = await response.json;
|
||||||
|
|
||||||
showNotification(data.message);
|
showNotification(data.message);
|
||||||
|
|
||||||
document.querySelector(`[data-comment-id="${id}"]`).remove();
|
document.querySelector(`[data-comment-id="${id}"]`).remove();
|
||||||
},
|
})
|
||||||
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'));
|
||||||
button.disabled = false;
|
button.disabled = false;
|
||||||
}
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
import Rails from '@rails/ujs';
|
import { post } from '@rails/request.js';
|
||||||
|
|
||||||
import I18n from 'retrospring/i18n';
|
import I18n from 'retrospring/i18n';
|
||||||
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
||||||
|
@ -19,14 +19,16 @@ export function commentCreateHandler(event: KeyboardEvent): boolean {
|
||||||
|
|
||||||
input.disabled = true;
|
input.disabled = true;
|
||||||
|
|
||||||
Rails.ajax({
|
post('/ajax/create_comment', {
|
||||||
url: '/ajax/create_comment',
|
body: {
|
||||||
type: 'POST',
|
|
||||||
data: new URLSearchParams({
|
|
||||||
answer: id,
|
answer: id,
|
||||||
comment: input.value
|
comment: input.value
|
||||||
}).toString(),
|
},
|
||||||
success: (data) => {
|
contentType: 'application/json'
|
||||||
|
})
|
||||||
|
.then(async response => {
|
||||||
|
const data = await response.json;
|
||||||
|
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
document.querySelector(`#ab-comments-${id}`).innerHTML = data.render;
|
document.querySelector(`#ab-comments-${id}`).innerHTML = data.render;
|
||||||
document.querySelector(`#ab-comment-count-${id}`).innerHTML = data.count;
|
document.querySelector(`#ab-comment-count-${id}`).innerHTML = data.count;
|
||||||
|
@ -39,14 +41,13 @@ export function commentCreateHandler(event: KeyboardEvent): boolean {
|
||||||
}
|
}
|
||||||
|
|
||||||
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(() => {
|
||||||
input.disabled = false;
|
input.disabled = false;
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
import Rails from '@rails/ujs';
|
import { post } from '@rails/request.js';
|
||||||
|
|
||||||
import I18n from 'retrospring/i18n';
|
import I18n from 'retrospring/i18n';
|
||||||
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
||||||
|
@ -22,25 +22,27 @@ export function commentSmileHandler(event: Event): void {
|
||||||
|
|
||||||
button.disabled = true;
|
button.disabled = true;
|
||||||
|
|
||||||
Rails.ajax({
|
post(targetUrl, {
|
||||||
url: targetUrl,
|
body: {
|
||||||
type: 'POST',
|
|
||||||
data: new URLSearchParams({
|
|
||||||
id: id
|
id: id
|
||||||
}).toString(),
|
},
|
||||||
success: (data) => {
|
contentType: 'application/json'
|
||||||
|
})
|
||||||
|
.then(async response => {
|
||||||
|
const data = await response.json;
|
||||||
|
|
||||||
success = data.success;
|
success = data.success;
|
||||||
if (success) {
|
if (success) {
|
||||||
document.querySelector(`#ab-comment-smile-count-${id}`).innerHTML = String(count);
|
document.querySelector(`#ab-comment-smile-count-${id}`).innerHTML = String(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
|
@ -53,6 +55,5 @@ export function commentSmileHandler(event: Event): void {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
|
@ -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 swal from 'sweetalert';
|
import swal from 'sweetalert';
|
||||||
|
|
||||||
|
@ -23,24 +23,25 @@ export function answerboxDestroyHandler(event: Event): void {
|
||||||
button.disabled = false;
|
button.disabled = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rails.ajax({
|
post('/ajax/destroy_answer', {
|
||||||
url: '/ajax/destroy_answer',
|
body: {
|
||||||
type: 'POST',
|
|
||||||
data: new URLSearchParams({
|
|
||||||
answer: answerId
|
answer: answerId
|
||||||
}).toString(),
|
},
|
||||||
success: (data) => {
|
contentType: 'application/json'
|
||||||
|
})
|
||||||
|
.then(async response => {
|
||||||
|
const data = await response.json;
|
||||||
|
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
document.querySelector(`.answerbox[data-id="${answerId}"]`).remove();
|
document.querySelector(`.answerbox[data-id="${answerId}"]`).remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
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'));
|
||||||
}
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
import Rails from '@rails/ujs';
|
import { post } from '@rails/request.js';
|
||||||
|
|
||||||
import I18n from 'retrospring/i18n';
|
import I18n from 'retrospring/i18n';
|
||||||
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
||||||
|
@ -22,25 +22,27 @@ export function answerboxSmileHandler(event: Event): void {
|
||||||
|
|
||||||
button.disabled = true;
|
button.disabled = true;
|
||||||
|
|
||||||
Rails.ajax({
|
post(targetUrl, {
|
||||||
url: targetUrl,
|
body: {
|
||||||
type: 'POST',
|
|
||||||
data: new URLSearchParams({
|
|
||||||
id: id
|
id: id
|
||||||
}).toString(),
|
},
|
||||||
success: (data) => {
|
contentType: 'application/json'
|
||||||
|
})
|
||||||
|
.then(async response => {
|
||||||
|
const data = await response.json;
|
||||||
|
|
||||||
success = data.success;
|
success = data.success;
|
||||||
if (success) {
|
if (success) {
|
||||||
document.querySelector(`#ab-smile-count-${id}`).innerHTML = String(count);
|
document.querySelector(`#ab-smile-count-${id}`).innerHTML = String(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
|
@ -53,6 +55,5 @@ export function answerboxSmileHandler(event: Event): void {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
import Rails from '@rails/ujs';
|
import { post } from '@rails/request.js';
|
||||||
|
|
||||||
import I18n from 'retrospring/i18n';
|
import I18n from 'retrospring/i18n';
|
||||||
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
||||||
|
@ -20,13 +20,15 @@ export function answerboxSubscribeHandler(event: Event): void {
|
||||||
targetUrl = '/ajax/unsubscribe';
|
targetUrl = '/ajax/unsubscribe';
|
||||||
}
|
}
|
||||||
|
|
||||||
Rails.ajax({
|
post(targetUrl, {
|
||||||
url: targetUrl,
|
body: {
|
||||||
type: 'POST',
|
|
||||||
data: new URLSearchParams({
|
|
||||||
answer: id
|
answer: id
|
||||||
}).toString(),
|
},
|
||||||
success: (data) => {
|
contentType: 'application/json'
|
||||||
|
})
|
||||||
|
.then(async response => {
|
||||||
|
const data = await response.json;
|
||||||
|
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
button.dataset.torpedo = ["yes", "no"][torpedo];
|
button.dataset.torpedo = ["yes", "no"][torpedo];
|
||||||
button.children[0].nextSibling.textContent = ' ' + (torpedo ? I18n.translate('voc.unsubscribe') : I18n.translate('voc.subscribe'));
|
button.children[0].nextSibling.textContent = ' ' + (torpedo ? I18n.translate('voc.unsubscribe') : I18n.translate('voc.subscribe'));
|
||||||
|
@ -34,10 +36,9 @@ export function answerboxSubscribeHandler(event: Event): void {
|
||||||
} else {
|
} else {
|
||||||
showErrorNotification(I18n.translate(`frontend.subscription.fail.${torpedo ? 'subscribe' : 'unsubscribe'}`));
|
showErrorNotification(I18n.translate(`frontend.subscription.fail.${torpedo ? 'subscribe' : 'unsubscribe'}`));
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
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'));
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
|
@ -1,4 +1,3 @@
|
||||||
import Rails from '@rails/ujs';
|
|
||||||
import { destroy, post } from '@rails/request.js';
|
import { destroy, post } from '@rails/request.js';
|
||||||
|
|
||||||
function createSubmitEvent(
|
function createSubmitEvent(
|
||||||
|
|
Loading…
Reference in New Issue