Refactor comment smiling to use request.js
This commit is contained in:
parent
40637c5f6d
commit
5ea28e1830
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
Loading…
Reference in New Issue