import { ActionFunction, LoaderFunctionArgs, MetaFunction, useLoaderData } from "react-router"; import { getTranslations } from "~/locale/i18next.server"; import { getTenantWithProfile, updateTenantTheme, TenantWithProfile } from "~/utils/db/tenants.db.server"; import { getTenantIdFromUrl } from "~/utils/services/.server/urlService"; import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService"; import EditPageLayout from "~/components/ui/layouts/EditPageLayout"; import UpdateTenantFormTheme from "~/components/core/tenants/forms/UpdateTenantFormTheme"; import { validateCSRFToken } from "~/utils/session.server"; type LoaderData = { title: string; tenant: TenantWithProfile; }; export const loader = async ({ request, params }: LoaderFunctionArgs) => { const tenantId = await getTenantIdFromUrl(params); await verifyUserHasPermission(request, "app.settings.account.view", tenantId); const { t } = await getTranslations(request); const tenant = await getTenantWithProfile(tenantId); if (!tenant) { throw Error(t("shared.notFound")); } const data: LoaderData = { title: t("settings.theme.metaTitle", { appName: process.env.APP_NAME }), tenant, }; return data; }; type ActionData = { error?: string; success?: string; }; const badRequest = (data: ActionData) => Response.json(data, { status: 400 }); export const action: ActionFunction = async ({ request, params }) => { const { t } = await getTranslations(request); const tenantId = await getTenantIdFromUrl(params); await verifyUserHasPermission(request, "app.settings.account.update", tenantId); await validateCSRFToken(request); const form = await request.formData(); const action = form.get("action")?.toString() ?? ""; if (action === "update-theme") { try { const theme = form.get("theme")?.toString() || null; const themeConfig = form.get("themeConfig")?.toString() || null; await updateTenantTheme(tenantId, { theme, themeConfig }); return Response.json({ success: t("shared.updated") }); } catch (error) { console.error(error); return badRequest({ error: t("shared.unexpectedError") }); } } return badRequest({ error: t("shared.invalidForm") }); }; export const meta: MetaFunction = ({ data }) => [{ title: data?.title }]; export default function ThemeSettingsRoute() { const data = useLoaderData(); return ( ); }