150 lines
4.4 KiB
TypeScript
150 lines
4.4 KiB
TypeScript
import { createButton, createTag } from "@ui/ui_template_elements";
|
|
import {
|
|
processJoinRequest,
|
|
type CompanyInfo,
|
|
type DashboardUser,
|
|
type JoinRequest,
|
|
type Member,
|
|
} from "./B01_Dashboard_Api_Fetch";
|
|
import { canChangeRole, canDeleteUser } from "./B01_Dashboard_UI_Helper";
|
|
import {
|
|
openChangeRoleModal,
|
|
openCreateCompanyModal,
|
|
openDeleteUserModal,
|
|
openEditUserModal,
|
|
openFindCompanyModal,
|
|
} from "./B01_Dashboard_UI_Modals";
|
|
import type { DashboardState } from "./B01_Dashboard_UI_Page";
|
|
import { actionPair, table, text } from "@ui/ui_template_general_blocks";
|
|
import { formatDate, L, runRequest } from "./B01_Dashboard_UI_Common";
|
|
|
|
export function buildCompanyPanel(state: DashboardState): HTMLElement {
|
|
const wrap = document.createElement("div");
|
|
wrap.className = "b01-dashboard__actions";
|
|
if (!state.company) {
|
|
wrap.append(
|
|
createTag(L("B01_Dashboard_NoCompany"), "warning"),
|
|
createButton({
|
|
label: L("B01_Dashboard_CreateCompany"),
|
|
onClick: () => openCreateCompanyModal(),
|
|
}),
|
|
createButton({
|
|
label: L("B01_Dashboard_FindCompany"),
|
|
variant: "ghost",
|
|
onClick: () => openFindCompanyModal(),
|
|
}),
|
|
);
|
|
return wrap;
|
|
}
|
|
wrap.append(
|
|
createTag(`${state.company.name} (${state.user.status})`, "success"),
|
|
text(`${L("B01_Dashboard_Metric_ActiveUsers")}: ${state.company.user_count ?? 0}`),
|
|
text(`${L("B01_Dashboard_Projects")}: ${state.company.project_count ?? 0}`),
|
|
);
|
|
return wrap;
|
|
}
|
|
|
|
export function memberTable(members: Member[], currentUser: DashboardUser): HTMLElement {
|
|
return table(
|
|
[
|
|
L("B01_Dashboard_Table_Email"),
|
|
L("B01_Dashboard_Table_Name"),
|
|
L("B01_Dashboard_Table_Position"),
|
|
L("B01_Dashboard_Table_Department"),
|
|
L("B01_Dashboard_Table_Role"),
|
|
L("B01_Dashboard_Table_Action"),
|
|
],
|
|
members.map((member) => {
|
|
const actionsEl = document.createElement("div");
|
|
actionsEl.className = "b01-dashboard__actions";
|
|
|
|
actionsEl.append(
|
|
createButton({
|
|
label: L("Common_Btn_Edit"),
|
|
variant: "ghost",
|
|
onClick: () => openEditUserModal(currentUser, member),
|
|
}),
|
|
);
|
|
|
|
if (canChangeRole(currentUser, member)) {
|
|
actionsEl.append(
|
|
createButton({
|
|
label: L("B01_Dashboard_ChangeRole"),
|
|
variant: "ghost",
|
|
onClick: () => openChangeRoleModal(member),
|
|
}),
|
|
);
|
|
}
|
|
|
|
if (canDeleteUser(currentUser, member)) {
|
|
actionsEl.append(
|
|
createButton({
|
|
label: L("B01_Dashboard_RemoveMember"),
|
|
variant: "danger",
|
|
onClick: () => openDeleteUserModal(member),
|
|
}),
|
|
);
|
|
}
|
|
|
|
return [
|
|
text(member.email),
|
|
text(member.name),
|
|
text(member.position),
|
|
text(member.department),
|
|
text(member.role),
|
|
actionsEl,
|
|
];
|
|
}),
|
|
);
|
|
}
|
|
|
|
export function joinRequestTable(requests: JoinRequest[], systemMode: boolean): HTMLElement {
|
|
return table(
|
|
[
|
|
L("B01_Dashboard_Table_Email"),
|
|
...(systemMode ? [L("B01_Dashboard_Table_Company")] : []),
|
|
L("B01_Dashboard_Table_Requested"),
|
|
L("B01_Dashboard_Table_Status"),
|
|
L("B01_Dashboard_Table_Action"),
|
|
],
|
|
requests.map((request) => [
|
|
text(request.user_email),
|
|
...(systemMode ? [text(request.company_name)] : []),
|
|
text(formatDate(request.requested_at)),
|
|
text(request.status),
|
|
actionPair(
|
|
() => onB01_JoinRequest_Process_Click(request.id, "APPROVE", systemMode),
|
|
() => onB01_JoinRequest_Process_Click(request.id, "REJECT", systemMode),
|
|
),
|
|
]),
|
|
);
|
|
}
|
|
|
|
export function companyTable(companies: CompanyInfo[]): HTMLElement {
|
|
return table(
|
|
[
|
|
L("B01_Dashboard_Table_Company"),
|
|
L("B01_Dashboard_Field_BusinessNumber"),
|
|
L("B01_Dashboard_Table_Status"),
|
|
],
|
|
companies.map((company) => [
|
|
text(company.name),
|
|
text(company.business_registration_number),
|
|
text(company.business_status),
|
|
]),
|
|
);
|
|
}
|
|
|
|
export async function onB01_JoinRequest_Process_Click(
|
|
requestId: number,
|
|
action: "APPROVE" | "REJECT",
|
|
systemMode: boolean,
|
|
): Promise<void> {
|
|
await runRequest(() =>
|
|
systemMode && action === "APPROVE"
|
|
? import("./B01_Dashboard_Api_Fetch").then((api) => api.systemApproveJoinRequest(requestId))
|
|
: processJoinRequest(requestId, action),
|
|
);
|
|
window.dispatchEvent(new HashChangeEvent("hashchange"));
|
|
}
|