fix: content list not getting updated

This commit is contained in:
Amit Ranjan 2024-10-01 03:51:19 +05:30
parent 7b57d50c47
commit 6a863e3be0
2 changed files with 18 additions and 1 deletions

View File

@ -194,6 +194,8 @@ export const Contents = () => {
id: params.row.id,
params: {
status: !params.row.status,
title: params.row.title,
entity: (params.row.entity as any).id,
},
});
}}

View File

@ -17,7 +17,7 @@ import {
} from "@/types/base.types";
import { merge } from "@/utils/object";
import { useNormalizeAndCache } from "./helpers";
import { isSameEntity, useNormalizeAndCache } from "./helpers";
import { useGetFromCache } from "./useGet";
import { useEntityApiClient } from "../useApiClient";
@ -42,12 +42,27 @@ export const useUpdate = <
) => {
const api = useEntityApiClient<TAttr, TBasic, TFull>(entity);
const normalizeAndCache = useNormalizeAndCache<TBasic, string>(entity);
const queryClient = useQueryClient();
const { invalidate = true } = options || {};
return useMutation({
mutationFn: async ({ id, params }) => {
const data = await api.update(id, params);
const { entities, result } = normalizeAndCache(data);
if (invalidate) {
queryClient.removeQueries({
predicate: ({ queryKey }) => {
const [qType, qEntity] = queryKey;
return (
(qType === QueryType.count || qType === QueryType.collection) &&
isSameEntity(qEntity, entity)
);
},
});
}
return entities[entity]?.[result] as unknown as TBasic;
},
...options,