68 lines
2.7 KiB
TypeScript
68 lines
2.7 KiB
TypeScript
import { ActionFunction } from "react-router";
|
|
import { Form, useActionData } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import ButtonPrimary from "~/components/ui/buttons/ButtonPrimary";
|
|
import IndexPageLayout from "~/components/ui/layouts/IndexPageLayout";
|
|
import SettingSection from "~/components/ui/sections/SettingSection";
|
|
import { deleteAllFeatureFlags } from "~/modules/featureFlags/db/featureFlags.db.server";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { validateCSRFToken } from "~/utils/session.server";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import { useSaveToast } from "~/hooks/use-save-toast";
|
|
|
|
type ActionData = {
|
|
intent: "delete-all";
|
|
ok?: true;
|
|
messageKey?: string;
|
|
errorKey?: string;
|
|
};
|
|
export const action: ActionFunction = async ({ request }) => {
|
|
await verifyUserHasPermission(request, "admin.featureFlags.manage");
|
|
await validateCSRFToken(request);
|
|
const form = await request.formData();
|
|
const action = form.get("action");
|
|
if (action === "delete-all") {
|
|
try {
|
|
await deleteAllFeatureFlags();
|
|
return Response.json({ intent: "delete-all", ok: true, messageKey: "featureFlags.danger.toast.reset" } satisfies ActionData);
|
|
} catch {
|
|
return Response.json({ intent: "delete-all", errorKey: "featureFlags.danger.errors.resetFailed" } satisfies ActionData, { status: 400 });
|
|
}
|
|
} else {
|
|
return Response.json({ intent: "delete-all", errorKey: "shared.invalidForm" } satisfies ActionData, { status: 400 });
|
|
}
|
|
};
|
|
|
|
export default function AdminAnalyticsOverviewRoute() {
|
|
const { t } = useTranslation();
|
|
const actionData = useActionData<ActionData>();
|
|
const rootData = useRootData();
|
|
|
|
useSaveToast(actionData);
|
|
return (
|
|
<>
|
|
<IndexPageLayout>
|
|
<div className="space-y-6 p-4 sm:px-6 lg:col-span-9 lg:px-0">
|
|
<SettingSection title={t("featureFlags.danger.title")} description={t("featureFlags.danger.description")}>
|
|
<div>
|
|
<div className="text-muted-foreground mt-2 max-w-xl text-sm leading-5">
|
|
<p>{t("featureFlags.danger.reset.description")}</p>
|
|
</div>
|
|
<div className="mt-5">
|
|
<Form method="post">
|
|
{rootData.csrf && <input hidden readOnly name="csrf" value={rootData.csrf} />}
|
|
<input hidden readOnly name="action" value="delete-all" />
|
|
<ButtonPrimary destructive type="submit">
|
|
{t("featureFlags.danger.reset.title")}
|
|
</ButtonPrimary>
|
|
|
|
</Form>
|
|
</div>
|
|
</div>
|
|
</SettingSection>
|
|
</div>
|
|
</IndexPageLayout>
|
|
</>
|
|
);
|
|
}
|