From d4a254f3e266b1a46cc61a534562cf64890c4606 Mon Sep 17 00:00:00 2001 From: Remko Date: Tue, 26 Mar 2024 12:31:42 +0100 Subject: [PATCH] added multiSelect load --- pwa/src/apiService/apiService.ts | 9 ++++++++- pwa/src/apiService/resources/object.ts | 2 +- pwa/src/hooks/object.ts | 12 +++++++++++- pwa/src/services/getIdFromObjectId.ts | 10 ++++++++++ .../schemasTemplate/SchemasTemplate.tsx | 2 +- .../EditObjectFormTemplate.tsx | 4 ++-- .../schemaForm/SchemaFormTemplate.tsx | 17 ++++++++++++++++- 7 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 pwa/src/services/getIdFromObjectId.ts diff --git a/pwa/src/apiService/apiService.ts b/pwa/src/apiService/apiService.ts index 3dde8d6b..0d2e871d 100644 --- a/pwa/src/apiService/apiService.ts +++ b/pwa/src/apiService/apiService.ts @@ -37,7 +37,7 @@ interface PromiseMessage { export type TSendFunction = ( instance: AxiosInstance, - action: "GET" | "POST" | "PUT" | "DELETE" | "DOWNLOAD", + action: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "DOWNLOAD", endpoint: string, payload?: JSON | FormData, promiseMessage?: PromiseMessage, @@ -273,6 +273,13 @@ export default class APIService { error: (err) => promiseMessage?.error ?? err.message, }); + case "PATCH": + return toast.promise(instance.patch(endpoint, _payload), { + loading: promiseMessage?.loading ?? "Patching item...", + success: promiseMessage?.success ?? "Succesfully updated item", + error: (err) => promiseMessage?.error ?? err.message, + }); + case "DELETE": return toast.promise(instance.delete(endpoint), { loading: promiseMessage?.loading ?? "Deleting item...", diff --git a/pwa/src/apiService/resources/object.ts b/pwa/src/apiService/resources/object.ts index fad3c8df..dbc1cf79 100644 --- a/pwa/src/apiService/resources/object.ts +++ b/pwa/src/apiService/resources/object.ts @@ -107,7 +107,7 @@ export default class Sources { const { payload, entityId, objectId } = variables; if (objectId || payload.id) { - const { data } = await this._send(this._instance, "PUT", `/admin/objects/${objectId ?? payload.id}`, payload, { + const { data } = await this._send(this._instance, "PATCH", `/admin/objects/${objectId ?? payload.id}`, payload, { loading: "Updating object...", success: "Object successfully updated.", }); diff --git a/pwa/src/hooks/object.ts b/pwa/src/hooks/object.ts index 0850f1f3..b9003e2a 100644 --- a/pwa/src/hooks/object.ts +++ b/pwa/src/hooks/object.ts @@ -121,5 +121,15 @@ export const useObject = () => { }, }); - return { getAll, getOne, getAllFromEntity, getAllFromList, getSchema, remove, createOrEdit, downloadPDF, getAllSelectOptions }; + return { + getAll, + getOne, + getAllFromEntity, + getAllFromList, + getSchema, + remove, + createOrEdit, + downloadPDF, + getAllSelectOptions, + }; }; diff --git a/pwa/src/services/getIdFromObjectId.ts b/pwa/src/services/getIdFromObjectId.ts new file mode 100644 index 00000000..3fd87bbe --- /dev/null +++ b/pwa/src/services/getIdFromObjectId.ts @@ -0,0 +1,10 @@ +export const getIdFromObjectId = (objects: any) => { + Object.keys(objects).forEach((key) => { objects[key] = getId(objects[key]) }); + return objects; + } + +const getId = (id: string) => { + const finalSlashIndex = id.lastIndexOf("/"); + return id.substring(finalSlashIndex + 1); + }; + diff --git a/pwa/src/templates/schemasTemplate/SchemasTemplate.tsx b/pwa/src/templates/schemasTemplate/SchemasTemplate.tsx index a3418fa6..2796e30e 100644 --- a/pwa/src/templates/schemasTemplate/SchemasTemplate.tsx +++ b/pwa/src/templates/schemasTemplate/SchemasTemplate.tsx @@ -28,7 +28,7 @@ export const SchemasTemplate: React.FC = () => { const downloadSchema = _useSchema.downloadSchema(); const _useObject = useObject(); - const getObjects = _useObject.getAll(1, "asc", 200); + const getObjects = _useObject.getAll(1, "asc", 20000); const { CheckboxBulkSelectAll, CheckboxBulkSelectOne, selectedItems, toggleItem } = useBulkSelect(getSchemas.data); diff --git a/pwa/src/templates/templateParts/objectsFormTemplate/EditObjectFormTemplate.tsx b/pwa/src/templates/templateParts/objectsFormTemplate/EditObjectFormTemplate.tsx index 18892c5a..ac71aaf7 100644 --- a/pwa/src/templates/templateParts/objectsFormTemplate/EditObjectFormTemplate.tsx +++ b/pwa/src/templates/templateParts/objectsFormTemplate/EditObjectFormTemplate.tsx @@ -132,7 +132,7 @@ export const EditObjectFormTemplate: React.FC = ({ type: "download", onSubmit: () => downloadObject.mutate({ id: objectId, name: object.name, type: "PDF" }), }, - { type: "delete", onSubmit: () => handleDeleteObject }, + { type: "delete", onSubmit: () => handleDeleteObject() }, ]} variant="secondary" /> @@ -140,7 +140,7 @@ export const EditObjectFormTemplate: React.FC = ({ } /> - + diff --git a/pwa/src/templates/templateParts/schemaForm/SchemaFormTemplate.tsx b/pwa/src/templates/templateParts/schemaForm/SchemaFormTemplate.tsx index 78415e96..e11e6dd0 100644 --- a/pwa/src/templates/templateParts/schemaForm/SchemaFormTemplate.tsx +++ b/pwa/src/templates/templateParts/schemaForm/SchemaFormTemplate.tsx @@ -20,6 +20,7 @@ import { useObject } from "../../../hooks/object"; import { useQueryClient } from "react-query"; import Skeleton from "react-loading-skeleton"; import { enrichValidation } from "../../../services/enrichReactHookFormValidation"; +import { getIdFromObjectId } from "../../../services/getIdFromObjectId"; export type SchemaInputType = | "string" @@ -40,11 +41,13 @@ interface ReactHookFormProps { } interface SchemaFormTemplateProps { + object?: any; schema: any; disabled?: boolean; } export const SchemaFormTemplate: React.FC = ({ + object, schema, register, errors, @@ -129,6 +132,7 @@ export const SchemaFormTemplate: React.FC ), @@ -156,6 +160,7 @@ export const SchemaFormTemplate: React.FC ), @@ -180,6 +185,7 @@ interface FormFieldGroupProps { maxLength?: number; minLength?: number; schema: any; + object: any; } const FormFieldGroup: React.FC = ({ @@ -200,6 +206,7 @@ const FormFieldGroup: React.FC = ({ maxLength, minLength, schema, + object, }) => { return ( @@ -344,6 +351,7 @@ const FormFieldGroup: React.FC = ({ multiple, type, schema, + object, }} /> )} @@ -364,6 +372,7 @@ const SchemaTypeObject: React.FC = ({ _enum, multiple, schema, + object, }) => { const queryClient = useQueryClient(); const _useObject = useObject(); @@ -371,6 +380,8 @@ const SchemaTypeObject: React.FC = ({ const getAllFromList = _useObject.getAllFromList(`${property?._list}&_limit=500`); + const objectValues = object && (object[name] as any); + const [defaultValue, setDefaultValue] = React.useState(null); React.useEffect(() => { @@ -383,7 +394,11 @@ const SchemaTypeObject: React.FC = ({ } if (multiple) { - selected = getAllFromList.data.filter((item) => property.value?.includes(item._id)); + if (!schema.properties[name].value && !_.isEmpty(objectValues)) { + selected = getAllFromList.data.filter((item) => getIdFromObjectId(objectValues)?.includes(item._id)); + } else { + selected = getAllFromList.data.filter((item) => property.value?.includes(item._id)); + } } if (!selected) {