fix: Rename optimistic update in children containers (#2141)
Fix: When you update the title of a unit/subsection in the subsection/section page, the text returns to the previous value for a while
This commit is contained in:
@@ -611,9 +611,12 @@ export const useContainer = (containerId?: string) => (
|
||||
);
|
||||
|
||||
/**
|
||||
* Use this mutation to update the fields of a container in a library
|
||||
* Use this mutation to update the fields of a container in a library.
|
||||
*
|
||||
* Use `affectedParentContainerId` to enable the optimistic update when the container
|
||||
* is updated from a children list of a container
|
||||
*/
|
||||
export const useUpdateContainer = (containerId: string) => {
|
||||
export const useUpdateContainer = (containerId: string, affectedParentContainerId?: string) => {
|
||||
const libraryId = getLibraryId(containerId);
|
||||
const queryClient = useQueryClient();
|
||||
const containerQueryKey = libraryAuthoringQueryKeys.container(containerId);
|
||||
@@ -621,15 +624,36 @@ export const useUpdateContainer = (containerId: string) => {
|
||||
mutationFn: (data: api.UpdateContainerDataRequest) => api.updateContainerMetadata(containerId, data),
|
||||
onMutate: (data) => {
|
||||
const previousData = queryClient.getQueryData(containerQueryKey) as api.Container;
|
||||
queryClient.setQueryData(containerQueryKey, {
|
||||
...previousData,
|
||||
...data,
|
||||
});
|
||||
|
||||
return { previousData };
|
||||
if (previousData) {
|
||||
queryClient.setQueryData(containerQueryKey, {
|
||||
...previousData,
|
||||
...data,
|
||||
});
|
||||
}
|
||||
|
||||
let childrenPreviousData;
|
||||
if (affectedParentContainerId) {
|
||||
const childrenQueryKey = libraryAuthoringQueryKeys.containerChildren(affectedParentContainerId);
|
||||
childrenPreviousData = queryClient.getQueryData(childrenQueryKey) as api.Container[];
|
||||
if (childrenPreviousData) {
|
||||
queryClient.setQueryData(childrenQueryKey, childrenPreviousData.map(item => (
|
||||
item.id === containerId ? { ...item, ...data } : item
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
return { previousData, childrenPreviousData };
|
||||
},
|
||||
onError: (_err, _data, context) => {
|
||||
queryClient.setQueryData(containerQueryKey, context?.previousData);
|
||||
if (context?.previousData) {
|
||||
queryClient.setQueryData(containerQueryKey, context?.previousData);
|
||||
}
|
||||
|
||||
if (affectedParentContainerId && context?.childrenPreviousData) {
|
||||
const childrenQueryKey = libraryAuthoringQueryKeys.containerChildren(affectedParentContainerId);
|
||||
queryClient.setQueryData(childrenQueryKey, context?.childrenPreviousData);
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
// NOTE: We invalidate the library query here because we need to update the library's
|
||||
|
||||
Reference in New Issue
Block a user