Skip to content

Commit 299f37e

Browse files
committed
fix: invalidate item hierarchy on add/remove to/from container
1 parent 0b76212 commit 299f37e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/library-authoring/data/apiHooks.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ describe('library api hooks', () => {
292292
});
293293

294294
it('should remove container children', async () => {
295-
const containerId = 'lct:org:lib1';
295+
const containerId = 'lct:org:lib:unit:unit-1';
296296
const url = getLibraryContainerChildrenApiUrl(containerId);
297297

298298
axiosMock.onDelete(url).reply(200);
@@ -331,7 +331,8 @@ describe('library api hooks', () => {
331331
// 2. containerChildren
332332
// 3. containerHierarchy
333333
// 4 & 5. subsections
334-
expect(spy).toHaveBeenCalledTimes(5);
334+
// 6 & 7. subsections hierarchy
335+
expect(spy).toHaveBeenCalledTimes(7);
335336
});
336337

337338
describe('publishContainer', () => {

src/library-authoring/data/apiHooks.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,14 @@ export const useAddItemsToContainer = (containerId?: string) => {
790790
queryClient.invalidateQueries({ predicate: (query) => libraryQueryPredicate(query, libraryId) });
791791

792792
const containerType = getBlockType(containerId);
793+
if (['subsection', 'section'].includes(containerType)) {
794+
// If the container is a subsection or section, we invalidate the
795+
// children query to update the hierarchy.
796+
variables.forEach((itemId) => {
797+
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.containerHierarchy(itemId) });
798+
});
799+
}
800+
793801
if (containerType === 'section') {
794802
// We invalidate the search query of the each itemId if the container is a section.
795803
// This because the subsection page calls this query individually.
@@ -849,13 +857,13 @@ export const useUpdateContainerChildren = (containerId?: string) => {
849857
export const useRemoveContainerChildren = (containerId?: string) => {
850858
const queryClient = useQueryClient();
851859
return useMutation({
852-
mutationFn: async (usageKeys: string[]) => {
860+
mutationFn: async (itemIds: string[]) => {
853861
if (!containerId) {
854862
return undefined;
855863
}
856-
return api.removeLibraryContainerChildren(containerId, usageKeys);
864+
return api.removeLibraryContainerChildren(containerId, itemIds);
857865
},
858-
onSettled: () => {
866+
onSettled: (_data, _error, variables) => {
859867
if (!containerId) {
860868
return;
861869
}
@@ -864,6 +872,15 @@ export const useRemoveContainerChildren = (containerId?: string) => {
864872
const libraryId = getLibraryId(containerId);
865873
queryClient.invalidateQueries({ predicate: (query) => libraryQueryPredicate(query, libraryId) });
866874
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.container(containerId) });
875+
876+
const containerType = getBlockType(containerId);
877+
if (['subsection', 'section'].includes(containerType)) {
878+
// If the container is a subsection or section, we invalidate the
879+
// children query to update the hierarchy.
880+
variables.forEach((itemId) => {
881+
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.containerHierarchy(itemId) });
882+
});
883+
}
867884
},
868885
});
869886
};

0 commit comments

Comments
 (0)