forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiHooks.ts
More file actions
74 lines (69 loc) · 2.45 KB
/
apiHooks.ts
File metadata and controls
74 lines (69 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import {
skipToken, useMutation, useQuery, useQueryClient,
} from '@tanstack/react-query';
import { libraryAuthoringQueryKeys } from '@src/library-authoring/data/apiHooks';
import {
getWaffleFlags,
waffleFlagDefaults,
bulkModulestoreMigrate,
getModulestoreMigrationStatus,
BulkMigrateRequestData,
} from './api';
export const migrationQueryKeys = {
all: ['contentLibrary'],
/**
* Base key for data specific to a migration task
*/
migrationTask: (migrationId?: string | null) => [...migrationQueryKeys.all, migrationId],
};
/**
* Get the waffle flags (which enable/disable specific features). They may
* depend on which course we're in.
*/
export const useWaffleFlags = (courseId?: string) => {
const queryClient = useQueryClient();
const { data, isPending: isLoading, isError } = useQuery({
queryKey: ['waffleFlags', courseId],
queryFn: () => getWaffleFlags(courseId),
// Waffle flags change rarely, so never bother refetching them:
staleTime: Infinity,
refetchOnWindowFocus: false,
});
let globalDefaults: typeof waffleFlagDefaults | undefined;
if (data === undefined && courseId) {
// If course-specific waffle flags were requested, first default to the
// global (studio-wide) flags until we've loaded the course-specific ones.
globalDefaults = queryClient.getQueryData(['waffleFlags', undefined]);
}
return {
...waffleFlagDefaults,
...globalDefaults, // Only used if we're requesting course-specific flags.
...data, // the actual flag values loaded from the server
id: courseId,
isLoading,
isError,
};
};
/**
* Use this mutation to migrate multiple sources to a library
*/
export const useBulkModulestoreMigrate = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (requestData: BulkMigrateRequestData) => bulkModulestoreMigrate(requestData),
onSettled: (_data, _err, variables) => {
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.courseImports(variables.target) });
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.allMigrationInfo() });
},
});
};
/**
* Get the migration status
*/
export const useModulestoreMigrationStatus = (migrationId: string | null, refetchInterval: number | false = 1000) => (
useQuery({
queryKey: migrationQueryKeys.migrationTask(migrationId),
queryFn: migrationId ? () => getModulestoreMigrationStatus(migrationId!) : skipToken,
refetchInterval,
})
);