2018-12-30 22:07:32 +00:00
|
|
|
enum UserRelationType {
|
|
|
|
None = 0,
|
|
|
|
Follow = 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface UserRelationInfo {
|
|
|
|
user_id: number;
|
|
|
|
subject_id: number;
|
|
|
|
relation_type: UserRelationType;
|
|
|
|
|
|
|
|
error: string;
|
|
|
|
message: string;
|
|
|
|
}
|
|
|
|
|
2019-06-10 17:04:53 +00:00
|
|
|
function userRelationsInit(): void {
|
2018-12-30 22:07:32 +00:00
|
|
|
const relationButtons: HTMLCollectionOf<HTMLElement> = document.getElementsByClassName('js-user-relation-action') as HTMLCollectionOf<HTMLElement>;
|
|
|
|
|
2019-06-10 17:04:53 +00:00
|
|
|
for(let i = 0; i < relationButtons.length; i++) {
|
|
|
|
switch(relationButtons[i].tagName.toLowerCase()) {
|
2018-12-30 22:07:32 +00:00
|
|
|
case 'a':
|
|
|
|
const anchor: HTMLAnchorElement = relationButtons[i] as HTMLAnchorElement;
|
|
|
|
anchor.removeAttribute('href');
|
|
|
|
anchor.removeAttribute('target');
|
|
|
|
anchor.removeAttribute('rel');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
relationButtons[i].addEventListener('click', userRelationSetEventHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function userRelationSetEventHandler(ev: Event): void {
|
|
|
|
const target: HTMLElement = this as HTMLElement,
|
|
|
|
userId: number = parseInt(target.dataset.relationUser),
|
|
|
|
relationType: UserRelationType = parseInt(target.dataset.relationType),
|
|
|
|
isButton: boolean = target.classList.contains('input__button'),
|
2019-02-28 21:06:30 +00:00
|
|
|
icon: HTMLElement = target.querySelector('[class^="fa"]'),
|
|
|
|
buttonBusy: string = 'input__button--busy',
|
|
|
|
iconAdd: string = 'fas fa-user-plus',
|
|
|
|
iconRemove: string = 'fas fa-user-minus',
|
|
|
|
iconBusy: string = 'fas fa-spinner fa-pulse';
|
2018-12-30 22:07:32 +00:00
|
|
|
|
2019-06-10 17:04:53 +00:00
|
|
|
if(isButton) {
|
|
|
|
if(target.classList.contains(buttonBusy))
|
2018-12-30 22:07:32 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
target.classList.add(buttonBusy);
|
|
|
|
}
|
|
|
|
|
2019-06-10 17:04:53 +00:00
|
|
|
if(icon) {
|
2019-02-28 21:06:30 +00:00
|
|
|
icon.className = iconBusy;
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:32 +00:00
|
|
|
userRelationSet(
|
|
|
|
userId,
|
|
|
|
relationType,
|
|
|
|
info => {
|
|
|
|
target.classList.remove(buttonBusy);
|
|
|
|
|
2019-06-10 17:04:53 +00:00
|
|
|
switch(info.relation_type) {
|
2018-12-30 22:07:32 +00:00
|
|
|
case UserRelationType.None:
|
2019-06-10 17:04:53 +00:00
|
|
|
if(isButton) {
|
|
|
|
if(target.classList.contains('input__button--destroy'))
|
2018-12-30 22:07:32 +00:00
|
|
|
target.classList.remove('input__button--destroy');
|
|
|
|
|
|
|
|
target.textContent = 'Follow';
|
|
|
|
}
|
|
|
|
|
2019-06-10 17:04:53 +00:00
|
|
|
if(icon) {
|
2019-02-28 21:06:30 +00:00
|
|
|
icon.className = iconAdd;
|
|
|
|
target.title = 'Follow';
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:32 +00:00
|
|
|
target.dataset.relationType = UserRelationType.Follow.toString();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UserRelationType.Follow:
|
2019-06-10 17:04:53 +00:00
|
|
|
if(isButton) {
|
|
|
|
if(!target.classList.contains('input__button--destroy'))
|
2018-12-30 22:07:32 +00:00
|
|
|
target.classList.add('input__button--destroy');
|
|
|
|
|
|
|
|
target.textContent = 'Unfollow';
|
|
|
|
}
|
|
|
|
|
2019-06-10 17:04:53 +00:00
|
|
|
if(icon) {
|
2019-02-28 21:06:30 +00:00
|
|
|
icon.className = iconRemove;
|
|
|
|
target.title = 'Unfollow';
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:32 +00:00
|
|
|
target.dataset.relationType = UserRelationType.None.toString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
msg => {
|
|
|
|
target.classList.remove(buttonBusy);
|
2019-01-22 16:38:20 +00:00
|
|
|
messageBox(msg);
|
2018-12-30 22:07:32 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function userRelationSet(
|
|
|
|
userId: number,
|
|
|
|
relationType: UserRelationType,
|
|
|
|
onSuccess: (info: UserRelationInfo) => void = null,
|
|
|
|
onFail: (message: string) => void = null
|
|
|
|
): void {
|
|
|
|
const xhr: XMLHttpRequest = new XMLHttpRequest;
|
|
|
|
|
|
|
|
xhr.addEventListener('readystatechange', () => {
|
2019-06-10 17:04:53 +00:00
|
|
|
if(xhr.readyState !== 4)
|
2018-12-30 22:07:32 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
updateCSRF(xhr.getResponseHeader('X-Misuzu-CSRF'));
|
|
|
|
|
|
|
|
let json: UserRelationInfo = JSON.parse(xhr.responseText) as UserRelationInfo,
|
|
|
|
message = json.error || json.message;
|
|
|
|
|
2019-06-10 17:04:53 +00:00
|
|
|
if(message && onFail)
|
2018-12-30 22:07:32 +00:00
|
|
|
onFail(message);
|
2019-06-10 17:04:53 +00:00
|
|
|
else if(!message && onSuccess)
|
2018-12-30 22:07:32 +00:00
|
|
|
onSuccess(json);
|
|
|
|
});
|
2019-02-28 23:06:28 +00:00
|
|
|
xhr.open('GET', urlFormat('user-relation-create', [{name: 'user', value: userId}, {name: 'type', value: relationType.toString()}]));
|
2018-12-30 22:07:32 +00:00
|
|
|
xhr.setRequestHeader('X-Misuzu-XHR', 'user_relation');
|
2019-08-14 18:06:57 +00:00
|
|
|
xhr.setRequestHeader('X-Misuzu-CSRF', getCSRFToken());
|
2018-12-30 22:07:32 +00:00
|
|
|
xhr.send();
|
|
|
|
}
|