refactor(course-outline): replace thunks with react query, add typed APIs, and improve type usages (#2900)
* Replaces configure xblock and section highlights redux functions with react-query. * Replaces section highlights thunks with react query * Replaces duplicate block thunks * Removes add subsection, unit redux functions * Replaces scrollTo redux state to react-query based state. * Replaces paste unit block redux functions * Removes a lot of redux related functions as a result. * Reduces API calls without compromising data integrity.
This commit is contained in:
@@ -130,3 +130,38 @@ export const useCourseDetails = (courseId: string) => {
|
||||
status,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a global state function for a query.
|
||||
*/
|
||||
export function createGlobalState<T>(
|
||||
queryKeyFn: (queryKeyArgs?: any) => unknown[],
|
||||
initialData: T | null = null,
|
||||
) {
|
||||
return (queryKeyArgs?: any) => {
|
||||
const queryClient = useQueryClient();
|
||||
const queryKey = queryKeyFn(queryKeyArgs);
|
||||
|
||||
const { data } = useQuery({
|
||||
queryKey,
|
||||
queryFn: () => Promise.resolve(initialData),
|
||||
refetchInterval: false,
|
||||
refetchOnMount: false,
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnReconnect: false,
|
||||
refetchIntervalInBackground: false,
|
||||
});
|
||||
|
||||
function setData(x: Partial<T>) {
|
||||
queryClient.setQueryData(queryKey, x);
|
||||
}
|
||||
|
||||
async function resetData() {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey,
|
||||
});
|
||||
}
|
||||
|
||||
return { data, setData, resetData };
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user