Skip to content

Commit 1478d59

Browse files
committed
Move post mutation helpers to hooks files
1 parent d74da7e commit 1478d59

File tree

5 files changed

+96
-116
lines changed

5 files changed

+96
-116
lines changed

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
UseQueryOptions,
3-
// useInfiniteQuery,
43
useMutation,
54
useQuery,
65
useQueryClient,
@@ -16,8 +15,9 @@ import type {
1615
PaginatedLearningResourceList,
1716
LearningResourcesApiLearningResourcesUserlistsPartialUpdateRequest,
1817
LearningResourcesApiLearningResourcesLearningPathsPartialUpdateRequest,
18+
MicroUserListRelationship,
1919
} from "../../generated/v1"
20-
import learningResources, { updateListParents } from "./keyFactory"
20+
import learningResources from "./keyFactory"
2121
import { invalidateResourceQueries } from "./invalidation"
2222
import { invalidateUserListQueries } from "../userLists/invalidation"
2323

@@ -144,6 +144,41 @@ const useSchoolsList = () => {
144144
return useQuery(learningResources.schools())
145145
}
146146

147+
/**
148+
* Given
149+
* - a LearningResource ID
150+
* - a paginated list of current resources
151+
* - a list of new relationships
152+
* - the type of list
153+
* Update the resources' user_list_parents field to include the new relationships
154+
*/
155+
const updateListParents = (
156+
resourceId: number,
157+
staleResources?: PaginatedLearningResourceList,
158+
newRelationships?: MicroUserListRelationship[],
159+
listType?: "userlist" | "learningpath",
160+
) => {
161+
if (!resourceId || !staleResources || !newRelationships || !listType)
162+
return staleResources
163+
const matchIndex = staleResources.results.findIndex(
164+
(res) => res.id === resourceId,
165+
)
166+
if (matchIndex === -1) return staleResources
167+
const updatedResults = [...staleResources.results]
168+
const newResource = { ...updatedResults[matchIndex] }
169+
if (listType === "userlist") {
170+
newResource.user_list_parents = newRelationships
171+
}
172+
if (listType === "learningpath") {
173+
newResource.learning_path_parents = newRelationships
174+
}
175+
updatedResults[matchIndex] = newResource
176+
return {
177+
...staleResources,
178+
results: updatedResults,
179+
}
180+
}
181+
147182
export {
148183
useLearningResourcesList,
149184
useFeaturedLearningResourcesList,

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,4 @@ const resourceHasUserList =
106106
)
107107
}
108108

109-
export {
110-
invalidateResourceQueries,
111-
// invalidateUserListQueries,
112-
invalidateResourceWithUserListQueries,
113-
}
109+
export { invalidateResourceQueries, invalidateResourceWithUserListQueries }

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

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,19 @@ import {
33
learningResourcesApi,
44
learningResourcesSearchApi,
55
topicsApi,
6-
// userListsApi,
76
offerorsApi,
87
platformsApi,
98
schoolsApi,
109
featuredApi,
1110
} from "../../clients"
12-
// import axiosInstance from "../../axios"
1311
import type {
1412
LearningResource,
15-
LearningResourcesApiLearningResourcesListRequest as LRListRequest,
13+
LearningResourcesApiLearningResourcesListRequest as LearningResourcesListRequest,
1614
TopicsApiTopicsListRequest as TopicsListRequest,
17-
PaginatedLearningResourceList,
18-
LearningResourcesSearchApiLearningResourcesSearchRetrieveRequest as LRSearchRequest,
19-
// UserlistsApiUserlistsItemsListRequest as ULResourcesListRequest,
20-
// UserlistsApiUserlistsListRequest as ULListRequest,
21-
// PaginatedUserListRelationshipList,
15+
LearningResourcesSearchApiLearningResourcesSearchRetrieveRequest as LearningResourcesSearchRetrieveRequest,
2216
OfferorsApiOfferorsListRequest,
2317
PlatformsApiPlatformsListRequest,
2418
FeaturedApiFeaturedListRequest as FeaturedListParams,
25-
// UserListRelationship,
26-
MicroUserListRelationship,
2719
} from "../../generated/v1"
2820

2921
const shuffle = ([...arr]) => {
@@ -63,7 +55,7 @@ const learningResources = createQueryKeys("learningResources", {
6355
.learningResourcesRetrieve({ id })
6456
.then((res) => res.data),
6557
}),
66-
list: (params: LRListRequest) => ({
58+
list: (params: LearningResourcesListRequest) => ({
6759
queryKey: [params],
6860
queryFn: () =>
6961
learningResourcesApi
@@ -88,7 +80,7 @@ const learningResources = createQueryKeys("learningResources", {
8880
queryKey: [params],
8981
queryFn: () => topicsApi.topicsList(params).then((res) => res.data),
9082
}),
91-
search: (params: LRSearchRequest) => {
83+
search: (params: LearningResourcesSearchRetrieveRequest) => {
9284
return {
9385
queryKey: [params],
9486
queryFn: () =>
@@ -122,40 +114,4 @@ const learningResources = createQueryKeys("learningResources", {
122114
},
123115
})
124116

125-
/**
126-
* Given
127-
* - a LearningResource ID
128-
* - a paginated list of current resources
129-
* - a list of new relationships
130-
* - the type of list
131-
* Update the resources' user_list_parents field to include the new relationships
132-
*/
133-
const updateListParents = (
134-
resourceId: number,
135-
staleResources?: PaginatedLearningResourceList,
136-
newRelationships?: MicroUserListRelationship[],
137-
listType?: "userlist" | "learningpath",
138-
) => {
139-
if (!resourceId || !staleResources || !newRelationships || !listType)
140-
return staleResources
141-
const matchIndex = staleResources.results.findIndex(
142-
(res) => res.id === resourceId,
143-
)
144-
if (matchIndex === -1) return staleResources
145-
const updatedResults = [...staleResources.results]
146-
const newResource = { ...updatedResults[matchIndex] }
147-
if (listType === "userlist") {
148-
newResource.user_list_parents = newRelationships
149-
}
150-
if (listType === "learningpath") {
151-
newResource.learning_path_parents = newRelationships
152-
}
153-
updatedResults[matchIndex] = newResource
154-
return {
155-
...staleResources,
156-
results: updatedResults,
157-
}
158-
}
159-
160117
export default learningResources
161-
export { updateListParents }

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

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ import type {
1818
} from "../../generated/v1"
1919
// import learningResources from "../learningResources/keyFactory"
2020
import userLists from "./keyFactory"
21-
// import {
22-
// updateListParentsOnAdd,
23-
// updateListParentsOnDestroy,
24-
// } from "./keyFactory"
2521
import {
2622
invalidateResourceWithUserListQueries,
2723
// invalidateResourceQueries,
@@ -196,6 +192,60 @@ const useUserListListItemMove = () => {
196192
},
197193
})
198194
}
195+
196+
// /**
197+
// * Given
198+
// * - a list of learning resources L
199+
// * - a new relationship between learningpath/userlist and a resource R
200+
// * Update the list L so that it includes the updated resource R. (If the list
201+
// * did not contain R to begin with, no change is made)
202+
// */
203+
// const updateListParentsOnAdd = (
204+
// relationship: UserListRelationship,
205+
// oldList?: PaginatedLearningResourceList,
206+
// ) => {
207+
// if (!oldList) return oldList
208+
// const matchIndex = oldList.results.findIndex(
209+
// (res) => res.id === relationship.child,
210+
// )
211+
// if (matchIndex === -1) return oldList
212+
// const updatesResults = [...oldList.results]
213+
// updatesResults[matchIndex] = relationship.resource
214+
// return {
215+
// ...oldList,
216+
// results: updatesResults,
217+
// }
218+
// }
219+
220+
// /**
221+
// * Given
222+
// * - a list of learning resources L
223+
// * - a destroyed relationship between learningpath/userlist and a resource R
224+
// * Update the list L so that it includes the updated resource R. (If the list
225+
// * did not contain R to begin with, no change is made)
226+
// */
227+
// const updateListParentsOnDestroy = (
228+
// relationship: MicroUserListRelationship,
229+
// list?: PaginatedLearningResourceList,
230+
// ) => {
231+
// if (!list) return list
232+
// if (!relationship) return list
233+
// const matchIndex = list.results.findIndex(
234+
// (res) => res.id === relationship.child,
235+
// )
236+
// if (matchIndex === -1) return list
237+
// const updatedResults = [...list.results]
238+
// const newResource = { ...updatedResults[matchIndex] }
239+
// newResource.user_list_parents =
240+
// newResource.user_list_parents?.filter((m) => m.id !== relationship.id) ??
241+
// null
242+
// updatedResults[matchIndex] = newResource
243+
// return {
244+
// ...list,
245+
// results: updatedResults,
246+
// }
247+
// }
248+
199249
export {
200250
useUserListList,
201251
useUserListsDetail,
Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { createQueryKeys } from "@lukemorales/query-key-factory"
22
import axiosInstance from "../../axios"
33
import type {
4-
PaginatedLearningResourceList,
54
UserlistsApiUserlistsItemsListRequest as ItemsListRequest,
65
UserlistsApiUserlistsListRequest as ListRequest,
76
PaginatedUserListRelationshipList,
8-
UserListRelationship,
9-
MicroUserListRelationship,
107
} from "../../generated/v1"
118
import { userListsApi } from "../../clients"
129

@@ -36,58 +33,4 @@ const userLists = createQueryKeys("userLists", {
3633
}),
3734
})
3835

39-
/**
40-
* Given
41-
* - a list of learning resources L
42-
* - a new relationship between learningpath/userlist and a resource R
43-
* Update the list L so that it includes the updated resource R. (If the list
44-
* did not contain R to begin with, no change is made)
45-
*/
46-
const updateListParentsOnAdd = (
47-
relationship: UserListRelationship,
48-
oldList?: PaginatedLearningResourceList,
49-
) => {
50-
if (!oldList) return oldList
51-
const matchIndex = oldList.results.findIndex(
52-
(res) => res.id === relationship.child,
53-
)
54-
if (matchIndex === -1) return oldList
55-
const updatesResults = [...oldList.results]
56-
updatesResults[matchIndex] = relationship.resource
57-
return {
58-
...oldList,
59-
results: updatesResults,
60-
}
61-
}
62-
63-
/**
64-
* Given
65-
* - a list of learning resources L
66-
* - a destroyed relationship between learningpath/userlist and a resource R
67-
* Update the list L so that it includes the updated resource R. (If the list
68-
* did not contain R to begin with, no change is made)
69-
*/
70-
const updateListParentsOnDestroy = (
71-
relationship: MicroUserListRelationship,
72-
list?: PaginatedLearningResourceList,
73-
) => {
74-
if (!list) return list
75-
if (!relationship) return list
76-
const matchIndex = list.results.findIndex(
77-
(res) => res.id === relationship.child,
78-
)
79-
if (matchIndex === -1) return list
80-
const updatedResults = [...list.results]
81-
const newResource = { ...updatedResults[matchIndex] }
82-
newResource.user_list_parents =
83-
newResource.user_list_parents?.filter((m) => m.id !== relationship.id) ??
84-
null
85-
updatedResults[matchIndex] = newResource
86-
return {
87-
...list,
88-
results: updatedResults,
89-
}
90-
}
91-
9236
export default userLists
93-
export { updateListParentsOnAdd, updateListParentsOnDestroy }

0 commit comments

Comments
 (0)