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
+112
@@ -0,0 +1,112 @@
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { LoaderFunctionArgs, useLoaderData } from "react-router";
|
||||
import { Outlet, useParams } 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";
|
||||
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
|
||||
type LoaderData = {
|
||||
entity: EntityWithDetails;
|
||||
items: EntityTemplate[];
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
await requireAuth({ request, params });
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
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>();
|
||||
const params = useParams();
|
||||
|
||||
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 (
|
||||
<EditPageLayout
|
||||
title={t(data.entity.titlePlural) + " - " + t("models.entity.templates")}
|
||||
withHome={false}
|
||||
menu={[
|
||||
{
|
||||
title: t("models.entity.plural"),
|
||||
routePath: UrlUtils.getModulePath(params, `entities`),
|
||||
},
|
||||
{
|
||||
title: data.entity.titlePlural,
|
||||
routePath: UrlUtils.getModulePath(params, `entities/${data.entity.slug}/templates`),
|
||||
},
|
||||
]}
|
||||
>
|
||||
<div className="space-y-3">
|
||||
<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 />
|
||||
</EditPageLayout>
|
||||
);
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
import { EntityTemplate } from "@toolctrl/db/prisma-browser";
|
||||
import { ActionFunction, LoaderFunctionArgs, redirect, useLoaderData } from "react-router";
|
||||
import { useNavigate, useParams } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import EntityTemplateForm from "~/components/entities/entityTemplates/EntityTemplateForm";
|
||||
import SlideOverWideEmpty from "~/components/ui/slideOvers/SlideOverWideEmpty";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { EntityWithDetails, getEntityBySlug } from "~/utils/db/entities/entities.db.server";
|
||||
import { deleteScopedEntityTemplate, getEntityTemplate, updateScopedEntityTemplate } from "~/utils/db/entities/entityTemplates.db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getTenantIdOrNull } from "~/utils/services/.server/urlService";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type LoaderData = {
|
||||
entity: EntityWithDetails;
|
||||
item: EntityTemplate;
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
await requireAuth({ request, params });
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
const entity = await getEntityBySlug({ tenantId, slug: params.entity ?? "" });
|
||||
const templateScope = { id: params.id ?? "", tenantId, entityId: entity.id };
|
||||
const item = await getEntityTemplate(templateScope.id, { tenantId, entityId: entity.id });
|
||||
if (!item) {
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}/templates`));
|
||||
}
|
||||
const data: LoaderData = {
|
||||
entity,
|
||||
item,
|
||||
};
|
||||
return data;
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
error?: string;
|
||||
};
|
||||
const badRequest = (data: ActionData) => Response.json(data, { status: 400 });
|
||||
export const action: ActionFunction = async ({ request, params }) => {
|
||||
await requireAuth({ request, params });
|
||||
const { t } = await getTranslations(request);
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
await validateCSRFToken(request);
|
||||
|
||||
const entity = await getEntityBySlug({ tenantId, slug: params.entity ?? "" });
|
||||
const templateScope = { id: params.id ?? "", tenantId, entityId: entity.id };
|
||||
const existing = await getEntityTemplate(templateScope.id, { tenantId, entityId: entity.id });
|
||||
if (!existing) {
|
||||
return badRequest({ error: t("shared.notFound") });
|
||||
}
|
||||
|
||||
const form = await request.formData();
|
||||
const action = form.get("action")?.toString() ?? "";
|
||||
const title = form.get("title")?.toString() ?? "";
|
||||
const config = form.get("config")?.toString() ?? "";
|
||||
|
||||
if (action === "edit") {
|
||||
try {
|
||||
await updateScopedEntityTemplate(templateScope, {
|
||||
title,
|
||||
config,
|
||||
});
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}/templates`));
|
||||
} catch {
|
||||
return badRequest({ error: t("models.entityTemplate.errors.updateFailed") });
|
||||
}
|
||||
} else if (action === "delete") {
|
||||
await deleteScopedEntityTemplate(templateScope);
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}/templates`));
|
||||
}
|
||||
return badRequest({ error: t("shared.invalidForm") });
|
||||
};
|
||||
|
||||
export default function EditEntityTemplateRoute() {
|
||||
const { t } = useTranslation();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const params = useParams();
|
||||
const navigate = useNavigate();
|
||||
function close() {
|
||||
navigate(UrlUtils.getModulePath(params, `entities/${params.entity}/templates`));
|
||||
}
|
||||
return (
|
||||
<SlideOverWideEmpty title={t("models.entityTemplate.edit")} open={true} className="sm:max-w-lg" onClose={close}>
|
||||
<EntityTemplateForm entity={data.entity} item={data.item} />
|
||||
</SlideOverWideEmpty>
|
||||
);
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
import { ActionFunction, LoaderFunctionArgs, redirect, useLoaderData } from "react-router";
|
||||
import { useNavigate, useParams } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import EntityTemplateForm from "~/components/entities/entityTemplates/EntityTemplateForm";
|
||||
import SlideOverWideEmpty from "~/components/ui/slideOvers/SlideOverWideEmpty";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { EntityWithDetails, getEntityBySlug } from "~/utils/db/entities/entities.db.server";
|
||||
import { createEntityTemplate } from "~/utils/db/entities/entityTemplates.db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getTenantIdOrNull } from "~/utils/services/.server/urlService";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type LoaderData = {
|
||||
title: string;
|
||||
entity: EntityWithDetails;
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
await requireAuth({ request, params });
|
||||
const { t } = await getTranslations(request);
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
const entity = await getEntityBySlug({ tenantId, slug: params.entity ?? "" });
|
||||
const data: LoaderData = {
|
||||
title: t("models.entityTemplate.metaTitle", { appName: process.env.APP_NAME }),
|
||||
entity,
|
||||
};
|
||||
return data;
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
error?: string;
|
||||
};
|
||||
const badRequest = (data: ActionData) => Response.json(data, { status: 400 });
|
||||
export const action: ActionFunction = async ({ request, params }) => {
|
||||
await requireAuth({ request, params });
|
||||
const { t } = await getTranslations(request);
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
await validateCSRFToken(request);
|
||||
|
||||
const entity = await getEntityBySlug({ tenantId, slug: params.entity! });
|
||||
|
||||
const form = await request.formData();
|
||||
const action = form.get("action")?.toString() ?? "";
|
||||
const title = form.get("title")?.toString() ?? "";
|
||||
const config = form.get("config")?.toString() ?? "";
|
||||
|
||||
if (action === "create") {
|
||||
try {
|
||||
await createEntityTemplate({
|
||||
entityId: entity.id,
|
||||
tenantId,
|
||||
title,
|
||||
config,
|
||||
});
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}/templates`));
|
||||
} catch {
|
||||
return badRequest({ error: t("models.entityTemplate.errors.createFailed") });
|
||||
}
|
||||
}
|
||||
return badRequest({ error: t("shared.invalidForm") });
|
||||
};
|
||||
|
||||
export default function NewEntityTemplateRoute() {
|
||||
const { t } = useTranslation();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const params = useParams();
|
||||
const navigate = useNavigate();
|
||||
function close() {
|
||||
navigate(UrlUtils.getModulePath(params, `entities/${params.entity}/templates`));
|
||||
}
|
||||
return (
|
||||
<SlideOverWideEmpty title={t("models.entityTemplate.new")} open={true} className="sm:max-w-lg" onClose={close}>
|
||||
<EntityTemplateForm entity={data.entity} />
|
||||
</SlideOverWideEmpty>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
import { ActionFunction, LoaderFunctionArgs, useLoaderData, useNavigate, useOutlet } from "react-router";
|
||||
import { useParams } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import TenantPropertiesList from "~/components/entities/properties/TenantPropertiesList";
|
||||
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { PropertiesApi } from "~/utils/api/.server/PropertiesApi";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { useAppData } from "~/utils/data/useAppData";
|
||||
import { getAppConfiguration } from "~/utils/db/appConfiguration.db.server";
|
||||
import { PropertyWithDetails, getEntityBySlug, EntityWithDetails, getAllEntities } from "~/utils/db/entities/entities.db.server";
|
||||
import { deleteScopedProperty, getScopedProperty, updateScopedProperty, updateScopedPropertyOrder } from "~/utils/db/entities/properties.db.server";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getTenantIdOrNull } from "~/utils/services/.server/urlService";
|
||||
import SlideOverWideEmpty from "~/components/ui/slideOvers/SlideOverWideEmpty";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type LoaderData = {
|
||||
entity: EntityWithDetails;
|
||||
properties: PropertyWithDetails[];
|
||||
allEntities: EntityWithDetails[];
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
await requireAuth({ request, params });
|
||||
const appConfiguration = await getAppConfiguration({ request });
|
||||
if (!appConfiguration.app.features.tenantEntityCustomization) {
|
||||
throw Error(t("settings.entities.errors.tenantEntityCustomizationNotEnabled"));
|
||||
}
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
const entity = await getEntityBySlug({ tenantId, slug: params.entity ?? "" });
|
||||
const data: LoaderData = {
|
||||
entity,
|
||||
properties: entity.properties,
|
||||
allEntities: await getAllEntities({ tenantId }),
|
||||
};
|
||||
return success(data);
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
error?: string;
|
||||
properties?: PropertyWithDetails[];
|
||||
created?: boolean;
|
||||
updated?: boolean;
|
||||
deleted?: boolean;
|
||||
};
|
||||
const success = (data: ActionData) => Response.json(data, { status: 200 });
|
||||
const badRequest = (data: ActionData) => Response.json(data, { status: 400 });
|
||||
export const action: ActionFunction = async ({ request, params }) => {
|
||||
await requireAuth({ request, params });
|
||||
const { t } = await getTranslations(request);
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
await validateCSRFToken(request);
|
||||
|
||||
const entity = await getEntityBySlug({ tenantId, slug: params.entity ?? "" });
|
||||
|
||||
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 allowedPropertyIds = new Set(entity.properties.map((property) => property.id));
|
||||
if (items.some(({ id }) => !allowedPropertyIds.has(id))) {
|
||||
return badRequest({ error: t("shared.notFound") });
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
items.map(async ({ id, order }) => {
|
||||
await updateScopedPropertyOrder({ id, tenantId, entityId: entity.id }, Number(order));
|
||||
})
|
||||
);
|
||||
return Response.json({ updated: true });
|
||||
} else if (action === "delete") {
|
||||
const id = form.get("id")?.toString() ?? "";
|
||||
const propertyScope = { id, tenantId, entityId: entity.id };
|
||||
const existingProperty = await getScopedProperty(propertyScope);
|
||||
if (!existingProperty) {
|
||||
return badRequest({ error: t("shared.notFound") });
|
||||
}
|
||||
await deleteScopedProperty(propertyScope);
|
||||
return success({
|
||||
properties: (await getEntityBySlug({ tenantId, slug: params.entity ?? "" }))?.properties,
|
||||
deleted: true,
|
||||
});
|
||||
// return redirect(adminPath(`entities/${params.entity}/properties`, { mode: "host-aware" }));
|
||||
} else if (action === "toggle-display") {
|
||||
const id = form.get("id")?.toString() ?? "";
|
||||
const propertyScope = { id, tenantId, entityId: entity.id };
|
||||
const existingProperty = await getScopedProperty(propertyScope);
|
||||
if (!existingProperty) {
|
||||
return badRequest({ error: t("shared.notFound") });
|
||||
}
|
||||
await updateScopedProperty(propertyScope, {
|
||||
isDisplay: !existingProperty.isDisplay,
|
||||
});
|
||||
return Response.json({});
|
||||
// return redirect(adminPath(`entities/${params.entity}/properties`, { mode: "host-aware" }));
|
||||
} else if (action === "duplicate") {
|
||||
try {
|
||||
const propertyId = form.get("id")?.toString() ?? "";
|
||||
const existingProperty = await getScopedProperty({ id: propertyId, tenantId, entityId: entity.id });
|
||||
if (!existingProperty) {
|
||||
return badRequest({ error: t("shared.notFound") });
|
||||
}
|
||||
await PropertiesApi.duplicate({ entity, propertyId });
|
||||
return Response.json({ created: true });
|
||||
} catch {
|
||||
return badRequest({ error: t("models.property.errors.duplicateFailed") });
|
||||
}
|
||||
}
|
||||
return badRequest({ error: t("shared.invalidForm") });
|
||||
};
|
||||
|
||||
export default function () {
|
||||
const { t } = useTranslation();
|
||||
const appData = useAppData();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const navigate = useNavigate();
|
||||
const outlet = useOutlet();
|
||||
const params = useParams();
|
||||
|
||||
return (
|
||||
<EditPageLayout
|
||||
title={`${t(data.entity.title)} - ${t("models.property.plural")}`}
|
||||
withHome={false}
|
||||
menu={[
|
||||
{
|
||||
title: t("models.entity.plural"),
|
||||
routePath: UrlUtils.getModulePath(params, `entities`),
|
||||
},
|
||||
{
|
||||
title: data.entity.titlePlural,
|
||||
routePath: UrlUtils.getModulePath(params, `entities/${data.entity.slug}`),
|
||||
},
|
||||
]}
|
||||
>
|
||||
<TenantPropertiesList tenantId={appData.currentTenant.id} items={data.properties} />
|
||||
<SlideOverWideEmpty
|
||||
title={params.id ? t("shared.edit") : t("shared.new")}
|
||||
open={!!outlet}
|
||||
onClose={() => {
|
||||
navigate(".", { replace: true });
|
||||
}}
|
||||
className="sm:max-w-sm"
|
||||
overflowYScroll={true}
|
||||
>
|
||||
<div className="-mx-1 -mt-3">
|
||||
<div className="space-y-4">{outlet}</div>
|
||||
</div>
|
||||
</SlideOverWideEmpty>
|
||||
</EditPageLayout>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import { ActionFunction, LoaderFunctionArgs, redirect, useLoaderData } from "react-router";
|
||||
import { useNavigate, useParams } from "react-router";
|
||||
import { PropertyType } from "~/application/enums/entities/PropertyType";
|
||||
import { Colors } from "~/application/enums/shared/Colors";
|
||||
import PropertyForm from "~/components/entities/properties/PropertyForm";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getAllFormulas } from "~/modules/formulas/db/formulas.db.server";
|
||||
import { FormulaDto } from "~/modules/formulas/dtos/FormulaDto";
|
||||
import FormulaHelpers from "~/modules/formulas/utils/FormulaHelpers";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { useAppOrAdminData } from "~/utils/data/useAppOrAdminData";
|
||||
import { PropertyWithDetails, EntityWithDetails, getEntityBySlug } from "~/utils/db/entities/entities.db.server";
|
||||
import {
|
||||
deleteScopedProperty,
|
||||
getScopedProperty,
|
||||
updateScopedProperty,
|
||||
updateScopedPropertyAttributes,
|
||||
updateScopedPropertyOptions,
|
||||
} from "~/utils/db/entities/properties.db.server";
|
||||
import { validateProperty } from "~/utils/helpers/PropertyHelper";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getTenantIdOrNull } from "~/utils/services/.server/urlService";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type LoaderData = {
|
||||
entity: EntityWithDetails;
|
||||
item: PropertyWithDetails;
|
||||
formulas: FormulaDto[];
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
await requireAuth({ request, params });
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
const entity = await getEntityBySlug({ tenantId, slug: params.entity ?? "" });
|
||||
const propertyScope = { id: params.id ?? "", tenantId, entityId: entity.id };
|
||||
const item = await getScopedProperty(propertyScope);
|
||||
if (!item) {
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}`));
|
||||
}
|
||||
const data: LoaderData = {
|
||||
entity,
|
||||
item,
|
||||
formulas: (await getAllFormulas()).map((formula) => FormulaHelpers.getFormulaDto(formula)),
|
||||
};
|
||||
return data;
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
error?: string;
|
||||
};
|
||||
const badRequest = (data: ActionData) => Response.json(data, { status: 400 });
|
||||
export const action: ActionFunction = async ({ request, params }) => {
|
||||
await requireAuth({ request, params });
|
||||
const { t } = await getTranslations(request);
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
await validateCSRFToken(request);
|
||||
|
||||
const entity = await getEntityBySlug({ tenantId, slug: params.entity ?? "" });
|
||||
|
||||
const propertyScope = { id: params.id ?? "", tenantId, entityId: entity.id };
|
||||
const existing = await getScopedProperty(propertyScope);
|
||||
if (!existing) {
|
||||
return badRequest({ error: t("shared.notFound") });
|
||||
}
|
||||
|
||||
const form = await request.formData();
|
||||
const action = form.get("action")?.toString() ?? "";
|
||||
const name = form.get("name")?.toString() ?? "";
|
||||
const title = form.get("title")?.toString() ?? "";
|
||||
const type = Number(form.get("type")) as PropertyType;
|
||||
const subtype = form.get("subtype")?.toString() ?? null;
|
||||
const order = Number(form.get("order"));
|
||||
let isRequired = Boolean(form.get("is-required"));
|
||||
const isHidden = Boolean(form.get("is-hidden"));
|
||||
const isDisplay = Boolean(form.get("is-display"));
|
||||
const isReadOnly = Boolean(form.get("is-read-only"));
|
||||
const canUpdate = Boolean(form.get("can-update"));
|
||||
let showInCreate = Boolean(form.get("show-in-create"));
|
||||
let formulaId = form.get("formula-id")?.toString() ?? null;
|
||||
|
||||
if (["id", "folio", "createdAt", "createdByUser", "sort", "page", "q", "v", "redirect", "tags"].includes(name)) {
|
||||
return badRequest({ error: t("models.property.errors.reservedName", { name }) });
|
||||
}
|
||||
|
||||
const options: { order: number; value: string; name?: string; color?: Colors }[] = form.getAll("options[]").map((f: FormDataEntryValue) => {
|
||||
return JSON.parse(f.toString());
|
||||
});
|
||||
|
||||
const attributes: { name: string; value: string }[] = form.getAll("attributes[]").map((f: FormDataEntryValue) => {
|
||||
return JSON.parse(f.toString());
|
||||
});
|
||||
|
||||
if ([PropertyType.SELECT, PropertyType.MULTI_SELECT].includes(type) && options.length === 0) {
|
||||
// return badRequest({ error: "Add at least one option" });
|
||||
}
|
||||
|
||||
if (type !== PropertyType.FORMULA) {
|
||||
formulaId = null;
|
||||
} else {
|
||||
isRequired = false;
|
||||
}
|
||||
if ([PropertyType.FORMULA].includes(type) && !formulaId) {
|
||||
return badRequest({ error: t("models.property.errors.selectFormula") });
|
||||
}
|
||||
|
||||
const errors = await validateProperty(name, title, entity.properties, existing, t);
|
||||
if (errors.length > 0) {
|
||||
return badRequest({ error: errors.join(", ") });
|
||||
}
|
||||
|
||||
if (action === "edit") {
|
||||
if (name?.includes(" ")) {
|
||||
return badRequest({ error: t("models.property.errors.nameNoSpaces") });
|
||||
}
|
||||
if (name?.includes("-")) {
|
||||
return badRequest({ error: t("models.property.errors.nameNoHyphen") });
|
||||
}
|
||||
try {
|
||||
await updateScopedProperty(propertyScope, {
|
||||
name,
|
||||
title,
|
||||
type,
|
||||
subtype,
|
||||
order,
|
||||
isDefault: existing?.isDefault ?? false,
|
||||
isRequired,
|
||||
isHidden,
|
||||
isDisplay,
|
||||
isReadOnly,
|
||||
canUpdate,
|
||||
showInCreate,
|
||||
formulaId,
|
||||
});
|
||||
await updateScopedPropertyOptions(propertyScope, options);
|
||||
await updateScopedPropertyAttributes(propertyScope, attributes);
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}`));
|
||||
} catch {
|
||||
return badRequest({ error: t("models.property.errors.updateFailed") });
|
||||
}
|
||||
} else if (action === "delete") {
|
||||
await deleteScopedProperty(propertyScope);
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}`));
|
||||
}
|
||||
return badRequest({ error: t("shared.invalidForm") });
|
||||
};
|
||||
|
||||
export default function EditEntityPropertyRoute() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const appOrAdminData = useAppOrAdminData();
|
||||
return <PropertyForm item={data.item} properties={[]} entities={appOrAdminData.entities} formulas={data.formulas} />;
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import { ActionFunction, LoaderFunctionArgs, redirect, useLoaderData } from "react-router";
|
||||
import { PropertyType } from "~/application/enums/entities/PropertyType";
|
||||
import { Colors } from "~/application/enums/shared/Colors";
|
||||
import PropertyForm from "~/components/entities/properties/PropertyForm";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getAllFormulas } from "~/modules/formulas/db/formulas.db.server";
|
||||
import { FormulaDto } from "~/modules/formulas/dtos/FormulaDto";
|
||||
import FormulaHelpers from "~/modules/formulas/utils/FormulaHelpers";
|
||||
import { PropertiesApi } from "~/utils/api/.server/PropertiesApi";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { useAppOrAdminData } from "~/utils/data/useAppOrAdminData";
|
||||
import { PropertyWithDetails, getEntityBySlug, EntityWithDetails } from "~/utils/db/entities/entities.db.server";
|
||||
import { validateProperty } from "~/utils/helpers/PropertyHelper";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getTenantIdOrNull } from "~/utils/services/.server/urlService";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type LoaderData = {
|
||||
entity: EntityWithDetails;
|
||||
properties: PropertyWithDetails[];
|
||||
formulas: FormulaDto[];
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
await requireAuth({ request, params });
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
const entity = await getEntityBySlug({ tenantId, slug: params.entity ?? "" });
|
||||
const data: LoaderData = {
|
||||
entity,
|
||||
properties: entity?.properties ?? [],
|
||||
formulas: (await getAllFormulas()).map((formula) => FormulaHelpers.getFormulaDto(formula)),
|
||||
};
|
||||
return success(data);
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
error?: string;
|
||||
properties?: PropertyWithDetails[];
|
||||
};
|
||||
const success = (data: ActionData) => Response.json(data, { status: 200 });
|
||||
const badRequest = (data: ActionData) => Response.json(data, { status: 400 });
|
||||
export const action: ActionFunction = async ({ request, params }) => {
|
||||
await requireAuth({ request, params });
|
||||
const { t } = await getTranslations(request);
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
await validateCSRFToken(request);
|
||||
|
||||
const entity = await getEntityBySlug({ tenantId, slug: params.entity ?? "" });
|
||||
|
||||
const form = await request.formData();
|
||||
const action = form.get("action")?.toString() ?? "";
|
||||
const name = form.get("name")?.toString() ?? "";
|
||||
const title = form.get("title")?.toString() ?? "";
|
||||
const type = Number(form.get("type")) as PropertyType;
|
||||
const subtype = form.get("subtype")?.toString() ?? null;
|
||||
const order = Number(form.get("order"));
|
||||
const isDefault = Boolean(form.get("is-default"));
|
||||
let isRequired = Boolean(form.get("is-required"));
|
||||
const isHidden = Boolean(form.get("is-hidden"));
|
||||
const isDisplay = Boolean(form.get("is-display"));
|
||||
const isReadOnly = Boolean(form.get("is-read-only"));
|
||||
const canUpdate = Boolean(form.get("can-update"));
|
||||
let showInCreate = Boolean(form.get("show-in-create"));
|
||||
let formulaId = form.get("formula-id")?.toString() ?? null;
|
||||
|
||||
if (["id", "folio", "createdAt", "createdByUser", "sort", "page", "q", "v", "redirect", "tags"].includes(name)) {
|
||||
return badRequest({ error: t("models.property.errors.reservedName", { name }) });
|
||||
}
|
||||
|
||||
const options: { order: number; value: string; name?: string; color?: Colors }[] = form.getAll("options[]").map((f: FormDataEntryValue) => {
|
||||
return JSON.parse(f.toString());
|
||||
});
|
||||
|
||||
const attributes: { name: string; value: string }[] = form.getAll("attributes[]").map((f: FormDataEntryValue) => {
|
||||
return JSON.parse(f.toString());
|
||||
});
|
||||
|
||||
if ([PropertyType.SELECT, PropertyType.MULTI_SELECT].includes(type) && options.length === 0) {
|
||||
// return badRequest({ error: "Add at least one option" });
|
||||
}
|
||||
|
||||
if (type !== PropertyType.FORMULA) {
|
||||
formulaId = null;
|
||||
} else {
|
||||
isRequired = false;
|
||||
showInCreate = false;
|
||||
}
|
||||
if ([PropertyType.FORMULA].includes(type) && !formulaId) {
|
||||
return badRequest({ error: t("models.property.errors.selectFormula") });
|
||||
}
|
||||
|
||||
const errors = await validateProperty(name, title, entity.properties, null, t);
|
||||
if (errors.length > 0) {
|
||||
return badRequest({ error: errors.join(", ") });
|
||||
}
|
||||
|
||||
if (action === "create") {
|
||||
try {
|
||||
await PropertiesApi.create({
|
||||
entityId: entity.id,
|
||||
name,
|
||||
title,
|
||||
type,
|
||||
subtype,
|
||||
order,
|
||||
isDefault,
|
||||
isRequired,
|
||||
isHidden,
|
||||
isDisplay,
|
||||
isReadOnly,
|
||||
canUpdate,
|
||||
showInCreate,
|
||||
formulaId,
|
||||
options,
|
||||
attributes,
|
||||
tenantId,
|
||||
});
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}`));
|
||||
} catch {
|
||||
return badRequest({ error: t("models.property.errors.createFailed") });
|
||||
}
|
||||
}
|
||||
return badRequest({ error: t("shared.invalidForm") });
|
||||
};
|
||||
|
||||
export default function NewEntityPropertyRoute() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const appOrAdminData = useAppOrAdminData();
|
||||
return <PropertyForm properties={data.properties} entities={appOrAdminData.entities} formulas={data.formulas} />;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import { LoaderFunctionArgs, useLoaderData } from "react-router";
|
||||
import { Link } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { DefaultEntityTypes } from "~/application/dtos/shared/DefaultEntityTypes";
|
||||
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
|
||||
import TableSimple from "~/components/ui/tables/TableSimple";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getAppConfiguration } from "~/utils/db/appConfiguration.db.server";
|
||||
import { EntityWithDetails, getAllEntities } from "~/utils/db/entities/entities.db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getTenantIdOrNull } from "~/utils/services/.server/urlService";
|
||||
|
||||
type LoaderData = {
|
||||
allEntities: EntityWithDetails[];
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
await requireAuth({ request, params });
|
||||
const appConfiguration = await getAppConfiguration({ request });
|
||||
if (!appConfiguration.app.features.tenantEntityCustomization) {
|
||||
throw Error(t("settings.entities.errors.tenantEntityCustomizationNotEnabled"));
|
||||
}
|
||||
const tenantId = await getTenantIdOrNull({ request, params });
|
||||
await verifyUserHasPermission(request, "app.settings.account.update", tenantId);
|
||||
const allEntities = await getAllEntities({ tenantId, active: true, types: [DefaultEntityTypes.All, DefaultEntityTypes.AppOnly] });
|
||||
const data: LoaderData = {
|
||||
allEntities,
|
||||
};
|
||||
return data;
|
||||
};
|
||||
export default function () {
|
||||
const { t } = useTranslation();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
return (
|
||||
<EditPageLayout title={t("models.entity.plural")}>
|
||||
<div className="space-y-2">
|
||||
<TableSimple
|
||||
items={data.allEntities}
|
||||
actions={[
|
||||
{
|
||||
title: t("models.entity.templates"),
|
||||
onClickRoute: (_, item) => item.slug + "/templates",
|
||||
},
|
||||
{
|
||||
title: t("models.property.plural"),
|
||||
onClickRoute: (_, item) => item.slug,
|
||||
},
|
||||
]}
|
||||
headers={[
|
||||
{
|
||||
name: "title",
|
||||
title: t("models.entity.title"),
|
||||
value: (item) => (
|
||||
<div>
|
||||
<div className="flex items-center space-x-1">
|
||||
<Link to={item.slug} className="font-medium hover:underline">
|
||||
{t(item.titlePlural)}
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
name: "properties",
|
||||
title: t("models.property.plural"),
|
||||
className: "w-full text-xs",
|
||||
value: (item) => (
|
||||
<div className="max-w-xs truncate">
|
||||
{item.properties.filter((f) => !f.isDefault).length > 0 ? (
|
||||
<Link className="truncate pb-1 hover:underline" to={item.slug}>
|
||||
{item.properties
|
||||
.filter((f) => !f.isDefault)
|
||||
.map((f) => t(f.title) + (f.isRequired ? "*" : ""))
|
||||
.join(", ")}
|
||||
</Link>
|
||||
) : (
|
||||
<Link className="text-muted-foreground truncate pb-1 hover:underline" to={item.slug}>
|
||||
{t("shared.setCustomProperties")}
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</div>
|
||||
</EditPageLayout>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user