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
+57
-29
@@ -1,56 +1,84 @@
|
||||
import { type ActionFunctionArgs, type LoaderFunctionArgs, Form, redirect, useActionData, useLoaderData } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { type ActionFunctionArgs, type LoaderFunctionArgs, redirect, useActionData, useLoaderData, useNavigation, useSubmit } from "react-router";
|
||||
import { buildLeadSourceAdminVM, type LeadSourceAdminVM } from "@hivegrid/view-models";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { badRequestResponse, leadActionErrorResponse, loadLeadContext, requireLeadPermission } from "~/utils/integrations/leads/leadAccess.server";
|
||||
import { auditLeadEvent } from "~/utils/integrations/leads/leadAudit.server";
|
||||
import { leadSourcesAdminPath, LEADS_MODULE_KEY } from "~/utils/integrations/leads/leadRoutePaths";
|
||||
import { listLeadSources, upsertLeadSource, type LeadSourceRow } from "~/utils/integrations/leads/leadService.server";
|
||||
import { archiveLeadSource, listAssignableUsers, listLeadSources, upsertLeadSource } from "~/utils/integrations/leads/leadService.server";
|
||||
import { LeadSourcesAdminPage } from "~/components/screens/leads/LeadSourcesAdminPage";
|
||||
import ServerError from "~/components/ui/errors/ServerError";
|
||||
import { useSaveToast } from "~/hooks/use-save-toast";
|
||||
|
||||
type LoaderData = { sources: LeadSourceRow[] };
|
||||
type LoaderData = { title: string; vm: LeadSourceAdminVM };
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
const ctx = await loadLeadContext({ request, params, requireModule: true });
|
||||
requireLeadPermission(ctx, "leads.admin");
|
||||
return Response.json({ sources: await listLeadSources(ctx.tenantId) } satisfies LoaderData);
|
||||
const [sources, assignableUsers] = await Promise.all([listLeadSources(ctx.tenantId), listAssignableUsers(ctx.tenantId)]);
|
||||
const title = `${t("leads.sources.title")} | ${process.env.APP_NAME}`;
|
||||
return Response.json({ title, vm: buildLeadSourceAdminVM({ rows: sources, assignableUsers, canWrite: true }) } satisfies LoaderData);
|
||||
};
|
||||
|
||||
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const ctx = await loadLeadContext({ request, params, requireModule: true });
|
||||
requireLeadPermission(ctx, "leads.admin");
|
||||
const form = await request.formData();
|
||||
if (form.get("intent")?.toString() !== "upsert") throw badRequestResponse();
|
||||
const intent = form.get("intent")?.toString();
|
||||
try {
|
||||
const source = await upsertLeadSource({
|
||||
tenantId: ctx.tenantId,
|
||||
key: form.get("key")?.toString() ?? "",
|
||||
name: form.get("name")?.toString() ?? "",
|
||||
displayLabel: form.get("displayLabel")?.toString(),
|
||||
defaultZustaendigerUserId: form.get("defaultZustaendigerUserId")?.toString() || null,
|
||||
});
|
||||
await auditLeadEvent({ ctx, action: "lead.source_created", targetType: "lead_source", targetId: source.id });
|
||||
return redirect(leadSourcesAdminPath(params.tenant ?? ""));
|
||||
if (intent === "upsert") {
|
||||
const source = await upsertLeadSource({
|
||||
tenantId: ctx.tenantId,
|
||||
key: form.get("key")?.toString() ?? "",
|
||||
name: form.get("name")?.toString() ?? "",
|
||||
defaultZustaendigerUserId: form.get("defaultZustaendigerUserId")?.toString() || null,
|
||||
});
|
||||
await auditLeadEvent({ ctx, action: "lead.source_created", targetType: "lead_source", targetId: source.id });
|
||||
return redirect(`${leadSourcesAdminPath(params.tenant ?? "")}?toast=${encodeURIComponent("leads.toast.sourceSaved")}`);
|
||||
}
|
||||
// "Delete" in the UI, archive in the data: leads captured through the
|
||||
// source keep pointing at a row that still exists.
|
||||
if (intent === "archive") {
|
||||
const sourceId = form.get("sourceId")?.toString();
|
||||
if (!sourceId) throw badRequestResponse();
|
||||
await archiveLeadSource({ tenantId: ctx.tenantId, sourceId });
|
||||
await auditLeadEvent({ ctx, action: "lead.source_archived", targetType: "lead_source", targetId: sourceId });
|
||||
return redirect(`${leadSourcesAdminPath(params.tenant ?? "")}?toast=${encodeURIComponent("leads.toast.sourceArchived")}`);
|
||||
}
|
||||
throw badRequestResponse();
|
||||
} catch (error) {
|
||||
return leadActionErrorResponse(error);
|
||||
}
|
||||
};
|
||||
|
||||
export default function LeadSourcesRoute() {
|
||||
const { t } = useTranslation();
|
||||
const { sources } = useLoaderData<LoaderData>();
|
||||
const { vm } = useLoaderData<LoaderData>();
|
||||
const actionData = useActionData<{ error?: string }>();
|
||||
useSaveToast(actionData);
|
||||
const submit = useSubmit();
|
||||
const navigation = useNavigation();
|
||||
return (
|
||||
<main className="mx-auto max-w-4xl px-4 py-10" data-ui-id="ui.leads.sources">
|
||||
<h1 className="text-foreground text-xl font-semibold">{t("leads.sources.title")}</h1>
|
||||
{actionData?.error ? <p className="mt-4 text-sm text-red-600">{t(actionData.error)}</p> : null}
|
||||
<Form method="post" className="mt-6 grid gap-3 rounded-md border p-4">
|
||||
<input type="hidden" name="intent" value="upsert" />
|
||||
<label className="grid gap-1 text-sm">{t("leads.sources.field.key")}<input name="key" className="rounded border px-3 py-2" /></label>
|
||||
<label className="grid gap-1 text-sm">{t("leads.sources.field.name")}<input name="name" className="rounded border px-3 py-2" /></label>
|
||||
<label className="grid gap-1 text-sm">{t("leads.sources.field.displayLabel")}<input name="displayLabel" className="rounded border px-3 py-2" /></label>
|
||||
<button className="bg-foreground text-background rounded px-4 py-2 text-sm font-medium">{t("leads.sources.actions.save")}</button>
|
||||
</Form>
|
||||
<ul className="mt-6 grid gap-2 text-sm">{sources.map((source) => <li key={source.id} className="rounded border p-3">{source.displayLabel}</li>)}</ul>
|
||||
</main>
|
||||
<LeadSourcesAdminPage
|
||||
vm={vm}
|
||||
submitting={navigation.state !== "idle"}
|
||||
errorKey={actionData?.error ?? null}
|
||||
actions={{
|
||||
onUpsert: ({ key, name, defaultZustaendigerUserId }) => {
|
||||
const form = new FormData();
|
||||
form.set("intent", "upsert");
|
||||
form.set("key", key);
|
||||
form.set("name", name);
|
||||
form.set("defaultZustaendigerUserId", defaultZustaendigerUserId);
|
||||
submit(form, { method: "post" });
|
||||
},
|
||||
onDelete: (sourceId) => {
|
||||
const form = new FormData();
|
||||
form.set("intent", "archive");
|
||||
form.set("sourceId", sourceId);
|
||||
submit(form, { method: "post" });
|
||||
},
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user