Skip to content

Commit f8a5b1e

Browse files
authored
prevent featured course carousel from re-randomizing (#1673)
* use skipFeatured when invalidating userlists / learning paths after update * add a new function for updating query data for user_list_parents and learning_path_parents on the featured courses list when either is edited without re-randomizing * fix learning path invalidations * fix test
1 parent fb1744f commit f8a5b1e

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

frontends/api/src/hooks/learningResources/index.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ describe("LearningPath CRUD", () => {
211211
}
212212

213213
test("useLearningpathCreate calls correct API", async () => {
214-
const { path, pathUrls, keys } = makeData()
214+
const { path, pathUrls } = makeData()
215215
const url = pathUrls.list
216216

217217
const requestData = { title: path.title }
@@ -226,9 +226,12 @@ describe("LearningPath CRUD", () => {
226226

227227
await waitFor(() => expect(result.current.isSuccess).toBe(true))
228228
expect(makeRequest).toHaveBeenCalledWith("post", url, requestData)
229-
expect(queryClient.invalidateQueries).toHaveBeenCalledWith(
230-
keys.learningResources,
231-
)
229+
expect(queryClient.invalidateQueries).toHaveBeenCalledWith([
230+
"learningResources",
231+
"learningpaths",
232+
"learning_paths",
233+
"list",
234+
])
232235
})
233236

234237
test("useLearningpathDestroy calls correct API", async () => {

frontends/api/src/hooks/learningResources/index.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import learningResources, {
4141
invalidateResourceWithUserListQueries,
4242
updateListParentsOnAdd,
4343
updateListParentsOnDestroy,
44+
updateListParents,
4445
} from "./keyFactory"
4546
import { ListType } from "../../common/constants"
4647

@@ -123,9 +124,9 @@ const useLearningpathCreate = () => {
123124
LearningPathResourceRequest: params,
124125
}),
125126
onSettled: () => {
126-
// Invalidate everything: this is over-aggressive, but the new resource
127-
// could appear in most lists
128-
queryClient.invalidateQueries(learningResources._def)
127+
queryClient.invalidateQueries(
128+
learningResources.learningpaths._ctx.list._def,
129+
)
129130
},
130131
})
131132
}
@@ -343,12 +344,19 @@ const useLearningResourceSetUserListRelationships = () => {
343344
) => learningResourcesApi.learningResourcesUserlistsPartialUpdate(params),
344345
onSettled: (_response, _err, vars) => {
345346
invalidateResourceQueries(queryClient, vars.id, {
346-
skipFeatured: false,
347+
skipFeatured: true,
347348
})
348349
vars.userlist_id?.forEach((userlistId) => {
349350
invalidateUserListQueries(queryClient, userlistId)
350351
})
351352
},
353+
onSuccess: (response, vars) => {
354+
queryClient.setQueriesData<PaginatedLearningResourceList>(
355+
learningResources.featured({}).queryKey,
356+
(featured) =>
357+
updateListParents(vars.id, featured, response.data, "userlist"),
358+
)
359+
},
352360
})
353361
}
354362

@@ -361,14 +369,21 @@ const useLearningResourceSetLearningPathRelationships = () => {
361369
learningResourcesApi.learningResourcesLearningPathsPartialUpdate(params),
362370
onSettled: (_response, _err, vars) => {
363371
invalidateResourceQueries(queryClient, vars.id, {
364-
skipFeatured: false,
372+
skipFeatured: true,
365373
})
366374
vars.learning_path_id?.forEach((learningPathId) => {
367375
invalidateResourceQueries(queryClient, learningPathId, {
368-
skipFeatured: false,
376+
skipFeatured: true,
369377
})
370378
})
371379
},
380+
onSuccess: (response, vars) => {
381+
queryClient.setQueriesData<PaginatedLearningResourceList>(
382+
learningResources.featured({}).queryKey,
383+
(featured) =>
384+
updateListParents(vars.id, featured, response.data, "learningpath"),
385+
)
386+
},
372387
})
373388
}
374389

frontends/api/src/hooks/learningResources/keyFactory.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,47 @@ const updateListParentsOnDestroy = (
371371
}
372372
}
373373

374+
/**
375+
* Given
376+
* - a LearningResource ID
377+
* - a paginated list of current resources
378+
* - a list of new relationships
379+
* - the type of list
380+
* Update the resources' user_list_parents field to include the new relationships
381+
*/
382+
const updateListParents = (
383+
resourceId: number,
384+
staleResources?: PaginatedLearningResourceList,
385+
newRelationships?: MicroUserListRelationship[],
386+
listType?: "userlist" | "learningpath",
387+
) => {
388+
if (!resourceId || !staleResources || !newRelationships || !listType)
389+
return staleResources
390+
const matchIndex = staleResources.results.findIndex(
391+
(res) => res.id === resourceId,
392+
)
393+
if (matchIndex === -1) return staleResources
394+
const updatedResults = [...staleResources.results]
395+
const newResource = { ...updatedResults[matchIndex] }
396+
if (listType === "userlist") {
397+
newResource.user_list_parents = newRelationships
398+
}
399+
if (listType === "learningpath") {
400+
newResource.learning_path_parents = newRelationships
401+
}
402+
updatedResults[matchIndex] = newResource
403+
return {
404+
...staleResources,
405+
results: updatedResults,
406+
}
407+
}
408+
374409
export default learningResources
375410
export {
376411
invalidateResourceQueries,
377412
invalidateUserListQueries,
378413
invalidateResourceWithUserListQueries,
379414
updateListParentsOnAdd,
380415
updateListParentsOnDestroy,
416+
updateListParents,
381417
}

0 commit comments

Comments
 (0)