Skip to content

Commit 82c50a9

Browse files
✨ [#264] feat: better abort implementation
1 parent 73c42fe commit 82c50a9

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

frontend/src/api/zaaktype.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@ import { request } from "~/api/request.ts";
22
import { ListResponse } from "~/api/types";
33
import { components } from "~/types";
44

5-
export const getZaaktype = async ({
5+
export const getZaaktype = ({
66
serviceSlug,
77
zaaktypeUUID,
88
}: {
99
serviceSlug: string;
1010
zaaktypeUUID: string;
11-
}) => {
12-
const response = await request<
11+
}): [
12+
Promise<components["schemas"]["DetailResponse_ExpandableZaakType_"]>,
13+
AbortController,
14+
] => {
15+
const controller = new AbortController();
16+
const signal = controller.signal;
17+
18+
const response = request<
1319
components["schemas"]["DetailResponse_ExpandableZaakType_"]
14-
>("GET", `/service/${serviceSlug}/zaaktypen/${zaaktypeUUID}/`);
15-
return { ...response };
20+
>(
21+
"GET",
22+
`/service/${serviceSlug}/zaaktypen/${zaaktypeUUID}/`,
23+
undefined,
24+
undefined,
25+
undefined,
26+
signal,
27+
);
28+
return [response, controller];
1629
};
1730

1831
/**

frontend/src/pages/zaaktype/components/zaaktypetabs/ZaaktypeAttributeGridTab.tsx

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ import React, {
2020
useMemo,
2121
useState,
2222
} from "react";
23-
import { Link, useLoaderData, useLocation, useParams } from "react-router";
23+
import {
24+
useLoaderData,
25+
useLocation,
26+
useNavigate,
27+
useParams,
28+
} from "react-router";
2429
import { getZaaktype, getZaaktypen } from "~/api/zaaktype.ts";
2530
import {
2631
RelatedObjectBadge,
@@ -57,6 +62,7 @@ export const ZaaktypeAttributeGridTab = ({
5762
}: ZaaktypeAttributeGridTabProps) => {
5863
const { fields } = useLoaderData() as ZaaktypeLoaderData;
5964
const location = useLocation();
65+
const navigate = useNavigate();
6066
const { serviceSlug, catalogusId } = useParams();
6167
invariant(serviceSlug, "serviceSlug must be provided!");
6268

@@ -108,30 +114,36 @@ export const ZaaktypeAttributeGridTab = ({
108114

109115
if (!Array.isArray(raw)) return;
110116

111-
let cancelled = false;
112-
117+
let abortControllers: AbortController[] = [];
113118
const run = async () => {
114119
const urls = raw as string[];
120+
const uuid = urls
121+
.map((url) => getZaaktypeUUID({ url }))
122+
.filter((uuid): uuid is string => Boolean(uuid));
123+
const fetchTuples = uuid.map((zaaktypeUUID) =>
124+
getZaaktype({ serviceSlug, zaaktypeUUID }),
125+
);
126+
const promises: Promise<
127+
components["schemas"]["DetailResponse_ExpandableZaakType_"]
128+
>[] = [];
115129

116-
const fetches = urls.map((url) => {
117-
const zaaktypeUUID = getZaaktypeUUID({ url });
118-
return zaaktypeUUID ? getZaaktype({ serviceSlug, zaaktypeUUID }) : null;
130+
fetchTuples.forEach(([promise, abortController]) => {
131+
promises.push(promise);
132+
abortControllers.push(abortController);
119133
});
120134

121-
const results = await Promise.all(fetches);
122-
if (cancelled) return;
123-
124-
const zaaktypen = results
125-
.filter((res) => res !== null)
126-
.map((res) => res.result);
127-
135+
const results = await Promise.all(promises);
136+
const zaaktypen = results.map((result) => result.result);
128137
setSelectedDeelzaaktypenOptions(mapZaaktypenToOptions(zaaktypen));
129138
};
130139

131140
void run();
132141

133142
return () => {
134-
cancelled = true;
143+
abortControllers.forEach((abortController) =>
144+
abortController.abort("useEffect cleanup"),
145+
);
146+
abortControllers = [];
135147
};
136148
}, [isEditing, fields, object, serviceSlug]);
137149

@@ -164,10 +176,14 @@ export const ZaaktypeAttributeGridTab = ({
164176
uuid && `/${serviceSlug}/${catalogusId}/zaaktypen/${uuid}`;
165177

166178
return to ? (
167-
<Badge>
168-
<Link key={option.value} to={to}>
169-
{option.label}
170-
</Link>
179+
<Badge
180+
href={to}
181+
onClick={(e: React.MouseEvent) => {
182+
e.preventDefault();
183+
navigate(to);
184+
}}
185+
>
186+
{option.label}
171187
</Badge>
172188
) : (
173189
<Badge key={option.value}>{option.label}</Badge>

0 commit comments

Comments
 (0)