58 lines
2.6 KiB
TypeScript
58 lines
2.6 KiB
TypeScript
/**
|
|
* Tenant admin: form type selection (WF-P16 Form Builder, screen S01).
|
|
*
|
|
* Permissions: `forms.builder` — picking a type creates a draft form and
|
|
* redirects straight into the S02 editor (forms.$id.tsx), where name and
|
|
* fields are edited.
|
|
*/
|
|
import { type ActionFunctionArgs, type LoaderFunctionArgs, redirect, useActionData } from "react-router";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { validateCSRFToken } from "~/utils/session.server";
|
|
import { createDraftForm, isFormSubtypeChoice } from "~/modules/forms/services/createDraftForm.server";
|
|
import { loadFormsContext, requireFormsPermission } from "~/modules/forms/routes/formsAccess.server";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import { FormNewPage } from "~/components/screens/forms/FormNewPage";
|
|
import { v2MetaFunction } from "~/utils/compat/v2MetaFunction";
|
|
|
|
type ActionData = { error?: string };
|
|
|
|
type LoaderData = { title: string };
|
|
|
|
export const meta: v2MetaFunction<LoaderData> = ({ data }) => [{ title: data?.title }];
|
|
|
|
export async function loader({ request, params }: LoaderFunctionArgs): Promise<LoaderData> {
|
|
const ctx = await loadFormsContext({ request, params, requireModule: true });
|
|
requireFormsPermission(ctx, "forms.builder");
|
|
const { t } = await getTranslations(request);
|
|
return { title: `${t("settings.forms.admin.new.title")} | ${process.env.APP_NAME}` };
|
|
}
|
|
|
|
export async function action({ request, params }: ActionFunctionArgs): Promise<Response | ActionData> {
|
|
const ctx = await loadFormsContext({ request, params, requireModule: true });
|
|
requireFormsPermission(ctx, "forms.builder");
|
|
await validateCSRFToken(request);
|
|
const { t } = await getTranslations(request);
|
|
const body = await request.formData();
|
|
const subtypeRaw = body.get("subtype")?.toString() ?? "";
|
|
if (!isFormSubtypeChoice(subtypeRaw)) {
|
|
return { error: t("settings.forms.admin.errors.unknownOperation") };
|
|
}
|
|
const created = await createDraftForm({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
name: t(`settings.forms.admin.new.defaultName.${subtypeRaw}`),
|
|
subtype: subtypeRaw,
|
|
t,
|
|
});
|
|
// Host-aware: derive the editor path from the request URL instead of
|
|
// hardcoding an /app prefix (admin host serves tenant routes unprefixed).
|
|
const url = new URL(request.url);
|
|
return redirect(`${url.pathname.replace(/\/new$/, `/${created.id}`)}?toast=${encodeURIComponent("settings.forms.admin.messages.created")}`);
|
|
}
|
|
|
|
export default function FormsNew(): React.JSX.Element {
|
|
const actionData = useActionData() as ActionData | undefined;
|
|
const rootData = useRootData();
|
|
return <FormNewPage csrf={rootData.csrf ?? ""} actionError={actionData?.error} />;
|
|
}
|