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
@@ -0,0 +1,92 @@
|
||||
import { adminLeadManualFormSchema } from "@hivegrid/view-models";
|
||||
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
||||
import { redirect, useActionData, useLoaderData, useNavigate, useNavigation, useSubmit } from "react-router";
|
||||
|
||||
import { AdminPlatformLeadNewPage } from "~/components/screens/admin/leads/AdminPlatformLeadNewPage";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { listPlatformLeadAssignees, requirePlatformLeadWrite, runAsPlatformLeadOperator } from "~/services/admin/platform-leads.server";
|
||||
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type ActionData = { readonly error: string };
|
||||
|
||||
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
||||
const data = await runAsPlatformLeadOperator(request, async ({ access, service }) => {
|
||||
requirePlatformLeadWrite(access);
|
||||
const [sources, assignees] = await Promise.all([service.listSources(), listPlatformLeadAssignees()]);
|
||||
return {
|
||||
sourceOptions: sources.filter((source) => source.enabled).map((source) => ({ id: source.id, label: source.displayName })),
|
||||
assigneeOptions: assignees,
|
||||
};
|
||||
});
|
||||
const session = await getUserInfo(request);
|
||||
return { ...data, csrf: session.csrf ?? "" };
|
||||
};
|
||||
|
||||
export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
await validateCSRFToken(request);
|
||||
const form = await request.formData();
|
||||
const parsed = adminLeadManualFormSchema.safeParse({
|
||||
sourceId: form.get("sourceId"),
|
||||
firstName: form.get("firstName") ?? "",
|
||||
lastName: form.get("lastName") ?? "",
|
||||
email: form.get("email") ?? "",
|
||||
phone: form.get("phone") ?? "",
|
||||
company: form.get("company") ?? "",
|
||||
message: form.get("message") ?? "",
|
||||
sourceDetail: form.get("sourceDetail") ?? "",
|
||||
assignedToAdminUserId:
|
||||
form.get("assignedToAdminUserId") && form.get("assignedToAdminUserId") !== "unassigned" ? form.get("assignedToAdminUserId") : undefined,
|
||||
});
|
||||
if (!parsed.success) {
|
||||
const messageKey = parsed.error.issues.find((issue) => issue.message.startsWith("admin."))?.message;
|
||||
return Response.json({ error: t(messageKey ?? "admin.platformLeads.errors.invalidForm") } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const created = await runAsPlatformLeadOperator(request, async ({ access, service }) => {
|
||||
requirePlatformLeadWrite(access);
|
||||
return service.createManual({
|
||||
channel: "manual",
|
||||
sourceId: parsed.data.sourceId,
|
||||
assignedToAdminUserId: parsed.data.assignedToAdminUserId ?? null,
|
||||
payload: {
|
||||
firstName: parsed.data.firstName,
|
||||
lastName: parsed.data.lastName,
|
||||
email: parsed.data.email,
|
||||
phone: parsed.data.phone,
|
||||
company: parsed.data.company,
|
||||
message: parsed.data.message,
|
||||
sourceDetail: parsed.data.sourceDetail,
|
||||
},
|
||||
...access.audit("Platform lead created manually"),
|
||||
});
|
||||
});
|
||||
return redirect(`${adminPath(`leads/${created.lead.id}`, { mode: "host-aware" })}?feedback=created`);
|
||||
} catch (error) {
|
||||
if (error instanceof Response) throw error;
|
||||
return Response.json({ error: t("admin.platformLeads.errors.actionFailed") } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
};
|
||||
|
||||
export default function AdminPlatformLeadNewRoute(): React.JSX.Element {
|
||||
const data = useLoaderData<typeof loader>();
|
||||
const actionData = useActionData<ActionData>();
|
||||
const navigate = useNavigate();
|
||||
const navigation = useNavigation();
|
||||
const submit = useSubmit();
|
||||
return (
|
||||
<AdminPlatformLeadNewPage
|
||||
sourceOptions={data.sourceOptions}
|
||||
assigneeOptions={data.assigneeOptions}
|
||||
csrf={data.csrf}
|
||||
submitting={navigation.state === "submitting"}
|
||||
error={actionData?.error}
|
||||
actions={{
|
||||
onBack: () => void navigate(adminPath("leads", { mode: "host-aware" })),
|
||||
onCreate: (input) => void submit(input, { method: "post" }),
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user