fix: invalidate search results when publishing all changes in library (#1925)

This commit is contained in:
Braden MacDonald
2025-05-08 15:28:07 -07:00
committed by GitHub
parent 8c3fab3792
commit cdb8016657

View File

@@ -91,7 +91,11 @@ export const xblockQueryKeys = {
componentMetadata: (usageKey: string) => [...xblockQueryKeys.xblock(usageKey), 'componentMetadata'], componentMetadata: (usageKey: string) => [...xblockQueryKeys.xblock(usageKey), 'componentMetadata'],
componentDownstreamLinks: (usageKey: string) => [...xblockQueryKeys.xblock(usageKey), 'downstreamLinks'], componentDownstreamLinks: (usageKey: string) => [...xblockQueryKeys.xblock(usageKey), 'downstreamLinks'],
/** Predicate used to invalidate all metadata only */ /**
* Predicate used to invalidate all metadata only (not OLX, fields, assets, etc.).
* Affects all libraries; we could do a more complex version that affects only one library, but it would require
* introspecting the usage keys.
*/
allComponentMetadata: (query: Query) => query.queryKey[0] === 'xblock' && query.queryKey[2] === 'componentMetadata', allComponentMetadata: (query: Query) => query.queryKey[0] === 'xblock' && query.queryKey[2] === 'componentMetadata',
}; };
@@ -208,23 +212,32 @@ export const useContentLibraryV2List = (customParams: api.GetLibrariesV2CustomPa
}) })
); );
/** Publish all changes in the library. */
export const useCommitLibraryChanges = () => { export const useCommitLibraryChanges = () => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: api.commitLibraryChanges, mutationFn: api.commitLibraryChanges,
onSettled: (_data, _error, libraryId) => { onSettled: (_data, _error, libraryId) => {
// Invalidate all content-related metadata and search results for the whole library.
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.contentLibrary(libraryId) }); queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.contentLibrary(libraryId) });
queryClient.invalidateQueries({ predicate: (query) => libraryQueryPredicate(query, libraryId) });
// For XBlocks, the only thing we need to invalidate is the metadata which includes "has unpublished changes"
queryClient.invalidateQueries({ predicate: xblockQueryKeys.allComponentMetadata });
}, },
}); });
}; };
/** Discard all un-published changes in the library */
export const useRevertLibraryChanges = () => { export const useRevertLibraryChanges = () => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: api.revertLibraryChanges, mutationFn: api.revertLibraryChanges,
onSettled: (_data, _error, libraryId) => { onSettled: (_data, _error, libraryId) => {
// Invalidate all content-related metadata and search results for the whole library.
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.contentLibrary(libraryId) }); queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.contentLibrary(libraryId) });
queryClient.invalidateQueries({ predicate: (query) => libraryQueryPredicate(query, libraryId) }); queryClient.invalidateQueries({ predicate: (query) => libraryQueryPredicate(query, libraryId) });
// For XBlocks, the only thing we need to invalidate is the metadata which includes "has unpublished changes"
queryClient.invalidateQueries({ predicate: xblockQueryKeys.allComponentMetadata });
}, },
}); });
}; };