ft_transcendence/frontend/static/js/views/MeView.js

106 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2023-11-30 19:29:56 -05:00
import { client, navigateTo } from "../index.js";
2023-12-16 12:00:38 -05:00
import { clear, fill_errors } from "../utils/formUtils.js";
import AbstractAuthentificateView from "./abstracts/AbstractAuthentifiedView.js";
2023-11-30 19:29:56 -05:00
export default class extends AbstractAuthentificateView
{
constructor(params)
{
super(params, "Me");
}
async postInit()
{
2023-12-16 12:00:38 -05:00
document.getElementById("save-account-button").onclick = this.save_account;
document.getElementById("delete-account-button").onclick = this.delete_account;
document.getElementById("save-profile-button").onclick = this.save_profile;
2023-11-30 19:29:56 -05:00
}
2023-12-16 12:00:38 -05:00
async delete_account()
2023-11-30 19:29:56 -05:00
{
2023-12-16 12:00:38 -05:00
let current_password = document.getElementById("current_password-input").value;
2023-11-30 19:29:56 -05:00
let response_data = await client.account.delete(current_password);
2023-12-16 12:00:38 -05:00
console.log(await client.isAuthentificate())
if (response_data === null || response_data === "user deleted")
2023-11-30 19:29:56 -05:00
{
navigateTo("/login");
return;
}
2023-12-16 12:00:38 -05:00
clear("innerHTML", ["current_password-input"])
fill_errors({"current_password-input": response_data["password"]}, "innerHTML")
2023-11-30 19:29:56 -05:00
}
2023-12-16 12:00:38 -05:00
async save_account()
2023-11-30 19:29:56 -05:00
{
2023-12-16 12:00:38 -05:00
let username = document.getElementById("username-input").value;
let new_password = document.getElementById("new_password-input").value;
let current_password = document.getElementById("current_password-input").value;
2023-11-30 19:29:56 -05:00
let data = {};
data.username = username;
if (new_password.length != 0)
data.new_password = new_password;
2023-12-16 12:00:38 -05:00
2023-11-30 19:29:56 -05:00
let response_data = await client.account.update(data, current_password);
2023-12-04 06:32:15 -05:00
2023-11-30 19:29:56 -05:00
if (response_data === null)
{
2023-12-04 06:32:15 -05:00
navigateTo("/login");
2023-11-30 19:29:56 -05:00
return;
}
2023-12-16 12:00:38 -05:00
if (response_data === "data has been alterate")
response_data = {"save-account": "saved"}
clear("innerHTML", ["username", "new_password", "current_password", "save-account", "delete-account"])
fill_errors(response_data, "innerHTML")
}
2023-11-30 19:29:56 -05:00
2023-12-16 12:00:38 -05:00
async save_profile()
{
let avatar = document.getElementById("avatar-input");
2023-12-06 09:19:41 -05:00
if (avatar.files[0] !== undefined)
{
let form_data = new FormData();
form_data.append("file", avatar.files[0]);
2023-12-16 12:00:38 -05:00
await client.me.change_avatar(form_data);
2023-12-06 09:19:41 -05:00
}
2023-12-16 12:00:38 -05:00
document.getElementById("save-profile").innerHTML = "Saved";
2023-11-30 19:29:56 -05:00
}
2023-12-16 12:00:38 -05:00
2023-11-30 19:29:56 -05:00
async getHtml()
{
return `
2023-12-16 12:00:38 -05:00
<link rel="stylesheet" href="/static/css/me.css">
2023-11-30 19:29:56 -05:00
<h1>ME</h1>
2023-12-16 12:00:38 -05:00
<div id="main">
<div class="account">
<h3>Account</h3>
<input type="text" placeholder="username" id="username-input" text=${client.me.username}>
<span id="username"></span>
<input type=password placeholder="new_password" id="new_password-input">
<span id="new_password"></span>
<input type=password placeholder="current_password" id="current_password-input">
<span id="current_password"></span>
<input type="button" value="Save Credentials" id="save-account-button">
<span id="save-account"></span>
<input type="button" value="Delete Account" id="delete-account-button">
<span id="delete-account"></span>
</div>
<div class="profile">
<h3>Profile</h3>
<input type="file" id="avatar-input" accept="image/png, image/jpeg">
<input type="button" value="Save profile" id="save-profile-button">
<span id="save-profile"></span>
</div>
<a href="/logout" class="nav__link" data-link>Logout</a>
2023-12-06 09:19:41 -05:00
</div>
2023-11-30 19:29:56 -05:00
`;
}
}