Skip to content

Commit 1ea7e59

Browse files
talissoncostaclaude
andcommitted
refactor(Dependencies): Read pending dependencies through the typed list query
Drops the cast on the raw responses, and shares each list's cache with the change request page. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 67f8e3b commit 1ea7e59

1 file changed

Lines changed: 54 additions & 29 deletions

File tree

‎frontend/common/services/useFeatureDependency.ts‎

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,34 +124,6 @@ export const featureDependencyService = service
124124
url: `environments/${query.environmentId}/features/${query.featureId}/dependents/`,
125125
}),
126126
}),
127-
// Each change request's dependencies as if it were live, in one query so
128-
// the caller can drop those that change none.
129-
getPendingFeatureDependencies: builder.query<
130-
Res['pendingFeatureDependencies'],
131-
Req['getPendingFeatureDependencies']
132-
>({
133-
providesTags: (res, err, query) => pendingTags(query.featureId),
134-
queryFn: async (query, _, _2, baseQuery) => {
135-
const responses = await Promise.all(
136-
query.changeRequestIds.map((changeRequestId) =>
137-
baseQuery({
138-
url: withChangeRequest(
139-
`environments/${query.environmentId}/features/${query.featureId}/dependencies/`,
140-
changeRequestId,
141-
),
142-
}),
143-
),
144-
)
145-
const failed = responses.find((response) => response.error)
146-
if (failed?.error) return { error: failed.error }
147-
return {
148-
data: responses.map((response, index) => ({
149-
changeRequestId: query.changeRequestIds[index],
150-
results: (response.data as Res['featureDependencies']).results,
151-
})),
152-
}
153-
},
154-
}),
155127
// END OF ENDPOINTS
156128
}),
157129
})
@@ -219,7 +191,6 @@ export const {
219191
useDeleteFeatureDependencyMutation,
220192
useGetFeatureDependenciesQuery,
221193
useGetFeatureDependentsQuery,
222-
useGetPendingFeatureDependenciesQuery,
223194
// END OF EXPORTS
224195
} = featureDependencyService
225196

@@ -228,3 +199,57 @@ const { data, isLoading } = useGetFeatureDependenciesQuery({ environmentId: 'abc
228199
const [createFeatureDependency, { isLoading, data, isSuccess }] = useCreateFeatureDependencyMutation() //create hook
229200
featureDependencyService.endpoints.getFeatureDependencies.select({ environmentId: 'abc', featureId: 2 })(store.getState()) //access data from any function
230201
*/
202+
203+
// Each change request's dependencies as if it were live, in one query so the
204+
// caller can drop those that change none. Injected apart, so it can read the
205+
// typed list query above rather than cast raw responses.
206+
export const pendingFeatureDependencyService =
207+
featureDependencyService.injectEndpoints({
208+
endpoints: (builder) => ({
209+
getPendingFeatureDependencies: builder.query<
210+
Res['pendingFeatureDependencies'],
211+
Req['getPendingFeatureDependencies']
212+
>({
213+
providesTags: (res, err, query) => pendingTags(query.featureId),
214+
queryFn: async (query, { dispatch }) => {
215+
const lists = await Promise.all(
216+
query.changeRequestIds.map((changeRequestId) =>
217+
dispatch(
218+
featureDependencyService.endpoints.getFeatureDependencies.initiate(
219+
{
220+
changeRequestId,
221+
environmentId: query.environmentId,
222+
featureId: query.featureId,
223+
},
224+
{ subscribe: false },
225+
),
226+
),
227+
),
228+
)
229+
if (lists.some((list) => list.isError)) {
230+
return {
231+
error: {
232+
error: "Could not load a change request's dependencies.",
233+
status: 'CUSTOM_ERROR',
234+
},
235+
}
236+
}
237+
return {
238+
data: lists.flatMap((list, index) =>
239+
list.data
240+
? [
241+
{
242+
changeRequestId: query.changeRequestIds[index],
243+
results: list.data.results,
244+
},
245+
]
246+
: [],
247+
),
248+
}
249+
},
250+
}),
251+
}),
252+
})
253+
254+
export const { useGetPendingFeatureDependenciesQuery } =
255+
pendingFeatureDependencyService

0 commit comments

Comments
 (0)