frontend-zackeneule/source/pages/member_view/logic.ts
2025-08-21 21:45:39 +00:00

176 lines
5.3 KiB
TypeScript

/*
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/>.
*/
lib_plankton.zoo_page.register(
"member_view",
async (parameters, target_element) => {
const id : int = parseInt(parameters["id"]);
let dom_fragment : DocumentFragment = template_request("member_view");
dom_fragment.querySelector(".member_view-title").textContent = lib_plankton.translate.get("page.member_view.title");
/**
* @todo cache
*/
const groups_as_array : Array<{id : int; name : string;}> = await _espe.backend.group_list();
const groups_as_map : Map<int, {name : string;}> = new Map<int, {name : string;}>();
for (const group_thingy of groups_as_array)
{
groups_as_map.set(group_thingy.id, {"name": group_thingy.name});
}
const member_data = await _espe.backend.member_get(id);
const form = new lib_plankton.zoo_form.class_form<
{
name : string;
groups : Array<int>;
enabled : boolean;
email_address : (null | string);
password_set : boolean;
},
{
name : string;
groups : Set<string>;
enabled : boolean;
email_address : (null | string);
password_set : boolean;
}
>(
value => ({
"name": value.name,
"groups": new Set<string>(value.groups.map(group_id => group_id.toFixed(0))),
"enabled": value.enabled,
"email_address": value.email_address,
"password_set": value.password_set,
}),
representation => ({
"name": representation.name,
"groups": (() => {
const array : Array<int> = [];
for (const group_id_encoded of representation.groups)
{
array.push(parseInt(group_id_encoded));
}
return array;
}) (),
"enabled": representation.enabled,
"email_address": representation.email_address,
"password_set": representation.password_set,
}),
new lib_plankton.zoo_input.class_input_group(
[
{
"name": "name",
"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
"label": lib_plankton.translate.get("domain.member.name.label"),
},
{
"name": "groups",
/*
"input": new lib_plankton.zoo_input.class_input_list(
() => new lib_plankton.zoo_input.class_input_number()
),
*/
"input": new _espe.class_input_set(
groups_as_array.map(
group_thingy => group_thingy.id.toFixed(0)
),
group_id_encoded => groups_as_map.get(parseInt(group_id_encoded)).name
),
"label": lib_plankton.translate.get("domain.member.groups.label"),
},
{
"name": "enabled",
"input": new lib_plankton.zoo_input.class_input_checkbox(),
"label": lib_plankton.translate.get("domain.member.enabled.label"),
},
{
"name": "password_set",
"input": new lib_plankton.zoo_input.class_input_checkbox({"read_only": true}),
"label": lib_plankton.translate.get("domain.member.password_set.label"),
},
{
"name": "email_address",
"input": new lib_plankton.zoo_input.class_input_text(),
"label": lib_plankton.translate.get("domain.member.email_address.label"),
},
]
),
(
[
{
"label": lib_plankton.translate.get("page.member_view.form.action.save"),
"procedure": async (get_value, get_representation) => {
const value = await get_value();
await _espe.backend.member_modify(
id,
{
"email_address": value.email_address,
"groups": value.groups,
"enabled": value.enabled,
}
);
},
},
]
/*
.concat(
member_data.registered
? []
: [
{
"label": lib_plankton.translate.get("page.member_view.form.action.summon"),
"procedure": async (get_value, get_representation) => {
const url : string = await _espe.backend.member_summon(
id,
lib_plankton.zoo_page.encode(
{
"name": "member_register",
"parameters": {
"id": id,
"verification": "{{verification}}",
}
}
)
);
if (_espe.conf.get().settings.test_mode) {
alert(lib_plankton.translate.get("page.member_view.misc.test_info", {"url": url}));
}
else {
alert(lib_plankton.translate.get("page.member_view.misc.summoned"));
}
},
},
]
)
*/
)
);
await form.setup(dom_fragment.querySelector(".member_view-form") as HTMLElement);
await form.input_write(
{
"name": member_data.name,
"groups": member_data.groups,
"enabled": member_data.enabled,
"email_address": member_data.email_address,
"password_set": member_data.password_set,
}
);
target_element.appendChild(dom_fragment);
},
);