Skip to content

Commit

Permalink
return gfay
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck committed Apr 16, 2024
1 parent 5bfe983 commit 7a259ad
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 34 deletions.
45 changes: 22 additions & 23 deletions pages/communes/[code].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,31 @@ const CommuneSource = ({ code }: CommuneSourcePageProps) => {
};

const onPageMesAdressesChange = (newPage: number) => {
setPageMesAdresses((setPageMesAdresses) => ({
...setPageMesAdresses,
const page: any = {
...pageMesAdresses,
current: newPage,
}));
};
setPageMesAdresses(page);
fetchBals(code, page).catch(console.error);
};

const onPageApiDepotChange = (newPage: number) => {
setPageApiDepot((pageApiDepot) => ({ ...pageApiDepot, current: newPage }));
};

const fetchBals = useCallback(
async (commune: string) => {
const res = await searchBasesLocales({
commune,
page: pageMesAdresses.current,
limit: pageMesAdresses.limit,
});
await getBasesLocalesIsHabilitationValid(res.results);
setBals(res.results);
setPageMesAdresses((pageMesAdresses) => ({
...pageMesAdresses,
count: res.count,
}));
},
[pageMesAdresses]
);
const fetchBals = useCallback(async (commune: string, page: any) => {
const res = await searchBasesLocales({
commune,
page: page.current,
limit: page.limit,
});
await getBasesLocalesIsHabilitationValid(res.results);
setBals(res.results);
setPageMesAdresses({
...page,
count: res.count,
});
}, []);

useEffect(() => {
const fetchData = async () => {
Expand All @@ -117,8 +116,8 @@ const CommuneSource = ({ code }: CommuneSourcePageProps) => {
}, [code]);

useEffect(() => {
fetchBals(code).catch(console.error);
}, [pageMesAdresses, code, fetchBals]);
fetchBals(code, pageMesAdresses).catch(console.error);
}, [code]);

Check warning on line 120 in pages/communes/[code].tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

React Hook useEffect has missing dependencies: 'fetchBals' and 'pageMesAdresses'. Either include them or remove the dependency array

Check warning on line 120 in pages/communes/[code].tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

React Hook useEffect has missing dependencies: 'fetchBals' and 'pageMesAdresses'. Either include them or remove the dependency array

const revisionsApiDepot = useMemo(() => {
const start = (pageApiDepot.current - 1) * pageApiDepot.limit;
Expand All @@ -135,7 +134,7 @@ const CommuneSource = ({ code }: CommuneSourcePageProps) => {
const onDeleteBal = useCallback(async () => {
try {
await removeBaseLocale(balToDeleted._id);
await fetchBals(code).catch(console.error);
await fetchBals(code, pageMesAdresses).catch(console.error);
setBalToDeleted(null);
toast("La BAL a bien été archivé", { type: "success" });
} catch (e: unknown) {
Expand All @@ -144,7 +143,7 @@ const CommuneSource = ({ code }: CommuneSourcePageProps) => {
toast(e.message, { type: "error" });
}
}
}, [balToDeleted, code, fetchBals, setBalToDeleted]);
}, [balToDeleted, code, fetchBals, setBalToDeleted, pageMesAdresses]);

const actionsBals = {
delete(item: BaseLocaleType) {
Expand Down
59 changes: 48 additions & 11 deletions pages/mes-adresses/base-locale/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Link from "next/link";
import { useRouter } from "next/router";

import { Badge } from "@codegouvfr/react-dsfr/Badge";
import { createModal } from "@codegouvfr/react-dsfr/Modal";

import { Alert } from "@codegouvfr/react-dsfr/Alert";
import type { BaseLocaleType } from "types/mes-adresses";
Expand All @@ -15,6 +16,11 @@ import CopyToClipBoard from "@/components/copy-to-clipboard";
import Button from "@codegouvfr/react-dsfr/Button";
import { validateHabilitation } from "@/lib/api-depot";

const deleteEventModale = createModal({
id: "delete-event-modale",
isOpenedByDefault: false,
});

const NEXT_PUBLIC_MES_ADRESSES_URL =
process.env.NEXT_PUBLIC_MES_ADRESSES_URL ||
"http://mes-adresses.data.gouv.fr";
Expand All @@ -38,7 +44,7 @@ const BaseLocale = () => {
habilitationValid
);
setComputedStatus(status);
setisHabilitationValid(habilitationValid)
setisHabilitationValid(habilitationValid);
}

useEffect(() => {
Expand All @@ -58,6 +64,7 @@ const BaseLocale = () => {
);
await validateHabilitation(habilitation._id);
await calcStatus(baseLocale);
deleteEventModale.close();
};

return baseLocale ? (
Expand Down Expand Up @@ -101,17 +108,36 @@ const BaseLocale = () => {
)}
</ul>
<div className="fr-my-4v">
<Badge severity={computedStatus?.intent} noIcon>
<Badge
severity={computedStatus?.intent}
noIcon
style={{ marginRight: 5 }}
>
{computedStatus?.label}
</Badge>
{isHabilitationValid ? (
<Badge severity="success" noIcon>
Habilitation Valide
</Badge>
) : (
<>
<Badge severity="warning" noIcon>
Pas Habilitaté
</Badge>
</>
)}
</div>

<div className="fr-my-4v">
{isHabilitationValid ?
<Badge severity='success' noIcon>Habilitation Valide</Badge>
:
<Button onClick={createBalHabilitation}>Habiliter</Button>
}
{!isHabilitationValid && (
<Button
onClick={() => {
deleteEventModale.open();
}}
>
Habiliter
</Button>
)}
</div>

<ul className="fr-tags-group">
Expand All @@ -138,10 +164,6 @@ const BaseLocale = () => {
</p>
</li>
</ul>




</div>

<div className="fr-col-2">
Expand All @@ -162,6 +184,21 @@ const BaseLocale = () => {
</div>
</div>
</div>
<deleteEventModale.Component title="Suppression">
<p>Êtes-vous sûr de vouloir habilité cette BAL?</p>
<div>
<Button onClick={createBalHabilitation}>Habiliter</Button>
<Button
style={{ marginLeft: "1rem" }}
priority="tertiary"
onClick={() => {
deleteEventModale.close();
}}
>
Annuler
</Button>
</div>
</deleteEventModale.Component>
</div>
) : null;
};
Expand Down

0 comments on commit 7a259ad

Please sign in to comment.