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
335 lines (317 loc) · 13.1 KB
/
apiHooks.ts
File metadata and controls
335 lines (317 loc) · 13.1 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { containerComparisonQueryKeys } from '@src/container-comparison/data/apiHooks';
import { addSection, duplicateSection, updateSectionList } from '@src/course-outline/data/slice';
import {
ConfigureSectionData,
ConfigureSubsectionData,
ConfigureUnitData,
StaticFileNotices,
} from '@src/course-outline/data/types';
import { getNotificationMessage } from '@src/course-unit/data/utils';
import { createGlobalState } from '@src/data/apiHooks';
import type { XBlockBase, XblockChildInfo } from '@src/data/types';
import { getBlockType, getCourseKey } from '@src/generic/key-utils';
import { useMutationWithProcessingNotification } from '@src/generic/processing-notification/data/apiHooks';
import { handleResponseErrors } from '@src/generic/saving-error-alert';
import { useToastContext } from '@src/generic/toast-context';
import { ParentIds } from '@src/generic/types';
import {
QueryClient,
skipToken, useMutation, useQuery, useQueryClient,
} from '@tanstack/react-query';
import { useDispatch } from 'react-redux';
import {
createCourseXblock,
type CreateCourseXBlockType,
deleteCourseItem,
editItemDisplayName,
getCourseDetails,
getCourseItem,
publishCourseItem,
configureCourseSection,
configureCourseSubsection,
configureCourseUnit,
updateCourseSectionHighlights,
duplicateCourseItem,
pasteBlock,
} from './api';
export const courseOutlineQueryKeys = {
all: ['courseOutline'],
/**
* Base key for data specific to a course in outline
*/
course: (courseId?: string) => [...courseOutlineQueryKeys.all, courseId],
courseItemId: (itemId?: string) => [
...courseOutlineQueryKeys.course(itemId ? getCourseKey(itemId) : undefined),
itemId,
],
scrollToCourseItemId: (courseId?: string) => [
...courseOutlineQueryKeys.course(courseId),
'scroll',
],
pasteFileNotices: (courseId?: string) => [
...courseOutlineQueryKeys.course(courseId),
'pasteFileNotices',
],
courseDetails: (courseId?: string) => [
...courseOutlineQueryKeys.course(courseId),
'details',
],
legacyLibReadyToMigrateBlocks: (courseId: string) => [
...courseOutlineQueryKeys.course(courseId),
'legacyLibReadyToMigrateBlocks',
],
legacyLibReadyToMigrateBlocksStatus: (courseId: string, taskId?: string) => [
...courseOutlineQueryKeys.legacyLibReadyToMigrateBlocks(courseId),
'status',
{ taskId },
],
};
type ScrollState = {
id?: string;
};
export const useScrollState = createGlobalState<ScrollState>(courseOutlineQueryKeys.scrollToCourseItemId, {
id: undefined,
});
/**
* Invalidate parent Subsection and Section data.
*
* This function ensures that cached data for parent subsection and section is invalidated
* when child items are created, updated, or deleted.
*
* Priority:
* 1. If sectionId exists, invalidate section data which also updates all children block data
* 2. Else If subsectionId exists, invalidate subsection data
*/
export const invalidateParentQueries = async (queryClient: QueryClient, variables: ParentIds) => {
if (variables.sectionId) {
await queryClient.invalidateQueries({ queryKey: courseOutlineQueryKeys.courseItemId(variables.sectionId) });
} else if (variables.subsectionId) {
// istanbul ignore next
await queryClient.invalidateQueries({ queryKey: courseOutlineQueryKeys.courseItemId(variables.subsectionId) });
}
};
type CreateCourseXBlockMutationProps = CreateCourseXBlockType & ParentIds;
/**
* Hook to create an XBLOCK in a course .
* The `locator` is the ID of the parent block where this new XBLOCK should be created.
* Can also be used to import block from library by passing `libraryContentKey` in request body
*
* @param callback - Optional function called after successful creation to handle additional logic
* @returns Mutation object for creating course blocks
*/
export const useCreateCourseBlock = (
courseKey: string,
callback?: ((locator: string, parentLocator: string) => Promise<void>),
) => {
const queryClient = useQueryClient();
const { setData } = useScrollState(courseKey);
const dispatch = useDispatch();
return useMutationWithProcessingNotification({
mutationFn: (variables: CreateCourseXBlockMutationProps) => createCourseXblock(variables),
onSuccess: async (data: { locator: string; }, variables) => {
await callback?.(data.locator, variables.parentLocator);
queryClient.invalidateQueries({
queryKey: courseOutlineQueryKeys.courseDetails(getCourseKey(data.locator)),
});
await invalidateParentQueries(queryClient, variables);
// scroll to newly added block
setData({ id: data.locator });
// if newly created block is chapter or section, fetch and add it to store
// all other types are handled by invalidateParentQueries and useCourseItemData
if (getBlockType(data.locator) === 'chapter') {
const newBlock = await getCourseItem(data.locator);
dispatch(addSection(newBlock));
}
},
});
};
export const useCourseItemData = <T extends XBlockBase>(itemId?: string, initialData?: T, enabled: boolean = true) => {
const queryClient = useQueryClient();
const dispatch = useDispatch();
return useQuery<T>({
initialData,
queryKey: courseOutlineQueryKeys.courseItemId(itemId),
queryFn: enabled && itemId ? async () => {
const data = await getCourseItem<T>(itemId!);
// If the container has children blocks, update children react-query cache
// data without hitting the API as each xblock call returns its children information as well.
if ('childInfo' in data) {
// This could mean that data is of a section or subsection
(data.childInfo as XblockChildInfo).children.forEach(async (child) => {
await queryClient.cancelQueries({ queryKey: courseOutlineQueryKeys.courseItemId(child.id) });
queryClient.setQueryData(courseOutlineQueryKeys.courseItemId(child.id), child);
if ('childInfo' in child) {
// This means that the data is of section and so its children subsections also
// have children i.e. units
(child.childInfo as XblockChildInfo).children.forEach(async (grandChild) => {
await queryClient.cancelQueries({ queryKey: courseOutlineQueryKeys.courseItemId(grandChild.id) });
queryClient.setQueryData(courseOutlineQueryKeys.courseItemId(grandChild.id), grandChild);
});
}
});
}
// We update redux store section list to update children list in outline.
// Even though each block has its own hook to fetch data, new child blocks or deleted blocks
// won't be detected as the child blocks are rendered in the outline from the top level
// sectionList from redux store.
if (['chapter', 'section'].includes(data.category)) {
const payload = { [data.id]: data };
dispatch(updateSectionList(payload));
}
return data;
} : skipToken,
});
};
export const useCourseDetails = (courseId?: string, enabled: boolean = true) => (
useQuery({
queryKey: courseOutlineQueryKeys.courseDetails(courseId),
queryFn: enabled && courseId ? () => getCourseDetails(courseId) : skipToken,
})
);
/**
* Hook to update the display name of a course block.
*
* This mutation updates the display name of a course item and invalidates relevant cache queries
* to ensure the UI reflects the changes.
*
* @param courseId - The ID of the course containing the item
* @returns Mutation object for updating course block names
*/
export const useUpdateCourseBlockName = (courseId: string) => {
const queryClient = useQueryClient();
return useMutationWithProcessingNotification({
mutationFn: (variables:{
itemId: string;
displayName: string;
} & ParentIds) => editItemDisplayName({ itemId: variables.itemId, displayName: variables.displayName }),
onSuccess: async (_data, variables) => {
await invalidateParentQueries(queryClient, variables);
queryClient.invalidateQueries({ queryKey: containerComparisonQueryKeys.course(courseId) });
queryClient.invalidateQueries({ queryKey: courseOutlineQueryKeys.courseDetails(courseId) });
},
});
};
export const usePublishCourseItem = () => {
const queryClient = useQueryClient();
return useMutationWithProcessingNotification({
mutationFn: (variables:{
itemId: string;
} & ParentIds) => publishCourseItem(variables.itemId),
onSettled: (_data, _err, variables) => {
invalidateParentQueries(queryClient, variables).catch((e) => handleResponseErrors(e));
queryClient.invalidateQueries({ queryKey: courseOutlineQueryKeys.courseDetails(getCourseKey(variables.itemId)) });
},
});
};
export const useDeleteCourseItem = () => {
const queryClient = useQueryClient();
return useMutationWithProcessingNotification({
mutationFn: (variables:{
itemId: string;
} & ParentIds) => deleteCourseItem(variables.itemId),
onSettled: (_data, _err, variables) => {
queryClient.invalidateQueries({ queryKey: courseOutlineQueryKeys.courseDetails(getCourseKey(variables.itemId)) });
invalidateParentQueries(queryClient, variables).catch((e) => handleResponseErrors(e));
},
});
};
export const useConfigureSection = () => {
const queryClient = useQueryClient();
return useMutationWithProcessingNotification({
mutationFn: (variables: ConfigureSectionData & ParentIds) => configureCourseSection(variables),
onSettled: (_data, _err, variables) => {
queryClient.invalidateQueries({
queryKey: courseOutlineQueryKeys.courseDetails(getCourseKey(variables.sectionId)),
});
invalidateParentQueries(queryClient, variables).catch((e) => handleResponseErrors(e));
},
});
};
export const useConfigureSubsection = () => {
const queryClient = useQueryClient();
return useMutationWithProcessingNotification({
mutationFn: (variables: ConfigureSubsectionData & ParentIds) => configureCourseSubsection(variables),
onSettled: (_data, _err, variables) => {
queryClient.invalidateQueries({ queryKey: courseOutlineQueryKeys.courseDetails(getCourseKey(variables.itemId)) });
invalidateParentQueries(queryClient, variables).catch((e) => handleResponseErrors(e));
},
});
};
export const useConfigureUnit = () => {
const queryClient = useQueryClient();
const { showToast, closeToast } = useToastContext();
return useMutation({
mutationFn: (variables: ConfigureUnitData & ParentIds) => configureCourseUnit(variables),
onMutate: (variables) => {
const msg = getNotificationMessage(variables.type, variables.isVisibleToStaffOnly, true);
// Show processing notification
showToast(msg, undefined, 15000);
},
onSettled: (_data, _err, variables) => {
queryClient.invalidateQueries({ queryKey: courseOutlineQueryKeys.courseDetails(getCourseKey(variables.unitId)) });
invalidateParentQueries(queryClient, variables).catch((e) => handleResponseErrors(e));
closeToast();
},
});
};
export const useUpdateCourseSectionHighlights = () => {
const queryClient = useQueryClient();
return useMutationWithProcessingNotification({
mutationFn: (variables: {
sectionId: string;
highlights: string[];
} & ParentIds) => updateCourseSectionHighlights(variables.sectionId, variables.highlights),
onSettled: (_data, _err, variables) => {
queryClient.invalidateQueries({
queryKey: courseOutlineQueryKeys.courseDetails(getCourseKey(variables.sectionId)),
});
invalidateParentQueries(queryClient, variables).catch((e) => handleResponseErrors(e));
},
});
};
export const useDuplicateItem = (courseKey: string) => {
const queryClient = useQueryClient();
const dispatch = useDispatch();
const { setData } = useScrollState(courseKey);
return useMutationWithProcessingNotification({
mutationFn: (variables: {
itemId: string;
parentId: string;
} & ParentIds) => duplicateCourseItem(variables.itemId, variables.parentId),
onSuccess: async (data, variables) => {
await invalidateParentQueries(queryClient, variables);
// add duplicated section to store, subsection and unit are handled by invalidateParentQueries
if (getBlockType(variables.itemId) === 'chapter') {
const duplicatedItem = await getCourseItem(data.locator);
dispatch(duplicateSection({ id: variables.itemId, duplicatedItem }));
}
// scroll to newly added block
setData({ id: data.locator });
},
});
};
export const usePasteFileNotices = createGlobalState<StaticFileNotices>(
courseOutlineQueryKeys.pasteFileNotices,
{
newFiles: [],
conflictingFiles: [],
errorFiles: [],
},
);
export const usePasteItem = (courseId?: string) => {
const queryClient = useQueryClient();
const { setData: setScrollState } = useScrollState(courseId);
const { setData } = usePasteFileNotices(courseId);
return useMutationWithProcessingNotification({
mutationFn: (variables: {
parentLocator: string;
} & ParentIds) => pasteBlock(variables.parentLocator),
onSuccess: async (data, variables) => {
await invalidateParentQueries(queryClient, variables);
// set pasteFileNotices
setData(data.staticFileNotices);
// scroll to pasted block
setScrollState({ id: data.locator });
},
});
};