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
132 lines (121 loc) · 4.2 KB
/
apiHooks.ts
File metadata and controls
132 lines (121 loc) · 4.2 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {
skipToken, useMutation, useQuery, useQueryClient,
} from '@tanstack/react-query';
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { libraryAuthoringQueryKeys } from '@src/library-authoring/data/apiHooks';
import {
getWaffleFlags,
waffleFlagDefaults,
bulkModulestoreMigrate,
getModulestoreMigrationStatus,
BulkMigrateRequestData,
getCourseDetails,
getPreviewModulestoreMigration,
} from './api';
import { RequestStatus, RequestStatusType } from './constants';
export const migrationQueryKeys = {
all: ['contentLibrary'],
/**
* Base key for data specific to a migration task
*/
migrationTask: (migrationId?: string | null) => [...migrationQueryKeys.all, migrationId],
migrationPreview: (library_key: string, source_key?: string) => [...migrationQueryKeys.all, 'preview', source_key, library_key],
};
export const courseDetailsKey = {
all: ['courseDetails'],
/**
* Base key for get course details data.
*/
courseDetails: (courseId: string) => [...courseDetailsKey.all, courseId],
};
/**
* 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,
})
);
/**
* Get the preview migration given a library key and a source key
*/
export const usePreviewMigration = (libraryKey: string, sourceKey?: string) => (
useQuery({
queryKey: migrationQueryKeys.migrationPreview(libraryKey, sourceKey),
queryFn: sourceKey ? () => getPreviewModulestoreMigration(libraryKey, sourceKey) : skipToken,
})
);
/**
* Get details of a course
*/
export const useCourseDetails = (courseId: string) => {
const query = useQuery({
queryKey: courseDetailsKey.courseDetails(courseId),
queryFn: () => getCourseDetails(courseId, getAuthenticatedUser().username),
retry: false,
});
/**
* Include a status summary field for now, to better match the old redux data
* loading status that other components expect. This could be changed/removed in the future.
*/
let status: RequestStatusType = RequestStatus.PENDING;
if (query.isLoading) {
status = RequestStatus.IN_PROGRESS;
} else if (query.isSuccess) {
status = RequestStatus.SUCCESSFUL;
} else if (query.error) {
const errorStatus = (query.error as any)?.response?.status;
if (errorStatus === 404) {
status = RequestStatus.NOT_FOUND;
} else {
status = RequestStatus.FAILED;
}
}
return {
...query,
status,
};
};