2021-12-24 18:33:33 -08:00
|
|
|
import Rails from '@rails/ujs';
|
2022-01-02 19:31:41 -08:00
|
|
|
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
2022-01-13 14:25:46 -08:00
|
|
|
import I18n from 'retrospring/i18n';
|
2021-12-24 18:33:33 -08:00
|
|
|
|
|
|
|
export function userActionHandler(event: Event): void {
|
|
|
|
const button: HTMLButtonElement = event.target as HTMLButtonElement;
|
|
|
|
const target = button.dataset.target;
|
|
|
|
const action = button.dataset.action;
|
|
|
|
|
2021-12-24 19:05:03 -08:00
|
|
|
const targetURL = action === 'follow' ? '/ajax/create_friend' : '/ajax/destroy_friend';
|
2021-12-24 18:33:33 -08:00
|
|
|
let success = false;
|
|
|
|
|
|
|
|
Rails.ajax({
|
|
|
|
url: targetURL,
|
|
|
|
type: 'POST',
|
|
|
|
data: new URLSearchParams({
|
|
|
|
screen_name: target
|
|
|
|
}).toString(),
|
|
|
|
success: (data) => {
|
|
|
|
success = data.success;
|
2022-01-02 19:31:41 -08:00
|
|
|
showNotification(data.message, data.success);
|
2021-12-24 18:33:33 -08:00
|
|
|
},
|
|
|
|
error: (data, status, xhr) => {
|
|
|
|
console.log(data, status, xhr);
|
2022-01-02 19:31:41 -08:00
|
|
|
showErrorNotification(I18n.translate('frontend.error.message'));
|
2021-12-24 18:33:33 -08:00
|
|
|
},
|
|
|
|
complete: () => {
|
|
|
|
if (!success) return;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case 'follow':
|
|
|
|
button.dataset.action = 'unfollow';
|
|
|
|
button.innerText = I18n.translate('views.actions.unfollow');
|
|
|
|
button.classList.remove('btn-primary');
|
|
|
|
button.classList.add('btn-default');
|
|
|
|
break;
|
|
|
|
case 'unfollow':
|
|
|
|
button.dataset.action = 'follow';
|
|
|
|
button.innerText = I18n.translate('views.actions.follow');
|
|
|
|
button.classList.remove('btn-default');
|
|
|
|
button.classList.add('btn-primary');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|