frontend-zackeneule/source/pages/register/logic.ts

224 lines
6.5 KiB
TypeScript
Raw Normal View History

2024-05-20 22:04:23 +02:00
/*
Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Frontend
Copyright (C) 2024 Christian Fraß
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see
<https://www.gnu.org/licenses/>.
*/
2024-04-22 10:02:43 +02:00
lib_plankton.zoo_page.register(
"register",
async (parameters, target_element) => {
2024-04-30 01:32:40 +02:00
function set_state(
state : ("load" | "fill" | "wait" | "done"),
2024-04-30 14:05:40 +02:00
messages : Array<string> = []
2024-04-30 01:32:40 +02:00
) : void
{
2024-04-22 10:02:43 +02:00
target_element.querySelector(".register").setAttribute("rel", state);
2024-04-30 14:05:40 +02:00
target_element.querySelector(".register-message").textContent = "";
let dom_list = document.createElement("ul");
messages.forEach(
message => {
let dom_message = document.createElement("li");
dom_message.textContent = message;
dom_list.appendChild(dom_message);
}
);
target_element.querySelector(".register-message").appendChild(dom_list);
2024-04-30 01:32:40 +02:00
}
2024-04-22 10:02:43 +02:00
const id : int = parseInt(parameters["id"]);
const verification : string = parameters["verification"];
2024-04-23 17:44:55 +02:00
// const verification : string = (new URLSearchParams(location.search)).get("verification");
2024-04-22 10:02:43 +02:00
update_nav({"mode": null});
2024-04-30 01:32:40 +02:00
target_element.appendChild(template_request("register"));
set_state(
"load",
[
]
);
2024-04-29 00:15:48 +02:00
2024-05-01 09:53:59 +02:00
let member_data : (
null
|
{
name_real_value : string;
name_real_index : int;
name_login : string;
email_address_veiled : string;
email_address_nominal : string;
}
);
try {
member_data = await _espe.backend.member_info(id, verification);
}
2024-05-01 09:53:59 +02:00
catch (error) {
member_data = null;
2024-04-30 01:32:40 +02:00
}
2024-05-01 09:53:59 +02:00
if (member_data === null) {
set_state(
"fill",
[
lib_plankton.translate.get("page.register.flaw.already_registered"),
]
);
}
else {
// title
2024-04-30 01:32:40 +02:00
{
2024-05-01 09:53:59 +02:00
target_element.querySelector(".register-title").textContent = lib_plankton.translate.get("page.register.title");
2024-04-30 01:32:40 +02:00
}
2024-05-01 09:53:59 +02:00
// info
2024-04-30 01:32:40 +02:00
{
2024-05-01 09:53:59 +02:00
}
// form
{
let input : lib_plankton.zoo_input.interface_input<
{
email_address : ("none" | "only_veiled" | "both");
email_redirect : boolean;
password_value : string;
password_confirmation : string;
}
>;
// input
{
input = new lib_plankton.zoo_input.class_input_group(
[
{
"name": "email_address",
"input": new lib_plankton.zoo_input.class_input_enumeration(
[
{
"value": "none",
"label": lib_plankton.translate.get("page.register.form.field.email_address.option.none"),
},
{
"value": "only_veiled",
"label": lib_plankton.translate.get("page.register.form.field.email_address.option.only_veiled"),
},
{
"value": "both",
"label": lib_plankton.translate.get("page.register.form.field.email_address.option.both"),
},
]
),
"label": lib_plankton.translate.get("page.register.form.field.email_address.label"),
"help": lib_plankton.translate.get(
"page.register.form.field.email_address.help",
2024-04-30 01:32:40 +02:00
{
2024-05-01 09:53:59 +02:00
"email_address_veiled": member_data.email_address_veiled,
"email_address_nominal": member_data.email_address_nominal,
2024-04-30 01:32:40 +02:00
}
2024-05-01 09:53:59 +02:00
),
},
{
"name": "email_redirect",
"input": new lib_plankton.zoo_input.class_input_checkbox(
),
"label": lib_plankton.translate.get("page.register.form.field.email_redirect.label"),
},
{
"name": "password_value",
"input": new lib_plankton.zoo_input.class_input_password(
),
"label": lib_plankton.translate.get("page.register.form.field.password_value.label"),
"help": lib_plankton.translate.get("page.register.form.field.password_value.help"),
},
{
"name": "password_confirmation",
"input": new lib_plankton.zoo_input.class_input_password(
),
"label": lib_plankton.translate.get("page.register.form.field.password_confirmation.label"),
},
]
);
await input.setup(target_element.querySelector(".register-form-input") as HTMLElement);
await input.write(
{
"email_address": "both",
"email_redirect": true,
"password_value": "",
"password_confirmation": "",
}
);
}
// actions
{
target_element.querySelector(".register-form-action-send").textContent = lib_plankton.translate.get("page.register.form.submit");
target_element.querySelector(".register-form-action-send").addEventListener(
"click",
async (event) => {
const value : {
email_address : ("none" | "only_veiled" | "both");
email_redirect : boolean;
password_value : string;
password_confirmation : string;
} = await input.read();
let flaws : Array<{incident : string; details : Record<string, any>;}>;
2024-05-20 14:07:25 +02:00
set_state(
"wait",
[
]
);
2024-05-01 09:53:59 +02:00
if (value.password_value !== value.password_confirmation) {
2024-04-30 14:05:40 +02:00
flaws = [
2024-05-01 09:53:59 +02:00
{"incident": "password_mismatch", "details": {}},
2024-04-30 14:05:40 +02:00
];
}
2024-05-01 09:53:59 +02:00
else {
try {
flaws = await _espe.backend.member_register(
id,
verification,
{
"email_use_veiled_address": (value.email_address !== "none"),
"email_use_nominal_address": (value.email_address === "both"),
"email_redirect_to_private_address": value.email_redirect,
"password": value.password_value,
}
);
}
catch (error) {
flaws = [
{"incident": "unhandled_error", "details": {}},
];
}
}
if (flaws.length > 0) {
set_state(
"fill",
flaws.map(
flaw => lib_plankton.string.coin(
lib_plankton.translate.get("page.register.flaw." + flaw.incident),
flaw.details
)
2024-04-30 14:05:40 +02:00
)
2024-05-01 09:53:59 +02:00
);
}
else {
set_state(
"done",
[
lib_plankton.translate.get("page.register.success")
]
);
}
2024-04-30 01:32:40 +02:00
}
2024-05-01 09:53:59 +02:00
);
}
2024-04-22 10:02:43 +02:00
}
2024-04-25 23:30:16 +02:00
}
2024-04-22 10:02:43 +02:00
},
);