Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+117
@@ -0,0 +1,117 @@
|
||||
import { buildPortalProjectDetailVM } from "@hivegrid/view-models";
|
||||
import { useRef, useState } from "react";
|
||||
import { useFetcher, useLoaderData, useNavigate, useRevalidator, useRouteLoaderData, type LoaderFunctionArgs } from "react-router";
|
||||
|
||||
import { CustomerPortalProjectDetailPage } from "~/components/screens/customer-portal/CustomerPortalInterimPages";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { loadPortalProjectDataset, portalNotFound, requirePortalSessionContext } from "~/services/customer-portal/portal-data.server";
|
||||
import { loadPortalProjectInteractionsVM } from "~/services/customer-portal/portal-interactions-data.server";
|
||||
import type { PortalRootData } from "~/services/customer-portal/portal-root";
|
||||
import { portalPublicPath } from "~/services/customer-portal/portal-origin.server";
|
||||
import { portalUploadSelectionSignature, uploadPortalFileBatch, type PortalUploadActionResult } from "~/services/customer-portal/portal-upload-batch.client";
|
||||
|
||||
export async function loader({ request, params }: LoaderFunctionArgs) {
|
||||
if (!params.projectId) throw portalNotFound();
|
||||
const session = await requirePortalSessionContext(request, params.tenant);
|
||||
const { t } = await getTranslations(request);
|
||||
const { identity } = session;
|
||||
const baseHref = portalPublicPath(session.origin);
|
||||
const dataset = await loadPortalProjectDataset(identity, undefined, baseHref);
|
||||
const paths = {
|
||||
dashboard: baseHref,
|
||||
projects: portalPublicPath(session.origin, "projects"),
|
||||
upload: portalPublicPath(session.origin, "upload"),
|
||||
settings: portalPublicPath(session.origin, "settings"),
|
||||
logout: portalPublicPath(session.origin, "logout"),
|
||||
};
|
||||
const project = dataset.projects.find((candidate) => candidate.id === params.projectId) ?? null;
|
||||
const interactionsVM = project
|
||||
? await loadPortalProjectInteractionsVM({
|
||||
identity,
|
||||
projectId: project.id,
|
||||
projectLabel: project.name,
|
||||
pendingFilename: t("customerPortal.upload.pendingFilename"),
|
||||
uploadAction: portalPublicPath(session.origin, `projects/${project.id}/upload`),
|
||||
complaintAction: (formId) => portalPublicPath(session.origin, `projects/${project.id}/complaints/${formId}`),
|
||||
})
|
||||
: { status: "empty" as const };
|
||||
const data = {
|
||||
vm: buildPortalProjectDetailVM({
|
||||
frozen: identity.tenantFrozen,
|
||||
project,
|
||||
projectsHref: paths.projects,
|
||||
locale: identity.viewerLocale,
|
||||
}),
|
||||
portalName: identity.portalTitle,
|
||||
viewerLabel: viewerLabel(identity),
|
||||
brandingLogoHref: portalPublicPath(session.origin, "branding/logo"),
|
||||
tenantSlug: identity.tenantSlug,
|
||||
interactionsVM,
|
||||
paths,
|
||||
};
|
||||
return Response.json(data, { status: project ? 200 : 404, headers: session.responseHeaders });
|
||||
}
|
||||
|
||||
function viewerLabel(identity: { viewerFirstName: string; viewerLastName: string; portalTitle: string }): string {
|
||||
return `${identity.viewerFirstName} ${identity.viewerLastName}`.trim() || identity.portalTitle;
|
||||
}
|
||||
|
||||
export default function CustomerPortalProjectDetailRoute() {
|
||||
const data = useLoaderData<typeof loader>();
|
||||
const root = useRouteLoaderData<PortalRootData>("root");
|
||||
const navigate = useNavigate();
|
||||
const revalidator = useRevalidator();
|
||||
const logout = useFetcher();
|
||||
const [uploadState, setUploadState] = useState<PortalUploadActionResult & { readonly busy: boolean }>({ ok: false, busy: false });
|
||||
const pendingUpload = useRef<{ signature: string; batchId: string; running: boolean } | null>(null);
|
||||
const complaint = useFetcher<{ ok: boolean; messageKey?: string; errorKey?: string }>();
|
||||
return (
|
||||
<CustomerPortalProjectDetailPage
|
||||
vm={data.vm}
|
||||
interactionsVM={data.interactionsVM}
|
||||
interactionsState={{
|
||||
uploadBusy: uploadState.busy,
|
||||
complaintBusy: complaint.state !== "idle",
|
||||
uploadMessageKey: uploadState.ok ? uploadState.messageKey : null,
|
||||
uploadErrorKey: !uploadState.busy && !uploadState.ok ? uploadState.errorKey : null,
|
||||
complaintMessageKey: complaint.data?.ok ? complaint.data.messageKey : null,
|
||||
complaintErrorKey: complaint.data?.ok === false ? complaint.data.errorKey : null,
|
||||
}}
|
||||
portalName={data.portalName}
|
||||
viewerLabel={data.viewerLabel}
|
||||
brandingLogoHref={data.brandingLogoHref}
|
||||
actions={{
|
||||
onDashboard: () => navigate(data.paths.dashboard),
|
||||
onProjects: () => navigate(data.paths.projects),
|
||||
onUpload: () => navigate(data.paths.upload),
|
||||
onSettings: () => navigate(data.paths.settings),
|
||||
onBack: () => navigate(data.paths.projects),
|
||||
onDownloadDocument: (href) => window.location.assign(href),
|
||||
onUploadFiles: async (action, files) => {
|
||||
const signature = portalUploadSelectionSignature(files);
|
||||
if (pendingUpload.current?.running) return;
|
||||
const batchId = pendingUpload.current?.signature === signature ? pendingUpload.current.batchId : crypto.randomUUID();
|
||||
pendingUpload.current = { signature, batchId, running: true };
|
||||
setUploadState({ ok: false, busy: true });
|
||||
const result = await uploadPortalFileBatch({ action, files, csrfToken: root?.csrf, batchId });
|
||||
setUploadState({ ...result, busy: false });
|
||||
pendingUpload.current = result.ok ? null : { signature, batchId, running: false };
|
||||
if (result.ok) void revalidator.revalidate();
|
||||
},
|
||||
onSubmitComplaint: (action, values, gdprAccepted) => {
|
||||
const form = new FormData();
|
||||
if (root?.csrf) form.set("csrf", root.csrf);
|
||||
form.set("idempotencyKey", crypto.randomUUID());
|
||||
form.set("gdprAccepted", String(gdprAccepted));
|
||||
form.set("payload", JSON.stringify(values));
|
||||
void complaint.submit(form, { method: "post", action });
|
||||
},
|
||||
onLogout: () => {
|
||||
const form = new FormData();
|
||||
if (root?.csrf) form.set("csrf", root.csrf);
|
||||
void logout.submit(form, { method: "post", action: data.paths.logout });
|
||||
},
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user