Skip to content

Commit

Permalink
added multiSelect load
Browse files Browse the repository at this point in the history
  • Loading branch information
remko48 committed Mar 26, 2024
1 parent daa98a6 commit d4a254f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
9 changes: 8 additions & 1 deletion pwa/src/apiService/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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...",
Expand Down
2 changes: 1 addition & 1 deletion pwa/src/apiService/resources/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
});
Expand Down
12 changes: 11 additions & 1 deletion pwa/src/hooks/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};
10 changes: 10 additions & 0 deletions pwa/src/services/getIdFromObjectId.ts
Original file line number Diff line number Diff line change
@@ -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);
};

2 changes: 1 addition & 1 deletion pwa/src/templates/schemasTemplate/SchemasTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ export const EditObjectFormTemplate: React.FC<EditObjectFormTemplateProps> = ({
type: "download",
onSubmit: () => downloadObject.mutate({ id: objectId, name: object.name, type: "PDF" }),
},
{ type: "delete", onSubmit: () => handleDeleteObject },
{ type: "delete", onSubmit: () => handleDeleteObject() },
]}
variant="secondary"
/>
</>
}
/>

<SchemaFormTemplate {...{ register, errors, control, schema }} disabled={loading} />
<SchemaFormTemplate {...{ register, errors, control, schema, object }} disabled={loading} />
</form>
</div>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -40,11 +41,13 @@ interface ReactHookFormProps {
}

interface SchemaFormTemplateProps {
object?: any;
schema: any;
disabled?: boolean;
}

export const SchemaFormTemplate: React.FC<SchemaFormTemplateProps & ReactHookFormProps> = ({
object,
schema,
register,
errors,
Expand Down Expand Up @@ -129,6 +132,7 @@ export const SchemaFormTemplate: React.FC<SchemaFormTemplateProps & ReactHookFor
maxLength,
minLength,
schema,
object,
}}
/>
),
Expand Down Expand Up @@ -156,6 +160,7 @@ export const SchemaFormTemplate: React.FC<SchemaFormTemplateProps & ReactHookFor
maxLength,
minLength,
schema,
object,
}}
/>
),
Expand All @@ -180,6 +185,7 @@ interface FormFieldGroupProps {
maxLength?: number;
minLength?: number;
schema: any;
object: any;
}

const FormFieldGroup: React.FC<FormFieldGroupProps & ReactHookFormProps> = ({
Expand All @@ -200,6 +206,7 @@ const FormFieldGroup: React.FC<FormFieldGroupProps & ReactHookFormProps> = ({
maxLength,
minLength,
schema,
object,
}) => {
return (
<FormField>
Expand Down Expand Up @@ -344,6 +351,7 @@ const FormFieldGroup: React.FC<FormFieldGroupProps & ReactHookFormProps> = ({
multiple,
type,
schema,
object,
}}
/>
)}
Expand All @@ -364,13 +372,16 @@ const SchemaTypeObject: React.FC<FormFieldGroupProps & ReactHookFormProps> = ({
_enum,
multiple,
schema,
object,
}) => {
const queryClient = useQueryClient();
const _useObject = useObject();
const property = schema?.properties[name];

const getAllFromList = _useObject.getAllFromList(`${property?._list}&_limit=500`);

const objectValues = object && (object[name] as any);

const [defaultValue, setDefaultValue] = React.useState<any>(null);

React.useEffect(() => {
Expand All @@ -383,7 +394,11 @@ const SchemaTypeObject: React.FC<FormFieldGroupProps & ReactHookFormProps> = ({
}

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) {
Expand Down

0 comments on commit d4a254f

Please sign in to comment.