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 {
|
2022-06-09 14:08:38 -07:00
|
|
|
event.preventDefault();
|
2021-12-24 18:33:33 -08:00
|
|
|
const button: HTMLButtonElement = event.target as HTMLButtonElement;
|
|
|
|
const target = button.dataset.target;
|
|
|
|
const action = button.dataset.action;
|
|
|
|
|
2022-06-09 14:08:38 -07:00
|
|
|
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;
|
|
|
|
}
|
2021-12-24 18:33:33 -08:00
|
|
|
let success = false;
|
|
|
|
|
|
|
|
Rails.ajax({
|
|
|
|
url: targetURL,
|
|
|
|
type: 'POST',
|
|
|
|
data: new URLSearchParams({
|
2022-01-02 15:31:55 -08:00
|
|
|
screen_name: target,
|
2022-06-09 14:08:38 -07:00
|
|
|
type: relationshipType,
|
2021-12-24 18:33:33 -08:00
|
|
|
}).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';
|
2022-07-30 10:58:49 -07:00
|
|
|
button.innerText = I18n.translate('voc.unfollow');
|
2021-12-24 18:33:33 -08:00
|
|
|
button.classList.remove('btn-primary');
|
|
|
|
button.classList.add('btn-default');
|
|
|
|
break;
|
|
|
|
case 'unfollow':
|
2022-06-11 14:08:41 -07:00
|
|
|
resetFollowButton(button);
|
2021-12-24 18:33:33 -08:00
|
|
|
break;
|
2022-06-09 14:08:38 -07:00
|
|
|
case 'block':
|
|
|
|
button.dataset.action = 'unblock';
|
2022-07-30 09:50:46 -07:00
|
|
|
button.querySelector('span').innerText = I18n.translate('voc.unblock');
|
2022-06-25 04:04:24 -07:00
|
|
|
if (button.classList.contains('btn')) {
|
|
|
|
button.classList.remove('btn-primary');
|
|
|
|
button.classList.add('btn-default');
|
|
|
|
}
|
2022-06-11 14:08:41 -07:00
|
|
|
resetFollowButton(document.querySelector<HTMLButtonElement>('button[data-action="unfollow"]'));
|
2022-06-09 14:08:38 -07:00
|
|
|
break;
|
|
|
|
case 'unblock':
|
|
|
|
button.dataset.action = 'block';
|
2022-07-30 09:50:46 -07:00
|
|
|
button.querySelector('span').innerText = I18n.translate('voc.block');
|
2022-06-25 04:04:24 -07:00
|
|
|
if (button.classList.contains('btn')) {
|
|
|
|
button.classList.remove('btn-default');
|
|
|
|
button.classList.add('btn-primary');
|
|
|
|
}
|
2022-06-09 14:08:38 -07:00
|
|
|
break;
|
2021-12-24 18:33:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-06-11 14:08:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function resetFollowButton(button: HTMLButtonElement) {
|
|
|
|
button.dataset.action = 'follow';
|
2022-07-30 10:58:49 -07:00
|
|
|
button.innerText = I18n.translate('voc.follow');
|
2022-06-11 14:08:41 -07:00
|
|
|
button.classList.remove('btn-default');
|
|
|
|
button.classList.add('btn-primary');
|
2021-12-24 18:33:33 -08:00
|
|
|
}
|