Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,102 @@
|
||||
import { ActionFunctionArgs, redirect, useActionData } from "react-router";
|
||||
import { useSubmit } from "react-router";
|
||||
import ServerError from "~/components/ui/errors/ServerError";
|
||||
import ActionResultModal from "~/components/ui/modals/ActionResultModal";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import KnowledgeBaseForm from "~/modules/knowledgeBase/components/bases/KnowledgeBaseForm";
|
||||
import { createKnowledgeBase, getKnowledgeBaseBySlug } from "~/modules/knowledgeBase/db/knowledgeBase.db.server";
|
||||
import { KbNavLinkDto } from "~/modules/knowledgeBase/dtos/KbNavLinkDto";
|
||||
import { getKnowledgeBaseActionErrorKey } from "~/modules/knowledgeBase/service/KnowledgeBaseService.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
import { appendCSRFToken } from "~/utils/csrf";
|
||||
|
||||
type ActionData = {
|
||||
error?: string;
|
||||
success?: string;
|
||||
};
|
||||
export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.kb.view");
|
||||
await validateCSRFToken(request);
|
||||
const { t } = await getTranslations(request);
|
||||
const form = await request.formData();
|
||||
const action = form.get("action")?.toString();
|
||||
if (action === "new") {
|
||||
await verifyUserHasPermission(request, "admin.kb.create");
|
||||
let basePath = form.get("basePath")?.toString() ?? "";
|
||||
let slug = form.get("slug")?.toString() ?? "";
|
||||
const title = form.get("title")?.toString() ?? "";
|
||||
const description = form.get("description")?.toString() ?? "";
|
||||
const defaultLanguage = form.get("defaultLanguage")?.toString() ?? "";
|
||||
const layout = form.get("layout")?.toString() ?? "";
|
||||
const color = Number(form.get("color")?.toString() ?? "");
|
||||
const enabled = Boolean(form.get("enabled"));
|
||||
const languages = form.getAll("languages[]").map((l) => l.toString());
|
||||
const links: KbNavLinkDto[] = form.getAll("links[]").map((l) => JSON.parse(l.toString()));
|
||||
const logo = form.get("logo")?.toString() ?? "";
|
||||
const seoImage = form.get("seoImage")?.toString() ?? "";
|
||||
|
||||
if (languages.length === 0) {
|
||||
return Response.json({ error: t("knowledgeBase.admin.errors.atLeastOneLanguageRequired") }, { status: 400 });
|
||||
}
|
||||
const existing = await getKnowledgeBaseBySlug(slug);
|
||||
if (existing) {
|
||||
return Response.json({ error: t("knowledgeBase.admin.errors.slugAlreadyExists") }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!basePath.startsWith("/")) {
|
||||
basePath = "/" + basePath;
|
||||
}
|
||||
if (slug.startsWith("/")) {
|
||||
slug = slug.substring(1);
|
||||
}
|
||||
try {
|
||||
await createKnowledgeBase({
|
||||
basePath,
|
||||
slug,
|
||||
title,
|
||||
description,
|
||||
defaultLanguage,
|
||||
layout,
|
||||
color,
|
||||
enabled,
|
||||
languages: JSON.stringify(languages),
|
||||
links: JSON.stringify(links),
|
||||
logo,
|
||||
seoImage,
|
||||
});
|
||||
} catch (e: unknown) {
|
||||
return Response.json({ error: t(getKnowledgeBaseActionErrorKey(e)) }, { status: 400 });
|
||||
}
|
||||
|
||||
return redirect(adminPath("knowledge-base/bases", { mode: "host-aware" }));
|
||||
} else {
|
||||
return Response.json({ error: t("knowledgeBase.admin.errors.invalidForm") }, { status: 400 });
|
||||
}
|
||||
};
|
||||
|
||||
export default function () {
|
||||
const actionData = useActionData<ActionData>();
|
||||
const submit = useSubmit();
|
||||
|
||||
function onDelete() {
|
||||
const form = new FormData();
|
||||
appendCSRFToken(form);
|
||||
form.set("action", "delete");
|
||||
submit(form, {
|
||||
method: "post",
|
||||
});
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
<KnowledgeBaseForm onDelete={onDelete} />
|
||||
|
||||
<ActionResultModal actionData={actionData} showSuccess={false} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function ErrorBoundary() {
|
||||
return <ServerError />;
|
||||
}
|
||||
Reference in new issue
Block a user