Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/routes/admin/entities/$entity/templates.tsx
T

94 lines
3.2 KiB
TypeScript

import { useTranslation } from "react-i18next";
import { LoaderFunctionArgs, useLoaderData } from "react-router";
import { Outlet } from "react-router";
import { PlusIcon } from "~/components/ui/icons/vendor";
import ButtonTertiary from "~/components/ui/buttons/ButtonTertiary";
import TableSimple from "~/components/ui/tables/TableSimple";
import { EntityWithDetails, getEntityBySlug } from "~/utils/db/entities/entities.db.server";
import { EntityTemplate } from "@toolctrl/db/prisma-browser";
import { getEntityTemplates } from "~/utils/db/entities/entityTemplates.db.server";
import { getTenantIdOrNull } from "~/utils/services/.server/urlService";
import ShowPayloadModalButton from "~/components/ui/json/ShowPayloadModalButton";
import { PropertyType } from "~/application/enums/entities/PropertyType";
type LoaderData = {
entity: EntityWithDetails;
items: EntityTemplate[];
};
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const tenantId = await getTenantIdOrNull({ request, params });
const entity = await getEntityBySlug({ tenantId, slug: params.entity ?? "" });
const items = await getEntityTemplates(entity.id, { tenantId });
const data: LoaderData = {
entity,
items,
};
return data;
};
export default function EntityTemplatesIndex() {
const { t } = useTranslation();
const data = useLoaderData<LoaderData>();
function getConfig(item: EntityTemplate) {
try {
const config = JSON.parse(item.config);
const values: string[] = [];
data.entity.properties
.filter((f) => !f.isDefault)
.sort((a, b) => a.order - b.order)
.forEach((property) => {
let value = config[property.name];
if (value) {
if (property.type === PropertyType.SELECT) {
const option = property.options.find((f) => f.value === value);
if (option) {
value = option.name || option.value;
}
}
values.push(t(property.title) + ": " + value);
}
});
return values.join(", ");
} catch (e) {
return "";
}
}
return (
<>
<div className="space-y-3">
<h3 className="text-foreground text-sm font-medium leading-3">{t("models.entityTemplate.plural")}</h3>
<TableSimple
headers={[
{
name: "title",
title: t("models.entityTemplate.title"),
value: (item) => item.title,
},
{
title: t("models.entityTemplate.object"),
name: "template",
className: "max-w-xs",
value: (item) => <ShowPayloadModalButton title={t("models.entityTemplate.templateWithTitle", { title: item.title })} payload={getConfig(item)} />,
},
]}
items={data.items}
actions={[
{
title: t("shared.edit"),
onClickRoute: (idx, item) => item.id,
},
]}
></TableSimple>
<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>
<Outlet />
</>
);
}