mirror of
https://github.com/Retrospring/retrospring.git
synced 2024-11-20 18:19:52 +01:00
Port (un)following from CoffeeScript to TypeScript
This commit is contained in:
parent
cf8d6b0884
commit
2cc8a33d5b
1 changed files with 45 additions and 0 deletions
45
app/javascript/retrospring/features/user/action.ts
Normal file
45
app/javascript/retrospring/features/user/action.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
import Rails from '@rails/ujs';
|
||||
import I18n from '../../../legacy/i18n';
|
||||
|
||||
export function userActionHandler(event: Event): void {
|
||||
const button: HTMLButtonElement = event.target as HTMLButtonElement;
|
||||
const target = button.dataset.target;
|
||||
const action = button.dataset.action;
|
||||
|
||||
let targetURL = action === 'follow' ? '/ajax/create_friend' : '/ajax/destroy_friend';
|
||||
let success = false;
|
||||
|
||||
Rails.ajax({
|
||||
url: targetURL,
|
||||
type: 'POST',
|
||||
data: new URLSearchParams({
|
||||
screen_name: target
|
||||
}).toString(),
|
||||
success: (data) => {
|
||||
success = data.success;
|
||||
window['showNotification'](data.message, data.success);
|
||||
},
|
||||
error: (data, status, xhr) => {
|
||||
console.log(data, status, xhr);
|
||||
window['showNotification'](I18n.translate('frontend.error.message'), false);
|
||||
},
|
||||
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;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue