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,163 @@
|
||||
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 UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
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 { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { validateProperty } from "~/utils/helpers/PropertyHelper";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type LoaderData = {
|
||||
entity: EntityWithDetails;
|
||||
item: PropertyWithDetails;
|
||||
formulas: FormulaDto[];
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.entities.view");
|
||||
const entity = await getEntityBySlug({ tenantId: null, slug: params.entity ?? "" });
|
||||
const propertyScope = { id: params.id ?? "", tenantId: null, entityId: entity.id };
|
||||
const item = await getScopedProperty(propertyScope);
|
||||
if (!item) {
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}/properties`));
|
||||
}
|
||||
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 verifyUserHasPermission(request, "admin.entities.update");
|
||||
const { t } = await getTranslations(request);
|
||||
|
||||
const entity = await getEntityBySlug({ tenantId: null, slug: params.entity ?? "" });
|
||||
|
||||
const propertyScope = { id: params.id ?? "", tenantId: null, entityId: entity.id };
|
||||
const existing = await getScopedProperty(propertyScope);
|
||||
if (!existing) {
|
||||
return badRequest({ error: t("shared.notFound") });
|
||||
}
|
||||
|
||||
await validateCSRFToken(request);
|
||||
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") {
|
||||
try {
|
||||
if (name?.includes(" ")) {
|
||||
throw Error(t("models.property.errors.nameNoSpaces"));
|
||||
}
|
||||
if (name?.includes("-")) {
|
||||
throw Error(t("models.property.errors.nameNoHyphen"));
|
||||
}
|
||||
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}/properties`));
|
||||
} catch (e: any) {
|
||||
return badRequest({ error: e.message });
|
||||
}
|
||||
} else if (action === "delete") {
|
||||
await verifyUserHasPermission(request, "admin.entities.delete");
|
||||
const id = form.get("id")?.toString() ?? "";
|
||||
const deleteScope = { id, tenantId: null, entityId: entity.id };
|
||||
const existingProperty = await getScopedProperty(deleteScope);
|
||||
if (!existingProperty) {
|
||||
return badRequest({ error: t("shared.notFound") });
|
||||
}
|
||||
await deleteScopedProperty(deleteScope);
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}/properties`));
|
||||
}
|
||||
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}
|
||||
formulaListPath={adminPath("entities/formulas", { mode: "host-aware" })}
|
||||
formulaDetailsPath={(formulaId) => adminPath(`entities/formulas/${formulaId}`, { mode: "host-aware" })}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
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 { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
import { useAppOrAdminData } from "~/utils/data/useAppOrAdminData";
|
||||
import { PropertyWithDetails, getEntityBySlug, EntityWithDetails } from "~/utils/db/entities/entities.db.server";
|
||||
import { validateProperty } from "~/utils/helpers/PropertyHelper";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type LoaderData = {
|
||||
entity: EntityWithDetails;
|
||||
properties: PropertyWithDetails[];
|
||||
formulas: FormulaDto[];
|
||||
};
|
||||
export const loader = async ({ params }: LoaderFunctionArgs) => {
|
||||
const entity = await getEntityBySlug({ tenantId: null, 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 }) => {
|
||||
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() ?? "";
|
||||
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: null,
|
||||
});
|
||||
return redirect(UrlUtils.getModulePath(params, `entities/${params.entity}/properties`));
|
||||
} catch (e: any) {
|
||||
return badRequest({ error: e.message });
|
||||
}
|
||||
}
|
||||
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}
|
||||
formulaListPath={adminPath("entities/formulas", { mode: "host-aware" })}
|
||||
formulaDetailsPath={(formulaId) => adminPath(`entities/formulas/${formulaId}`, { mode: "host-aware" })}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user