90 lines
3.6 KiB
TypeScript
90 lines
3.6 KiB
TypeScript
import { useTranslation } from "react-i18next";
|
|
import { ActionFunction, LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { PlusIcon } from "~/components/ui/icons/vendor";
|
|
import { useNavigate, useOutlet } from "react-router";
|
|
import ButtonTertiary from "~/components/ui/buttons/ButtonTertiary";
|
|
import { EntityRelationshipWithDetails, getEntityRelationshipsWithCount, updateScopedEntityRelationship } from "~/utils/db/entities/entityRelationships.db.server";
|
|
import { getEntityBySlug } from "~/utils/db/entities/entities.db.server";
|
|
import EntityRelationshipsTable from "~/components/entities/relationships/EntityRelationshipsTable";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import SlideOverWideEmpty from "~/components/ui/slideOvers/SlideOverWideEmpty";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { validateCSRFToken } from "~/utils/session.server";
|
|
|
|
type LoaderData = {
|
|
items: (EntityRelationshipWithDetails & { _count: { rows: number } })[];
|
|
};
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
await verifyUserHasPermission(request, "admin.entities.view");
|
|
const entity = await getEntityBySlug({ tenantId: null, slug: params.entity ?? "" });
|
|
const items = await getEntityRelationshipsWithCount(entity.id);
|
|
const data: LoaderData = {
|
|
items,
|
|
};
|
|
return data;
|
|
};
|
|
|
|
export const action: ActionFunction = async ({ request, params }) => {
|
|
await verifyUserHasPermission(request, "admin.entities.update");
|
|
const { t } = await getTranslations(request);
|
|
const entity = await getEntityBySlug({ tenantId: null, slug: params.entity ?? "" });
|
|
|
|
await validateCSRFToken(request);
|
|
const form = await request.formData();
|
|
const action = form.get("action")?.toString() ?? "";
|
|
|
|
if (action === "set-orders") {
|
|
const items: { id: string; order: number }[] = form.getAll("orders[]").map((f: FormDataEntryValue) => {
|
|
return JSON.parse(f.toString());
|
|
});
|
|
const allowedRelationshipIds = new Set((await getEntityRelationshipsWithCount(entity.id)).map((relationship) => relationship.id));
|
|
if (items.some(({ id }) => !allowedRelationshipIds.has(id))) {
|
|
return Response.json({ error: t("shared.notFound") }, { status: 400 });
|
|
}
|
|
|
|
await Promise.all(
|
|
items.map(async ({ id, order }) => {
|
|
await updateScopedEntityRelationship({ id, entityId: entity.id }, { order: Number(order) });
|
|
})
|
|
);
|
|
return Response.json({ updated: true });
|
|
}
|
|
return Response.json({ error: t("shared.invalidForm") }, { status: 400 });
|
|
};
|
|
|
|
export default function EditEntityRelationshipsIndexRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const { t } = useTranslation();
|
|
const outlet = useOutlet();
|
|
const navigate = useNavigate();
|
|
|
|
return (
|
|
<>
|
|
<div className="space-y-3">
|
|
<h3 className="text-foreground text-sm font-medium leading-3">{t("models.relationship.plural")}</h3>
|
|
<EntityRelationshipsTable items={data.items} editable={true} />
|
|
<div className="w-fu flex justify-start">
|
|
<ButtonTertiary to="new">
|
|
<PlusIcon className="h-5 w-5" />
|
|
<span className="font-medium uppercase">{t("shared.add")}</span>
|
|
</ButtonTertiary>
|
|
</div>
|
|
</div>
|
|
|
|
<SlideOverWideEmpty
|
|
title={""}
|
|
open={!!outlet}
|
|
onClose={() => {
|
|
navigate(".", { replace: true });
|
|
}}
|
|
className="sm:max-w-md"
|
|
overflowYScroll={true}
|
|
>
|
|
<div className="-mx-1 -mt-3">
|
|
<div className="space-y-4">{outlet}</div>
|
|
</div>
|
|
</SlideOverWideEmpty>
|
|
</>
|
|
);
|
|
}
|