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-button").onclick = this.save; document.getElementById("delete-button").onclick = this.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 `