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,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>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user