Retrospring/app/javascript/retrospring/features/user/action.ts

88 lines
2.7 KiB
TypeScript
Raw Normal View History

import Rails from '@rails/ujs';
import { showNotification, showErrorNotification } from 'utilities/notifications';
import I18n from 'retrospring/i18n';
export function userActionHandler(event: Event): void {
event.preventDefault();
const button: HTMLButtonElement = event.target as HTMLButtonElement;
const target = button.dataset.target;
const action = button.dataset.action;
let targetURL, relationshipType;
switch (action) {
case 'follow':
targetURL = '/ajax/create_relationship';
relationshipType = 'follow';
break;
case 'unfollow':
targetURL = '/ajax/destroy_relationship';
relationshipType = 'follow';
break;
case 'block':
targetURL = '/ajax/create_relationship';
relationshipType = 'block';
break;
case 'unblock':
targetURL = '/ajax/destroy_relationship';
relationshipType = 'block';
break;
}
let success = false;
Rails.ajax({
url: targetURL,
type: 'POST',
data: new URLSearchParams({
screen_name: target,
type: relationshipType,
}).toString(),
success: (data) => {
success = data.success;
showNotification(data.message, data.success);
},
error: (data, status, xhr) => {
console.log(data, status, xhr);
showErrorNotification(I18n.translate('frontend.error.message'));
},
complete: () => {
if (!success) return;
switch (action) {
case 'follow':
button.dataset.action = 'unfollow';
2022-07-30 10:58:49 -07:00
button.innerText = I18n.translate('voc.unfollow');
button.classList.remove('btn-primary');
button.classList.add('btn-default');
break;
case 'unfollow':
resetFollowButton(button);
break;
case 'block':
button.dataset.action = 'unblock';
2022-07-30 09:50:46 -07:00
button.querySelector('span').innerText = I18n.translate('voc.unblock');
if (button.classList.contains('btn')) {
button.classList.remove('btn-primary');
button.classList.add('btn-default');
}
resetFollowButton(document.querySelector<HTMLButtonElement>('button[data-action="unfollow"]'));
break;
case 'unblock':
button.dataset.action = 'block';
2022-07-30 09:50:46 -07:00
button.querySelector('span').innerText = I18n.translate('voc.block');
if (button.classList.contains('btn')) {
button.classList.remove('btn-default');
button.classList.add('btn-primary');
}
break;
}
}
});
}
function resetFollowButton(button: HTMLButtonElement) {
button.dataset.action = 'follow';
2022-07-30 10:58:49 -07:00
button.innerText = I18n.translate('voc.follow');
button.classList.remove('btn-default');
button.classList.add('btn-primary');
}