131 lines
4.3 KiB
JavaScript
131 lines
4.3 KiB
JavaScript
import { client, navigateTo } from "../index.js";
|
|
import AbstractAuthentificateView from "./AbstractAuthentifiedView.js";
|
|
|
|
export default class extends AbstractAuthentificateView
|
|
{
|
|
constructor(params)
|
|
{
|
|
super(params, "Me");
|
|
}
|
|
|
|
async postInit()
|
|
{
|
|
if (this.fill() === null)
|
|
return;
|
|
document.getElementById("save-account-button").onclick = this.acccount_save;
|
|
document.getElementById("delete-account-button").onclick = this.account_delete_accounts;
|
|
}
|
|
|
|
async fill()
|
|
{
|
|
let data = await client.account.get();
|
|
|
|
if (data === null)
|
|
{
|
|
navigateTo("/login")
|
|
return;
|
|
}
|
|
document.getElementById("username").value = data.username;
|
|
}
|
|
|
|
async delete_accounts()
|
|
{
|
|
let current_password = document.getElementById("current_password").value;
|
|
|
|
let response_data = await client.account.delete(current_password);
|
|
|
|
if (response_data === null)
|
|
{
|
|
navigateTo("/login");
|
|
return;
|
|
}
|
|
|
|
["delete", "current_password"].forEach(error_field => {
|
|
let error_display = document.getElementById(`error_${error_field}`);
|
|
if (error_display != null)
|
|
error_display.innerHTML = "";
|
|
});
|
|
|
|
if (response_data === "user deleted")
|
|
{
|
|
document.getElementById(`error_delete`).innerHTML = "OK";
|
|
navigateTo("/login")
|
|
return;
|
|
}
|
|
|
|
document.getElementById("error_current_password").innerHTML = response_data["password"]
|
|
}
|
|
|
|
async save()
|
|
{
|
|
let username = document.getElementById("username").value;
|
|
let new_password = document.getElementById("new_password").value;
|
|
let current_password = document.getElementById("current_password").value;
|
|
|
|
let data = {};
|
|
|
|
data.username = username;
|
|
if (new_password.length != 0)
|
|
data.new_password = new_password;
|
|
let response_data = await client.account.update(data, current_password);
|
|
|
|
if (response_data === null)
|
|
{
|
|
navigateTo("/login");
|
|
return;
|
|
}
|
|
else if (response_data === "data has been alterate")
|
|
{
|
|
navigateTo("/me");
|
|
return;
|
|
}
|
|
|
|
["username", "new_password", "current_password"].forEach(error_field => {
|
|
let error_display = document.getElementById(`error_${error_field}`);
|
|
if (error_display != null)
|
|
error_display.innerHTML = "";
|
|
});
|
|
|
|
Object.keys(response_data).forEach(error_field => {
|
|
let error_display = document.getElementById(`error_${error_field}`);
|
|
if (error_display != null)
|
|
error_display.innerHTML = response_data[error_field];
|
|
});
|
|
let avatar = document.getElementById("avatar");
|
|
|
|
if (avatar.files[0] !== undefined)
|
|
{
|
|
let form_data = new FormData();
|
|
form_data.append("file", avatar.files[0]);
|
|
await client.me.change_avatar(form_data)
|
|
}
|
|
}
|
|
|
|
async getHtml()
|
|
{
|
|
return `
|
|
<link rel="stylesheet" href="static/css/me.css">
|
|
<h1>ME</h1>
|
|
<div class="account">
|
|
<h3>Account</h3>
|
|
<input type="text" placeholder="username" id="username">
|
|
<span id="error_username"></span>
|
|
<input type=password placeholder="new password" id="new_password">
|
|
<span id="error_new_password"></span>
|
|
<input type=password placeholder="current password" id="current_password">
|
|
<span id="error_current_password"></span>
|
|
<input type="button" value="Save Credentials" id="save-account-button">
|
|
<span id="error_save"></span>
|
|
<input type="button" value="Delete Account" id="delete-account-button">
|
|
<span id="error_delete"></span>
|
|
</div>
|
|
<div class="profile">
|
|
<h3>Profile</h3>
|
|
<input type="file" id="avatar" accept="image/png, image/jpeg">
|
|
<input type="button" value="Save profile" id="save-profile-button">
|
|
<span id="error_save"></span>
|
|
</div>
|
|
<a href="/logout" class="nav__link" data-link>Logout</a>
|
|
`;
|
|
}
|
|
} |