42_ft_transcendence/frontend/static/js/views/ProfilePageView.js

136 lines
3.2 KiB
JavaScript

import AbstractView from "./abstracts/AbstractView.js";
import { client } from "../index.js"
export default class extends AbstractView {
constructor(params) {
super(params, "Profile ");
this.id = Number(params.id);
}
async postInit()
{
this.profile = await client.profiles.getProfile(this.id);
if (this.profile === null)
return 404;
this.info = document.getElementById("info");
// Username
let username = document.createElement("a");
username.id = "username";
username.appendChild(document.createTextNode(this.profile.username));
this.info.appendChild(username);
this.info.appendChild(document.createElement("br"));
// Avatar
let avatar = document.createElement("img");
avatar.id = "avatar";
avatar.src = this.profile.avatar_url;
this.info.appendChild(avatar);
await this.blockButton();
await this.friendButton();
client.notice.rewrite_profile = async () => {
let result = await this.profile.getFriend();
await this.profile.getBlock()
await this.friendButton();
}
}
async blockButton() {
// Block option
if (await client.isAuthentificate() === false)
return;
if (client.me.id != this.id) {
let block = document.getElementById("block");
if (block == undefined) {
block = document.createElement("p");
this.info.appendChild(block);
}
block.id = "block";
block.onclick = async () => {
if (this.profile.isBlocked)
await client.profiles.deblock(this.id);
else
await client.profiles.block(this.id);
this.profile = await client.profiles.getProfile(this.id);
this.blockButton();
};
if (this.profile.isBlocked)
block.textContent = "Deblock";
else
block.textContent = "Block";
}
}
async friendButton() {
if (await client.isAuthentificate() === false)
return;
if (client.me.id != this.id) {
let yes = document.getElementById("yes") || document.createElement("p");
let no = document.getElementById("no") || document.createElement("p");
let friend = document.getElementById("friend") || document.createElement("p");
if (client.notice.data["asker"].includes(this.id)) {
if (friend)
friend.remove();
yes.id = "yes";
yes.textContent = "Accept Friend";
yes.onclick = async () => {
client.notice.accept_friend(this.id);
}
no.id = "no";
no.textContent = "Refuse Friend";
no.onclick = async () => {
client.notice.refuse_friend(this.id);
}
this.info.appendChild(yes);
this.info.appendChild(document.createTextNode(" "));
this.info.appendChild(no);
}
else {
if (yes && no)
yes.remove(); no.remove();
friend.id = "friend"
friend.onclick = async () => {
if (this.profile.isFriend)
await client.notice.remove_friend(this.id);
else
await client.notice.ask_friend(this.id);
this.profile = await client.profiles.getProfile(this.id);
this.friendButton();
};
if (this.profile.isFriend)
friend.textContent = "Remove Friend";
else {
friend.textContent = "Ask Friend";
}
this.info.appendChild(friend);
}
}
}
async getHtml() {
return `
<link rel="stylesheet" href="/static/css/profile.css">
<div id="info">
</div>
`;
}
}