Skip to content

Commit 49d3079

Browse files
refactor(resources): migrate sanction-check fetchers (#1034)
1 parent 107a56c commit 49d3079

4 files changed

Lines changed: 52 additions & 36 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { sanctionsI18n } from '@app-builder/components/Sanctions/sanctions-i18n';
2+
import { useLoaderRevalidator } from '@app-builder/contexts/LoaderRevalidatorContext';
3+
import { useEnrichMatchMutation } from '@app-builder/queries/screening/enrich-match';
4+
import { useCallbackRef } from '@app-builder/utils/hooks';
5+
import { useTranslation } from 'react-i18next';
6+
import { Button } from 'ui-design-system';
7+
import { Icon } from 'ui-icons';
8+
9+
export function EnrichMatchButton({ matchId }: { matchId: string }) {
10+
const { t } = useTranslation(sanctionsI18n);
11+
const enrichMatchMutation = useEnrichMatchMutation();
12+
const revalidate = useLoaderRevalidator();
13+
14+
const handleButtonClick = useCallbackRef(() => {
15+
enrichMatchMutation.mutateAsync(matchId).then(() => {
16+
revalidate();
17+
});
18+
});
19+
20+
return (
21+
<Button type="button" variant="secondary" className="h-8" onClick={handleButtonClick}>
22+
<Icon icon="download" className="size-5" />
23+
{t('sanctions:enrich_button')}
24+
</Button>
25+
);
26+
}

packages/app-builder/src/components/Sanctions/MatchCard/MatchCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { EnrichMatchButton } from '@app-builder/components/Sanctions/EnrichMatchButton';
12
import { type SanctionCheckMatch } from '@app-builder/models/sanction-check';
2-
import { EnrichMatchButton } from '@app-builder/routes/ressources+/sanction-check+/enrich-match.$matchId';
33
import { useState } from 'react';
44
import { useTranslation } from 'react-i18next';
55
import { CollapsibleV2, Tag } from 'ui-design-system';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getRoute } from '@app-builder/utils/routes';
2+
import { fromUUIDtoSUUID } from '@app-builder/utils/short-uuid';
3+
import { useMutation } from '@tanstack/react-query';
4+
5+
const endpoint = (matchId: string) =>
6+
getRoute('/ressources/sanction-check/enrich-match/:matchId', {
7+
matchId: fromUUIDtoSUUID(matchId),
8+
});
9+
10+
export const useEnrichMatchMutation = () => {
11+
return useMutation({
12+
mutationKey: ['screening', 'enrich-match'],
13+
mutationFn: async (matchId: string) => {
14+
const response = await fetch(endpoint(matchId), {
15+
method: 'POST',
16+
});
17+
18+
return response.json();
19+
},
20+
});
21+
};
Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import { setToastMessage } from '@app-builder/components/MarbleToaster';
2-
import { sanctionsI18n } from '@app-builder/components/Sanctions/sanctions-i18n';
32
import { isStatusConflictHttpError } from '@app-builder/models';
43
import { initServerServices } from '@app-builder/services/init.server';
5-
import { useCallbackRef } from '@app-builder/utils/hooks';
64
import { getRoute } from '@app-builder/utils/routes';
7-
import { fromParams, fromUUIDtoSUUID } from '@app-builder/utils/short-uuid';
5+
import { fromParams } from '@app-builder/utils/short-uuid';
86
import { type ActionFunctionArgs } from '@remix-run/node';
9-
import { useFetcher } from '@remix-run/react';
10-
import { useTranslation } from 'react-i18next';
11-
import { Button } from 'ui-design-system';
12-
import { Icon } from 'ui-icons';
137

148
export async function action({ request, params }: ActionFunctionArgs) {
159
const {
@@ -26,17 +20,15 @@ export async function action({ request, params }: ActionFunctionArgs) {
2620
const t = await getFixedT(request, ['common', 'sanctions']);
2721

2822
try {
29-
const match = await sanctionCheck.enrichMatch({ matchId });
23+
await sanctionCheck.enrichMatch({ matchId });
3024

3125
setToastMessage(session, {
3226
type: 'success',
3327
message: t('sanctions:success.match_enriched'),
3428
});
3529

3630
return Response.json(
37-
{
38-
match,
39-
},
31+
{},
4032
{
4133
headers: { 'Set-Cookie': await commitSession(session) },
4234
},
@@ -55,33 +47,10 @@ export async function action({ request, params }: ActionFunctionArgs) {
5547
});
5648

5749
return Response.json(
58-
{ match: null },
50+
{},
5951
{
6052
headers: { 'Set-Cookie': await commitSession(session) },
6153
},
6254
);
6355
}
6456
}
65-
66-
export function EnrichMatchButton({ matchId }: { matchId: string }) {
67-
const { t } = useTranslation(sanctionsI18n);
68-
const fetcher = useFetcher<typeof action>();
69-
const handleButtonClick = useCallbackRef(() => {
70-
fetcher.submit(
71-
{},
72-
{
73-
method: 'POST',
74-
action: getRoute('/ressources/sanction-check/enrich-match/:matchId', {
75-
matchId: fromUUIDtoSUUID(matchId),
76-
}),
77-
},
78-
);
79-
});
80-
81-
return (
82-
<Button type="button" variant="secondary" className="h-8" onClick={handleButtonClick}>
83-
<Icon icon="download" className="size-5" />
84-
{t('sanctions:enrich_button')}
85-
</Button>
86-
);
87-
}

0 commit comments

Comments
 (0)