Skip to content

Commit

Permalink
added different PATCH and PUT route
Browse files Browse the repository at this point in the history
  • Loading branch information
remko48 committed Mar 27, 2024
1 parent d4a254f commit d571ded
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
28 changes: 27 additions & 1 deletion pwa/src/apiService/resources/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class Sources {
return data;
};

public createOrUpdate = async (variables: { payload: any; entityId: string; objectId?: string }): Promise<any> => {
public importCreateOrUpdate = async (variables: { payload: any; entityId: string; objectId?: string }): Promise<any> => {
const { payload, entityId, objectId } = variables;

if (objectId || payload.id) {
Expand All @@ -128,4 +128,30 @@ export default class Sources {

return data;
};

public createOrUpdate = async (variables: { payload: any; entityId: string; objectId?: string }): Promise<any> => {
const { payload, entityId, objectId } = variables;

if (objectId || payload.id) {
const { data } = await this._send(this._instance, "PUT", `/admin/objects/${objectId ?? payload.id}`, payload, {
loading: "Updating object...",
success: "Object successfully updated.",
});

return data;
}

const { data } = await this._send(
this._instance,
"POST",
"/admin/objects",
{ ...payload, _self: payload._self ?? { schema: { id: entityId } } },
{
loading: "Creating object...",
success: "Object successfully created.",
},
);

return data;
};
}
25 changes: 25 additions & 0 deletions pwa/src/hooks/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ export const useObject = () => {
},
});

const importCreateOrEdit = (objectId?: string) =>
useMutation<any, Error, any>(API.Object.importCreateOrUpdate, {
onSuccess: async (newObject) => {
if (objectId) {
updateItem(queryClient, "object", newObject);
}

if (!objectId) {
addItem(queryClient, "object", newObject);
}
},
onError: (error) => {
queryClient.invalidateQueries(["object", objectId]);

console.warn(error.message);
},
onSettled: () => {
if (objectId) {
queryClient.resetQueries(["object", objectId]);
queryClient.resetQueries(["object_schema", objectId]);
}
},
});

const createOrEdit = (objectId?: string) =>
useMutation<any, Error, any>(API.Object.createOrUpdate, {
onSuccess: async (newObject) => {
Expand Down Expand Up @@ -129,6 +153,7 @@ export const useObject = () => {
getSchema,
remove,
createOrEdit,
importCreateOrEdit,
downloadPDF,
getAllSelectOptions,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const FormStepFinalizeImport: React.FC<FormStepFinalizeImportProps> = ({
setExecutedActions,
}) => {
const [isLoading, setIsLoading] = React.useState<boolean>(false);
const createOrEdit = useObject().createOrEdit();
const createOrEdit = useObject().importCreateOrEdit();

const { CheckboxBulkSelectAll, CheckboxBulkSelectOne, selectedItems, toggleItem } = useBulkSelect(
uploadQuery.data?.results,
Expand Down

0 comments on commit d571ded

Please sign in to comment.