73 lines
2.8 KiB
TypeScript
73 lines
2.8 KiB
TypeScript
import { useActionData, useLoaderData, useNavigation } from "react-router";
|
|
import { ActionFunction, LoaderFunctionArgs, redirect } from "react-router";
|
|
import { TenantUserType } from "~/application/enums/tenants/TenantUserType";
|
|
import { CreateWorkspacePage } from "~/components/screens/auth/CreateWorkspacePage";
|
|
import { getAllRoles } from "~/utils/db/permissions/roles.db.server";
|
|
import { createTenantUser, getMyTenants, TenantSimple } from "~/utils/db/tenants.db.server";
|
|
import { getUser } from "~/utils/db/users.db.server";
|
|
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
|
import { TenantsApi } from "~/utils/api/.server/TenantsApi";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import { withMetrics } from "~/lib/metrics/ssr";
|
|
import { appPath } from "~/utils/app/CanonicalUrlUtils";
|
|
|
|
type LoaderData = {
|
|
myTenants: TenantSimple[];
|
|
};
|
|
|
|
export const loader = withMetrics("new-account", async ({ request }: LoaderFunctionArgs) => {
|
|
const userInfo = await getUserInfo(request);
|
|
const user = await getUser(userInfo.userId);
|
|
if (!user) {
|
|
throw redirect(`/login`);
|
|
}
|
|
const myTenants = await getMyTenants(userInfo.userId);
|
|
const data: LoaderData = {
|
|
myTenants,
|
|
};
|
|
return data;
|
|
});
|
|
|
|
export function newAccountDashboardPath(tenantSlug: string) {
|
|
return appPath({ tenantSlug, path: "dashboard", mode: "host-aware" });
|
|
}
|
|
|
|
type ActionData = {
|
|
error?: string;
|
|
};
|
|
export const action: ActionFunction = async ({ request }) => {
|
|
try {
|
|
await validateCSRFToken(request);
|
|
const form = await request.formData();
|
|
const name = form.get("name")?.toString() ?? "";
|
|
const slug = form.get("slug")?.toString();
|
|
const { tenant, user } = await TenantsApi.create({ request, name, slug });
|
|
const roles = await getAllRoles("app");
|
|
await createTenantUser(
|
|
{
|
|
tenantId: tenant.id,
|
|
userId: user.id,
|
|
type: TenantUserType.OWNER,
|
|
},
|
|
roles
|
|
);
|
|
return redirect(newAccountDashboardPath(tenant.slug));
|
|
} catch (e: any) {
|
|
return Response.json({ error: e.message }, { status: 400 });
|
|
}
|
|
};
|
|
|
|
// Interim-UI (AD-28): renders the ~/ui preview screen behind the stable loader/action boundary;
|
|
// final Subframe optics land as a tracked design-nachzug once David's D-P2 delta ships.
|
|
export default function AppNewAccountRoute() {
|
|
const { myTenants } = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<ActionData>();
|
|
const navigation = useNavigation();
|
|
const { csrf } = useRootData();
|
|
// Back target: return to the tenant picker when the user already has workspaces, else login.
|
|
const backHref = myTenants.length > 0 ? "/select-tenant" : "/login";
|
|
return (
|
|
<CreateWorkspacePage csrf={csrf ?? ""} error={navigation.state === "idle" ? actionData?.error : undefined} backHref={backHref} />
|
|
);
|
|
}
|