Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add signalement client authentification #919

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ NEXT_PUBLIC_MATOMO_SITE_ID=
NEXT_PUBLIC_MATOMO_TRACKER_URL=https://stats.beta.gouv.fr/
NEXT_PUBLIC_BAL_ADMIN_URL=
NEXT_PUBLIC_API_SIGNALEMENT=
PORT=3000
PORT=3000
SIGNALEMENT_CLIENT_TOKEN=
29 changes: 6 additions & 23 deletions components/bal/address-preview.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
import { Pane, Text } from "evergreen-ui";

import { computeCompletNumero } from "@/lib/utils/numero";
import { CommuneType } from "@/types/commune";

const getAddressPreview = (numero, suffixe, toponyme, voie, commune) => {
const completNumero = computeCompletNumero(numero, suffixe) || "";
if (toponyme) {
return `${completNumero} ${voie}, ${toponyme} - ${commune.nom} (${commune.code})`;
}

if (voie) {
return `${completNumero} ${voie} - ${commune.nom} (${commune.code})`;
}

if (!voie && !toponyme) {
return `${completNumero} - ${commune.nom} (${commune.code})`;
}

return `${completNumero} ${voie} - ${commune.nom} (${commune.code})`;
};
import { getAddressPreview } from "@/lib/utils/address";

interface AddressPreviewProps {
numero: string | number;
suffixe?: string;
selectedNomToponyme: string;
voie: string;
commune: CommuneType;
selectedNomToponyme?: string;
voie?: string;
commune?: CommuneType;
}

function AddressPreview({
Expand All @@ -38,9 +21,9 @@ function AddressPreview({
const address = getAddressPreview(
numero,
suffixe,
commune,
selectedNomToponyme,
voie,
commune
voie
);

return (
Expand Down
5 changes: 2 additions & 3 deletions components/bal/habilitation-infos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ function HabilitationInfos({ commune }: HabilitationInfosProps) {
}
>
<Text marginTop={5} is="p">
Cette Base Adresse Locale détient une habilitation permettant
d&apos;alimenter la Base Adresse Nationale pour la commune de{" "}
{commune.nom}. Cette habilitation est valide jusqu&apos;au{" "}
Toutes les modifications remonteront automatiquement dans la Base
Adresse Nationale jusqu&apos;au{" "}
<b>{format(new Date(habilitation.expiresAt), "dd/MM/yyyy")}</b>.
</Text>
</Alert>
Expand Down
6 changes: 3 additions & 3 deletions components/bal/numero-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
refreshBALSync,
reloadVoies,
} = useContext(BalDataContext);
const { selectedParcelles } = useContext(ParcellesContext);
const { highlightedParcelles } = useContext(ParcellesContext);
const { markers, suggestedNumero, setCompleteNumero } =
useContext(MarkersContext);
const { reloadTiles } = useContext(MapContext);
Expand Down Expand Up @@ -116,7 +116,7 @@
numero: Number(numero),
suffixe: suffixe?.length > 0 ? suffixe.toLowerCase().trim() : null,
comment: comment.length > 0 ? comment : null,
parcelles: selectedParcelles,
parcelles: highlightedParcelles,
certifie: certifie ?? (initialValue?.certifie || false),
};

Expand All @@ -143,7 +143,7 @@
certifie,
toponymeId,
comment,
selectedParcelles,
highlightedParcelles,
]);

const onFormSubmit = useCallback(
Expand Down Expand Up @@ -262,14 +262,14 @@
if (onVoieChanged) {
onVoieChanged();
}
}, []);

Check warning on line 265 in components/bal/numero-editor.tsx

View workflow job for this annotation

GitHub Actions / build (22.x)

React Hook useCallback has a missing dependency: 'onVoieChanged'. Either include it or remove the dependency array. If 'onVoieChanged' changes too often, find the parent component that defines it and wrap that definition in useCallback

const handleNomVoieChange = useCallback((nomVoie) => {
onNomVoieChange(nomVoie);
if (onVoieChanged) {
onVoieChanged();
}
}, []);

Check warning on line 272 in components/bal/numero-editor.tsx

View workflow job for this annotation

GitHub Actions / build (22.x)

React Hook useCallback has a missing dependency: 'onVoieChanged'. Either include it or remove the dependency array. If 'onVoieChanged' changes too often, find the parent component that defines it and wrap that definition in useCallback

useEffect(() => {
let nom = null;
Expand Down
16 changes: 10 additions & 6 deletions components/bal/numero-editor/select-parcelles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function SelectParcelles({
const { isCadastreDisplayed, setIsCadastreDisplayed } =
useContext(MapContext);
const {
selectedParcelles,
setSelectedParcelles,
highlightedParcelles,
setHighlightedParcelles,
setIsParcelleSelectionEnabled,
hoveredParcelle,
handleHoveredParcelle,
Expand All @@ -36,23 +36,27 @@ function SelectParcelles({
const addressType = isToponyme ? "toponyme" : "numéro";

useEffect(() => {
setSelectedParcelles(initialParcelles);
setHighlightedParcelles(initialParcelles);
setIsParcelleSelectionEnabled(true);

return () => {
setIsParcelleSelectionEnabled(false);
};
}, []);
}, [
setHighlightedParcelles,
setIsParcelleSelectionEnabled,
initialParcelles,
]);

return (
<Pane display="flex" flexDirection="column">
<InputLabel
title="Parcelles cadastre"
help={`Depuis la carte, cliquez sur les parcelles que vous souhaitez ajouter au ${addressType}. En précisant les parcelles associées à cette adresse, vous accélérez sa réutilisation par de nombreux services, DDFiP, opérateurs de courrier, de fibre et de GPS.`}
/>
{selectedParcelles.length > 0 ? (
{highlightedParcelles.length > 0 ? (
<Pane display="grid" gridTemplateColumns="1fr 1fr 1fr">
{selectedParcelles.map((parcelle) => {
{highlightedParcelles.map((parcelle) => {
const isHovered = parcelle === hoveredParcelle?.id;

return (
Expand Down
46 changes: 35 additions & 11 deletions components/bal/signalement-infos.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
import React from "react";
import NextLink from "next/link";
import { Pane, Text, Heading, Link } from "evergreen-ui";
import { Pane, Heading, Button, Alert } from "evergreen-ui";
import { Signalement } from "@/lib/openapi-signalement";
import { useRouter } from "next/navigation";

interface SignalementInfosProps {
balId: string;
signalements: Signalement[];
}

function SignalementInfos({ balId, signalements }: SignalementInfosProps) {
const router = useRouter();
const onClick = () => {
router.push(`/bal/${balId}/signalements`);
};

return (
<Pane backgroundColor="white" padding={8} borderRadius={10} margin={8}>
<Pane
display="flex"
flexDirection="column"
backgroundColor="white"
padding={8}
borderRadius={10}
margin={8}
>
<Heading marginBottom={15}>Signalements</Heading>
<Text marginTop={5} is="p">
Vous avez <b>{signalements.length}</b>{" "}
{signalements.length > 1 ? "signalements" : "signalement"} en attente de
traitement.
</Text>
<Link is={NextLink} href={`/bal/${balId}/signalements`}>
Consulter les signalements
</Link>
<Alert
intent="info"
title={
<Pane fontWeight="normal">
Vous avez reçu <b>{signalements.length}</b>{" "}
{signalements.length > 1 ? "propositions" : "proposition"}.
</Pane>
}
>
<Button
marginTop="1rem"
type="button"
onClick={onClick}
width="fit-content"
alignSelf="center"
appearance="primary"
>
Consulter les signalements
</Button>
</Alert>
</Pane>
);
}
Expand Down
6 changes: 3 additions & 3 deletions components/bal/toponyme-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function ToponymeEditor({
reloadNumeros,
} = useContext(BalDataContext);
const { markers } = useContext(MarkersContext);
const { selectedParcelles } = useContext(ParcellesContext);
const { highlightedParcelles } = useContext(ParcellesContext);
const [ref, setIsFocus] = useFocus(true);

const updateNumerosToponyme = useCallback(
Expand Down Expand Up @@ -89,7 +89,7 @@ function ToponymeEditor({
nom,
nomAlt: Object.keys(nomAlt).length > 0 ? nomAlt : null,
positions: [],
parcelles: selectedParcelles,
parcelles: highlightedParcelles,
};

if (markers) {
Expand Down Expand Up @@ -163,7 +163,7 @@ function ToponymeEditor({
nom,
nomAlt,
markers,
selectedParcelles,
highlightedParcelles,
setToponyme,
closeForm,
refreshBALSync,
Expand Down
8 changes: 5 additions & 3 deletions components/bal/toponymes-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ interface ToponymesListProps {
openForm: () => void;
}

const fuseOptions = {
keys: ["nom"],
};

function ToponymesList({
toponymes,
onEnableEditing,
Expand Down Expand Up @@ -70,9 +74,7 @@ function ToponymesList({
void router.push(`/bal/${balId}/toponymes/${id}`);
};

const [filtered, setFilter] = useFuse(toponymes, 200, {
keys: ["nom"],
});
const [filtered, setFilter] = useFuse(toponymes, 200, fuseOptions);

const scrollableItems = useMemo(
() => sortBy(filtered, (v) => normalizeSort(v.nom)),
Expand Down
8 changes: 5 additions & 3 deletions components/bal/voies-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ interface VoiesListProps {
openForm: () => void;
}

const fuseOptions = {
keys: ["nom"],
};

function VoiesList({
voies,
onEnableEditing,
Expand Down Expand Up @@ -79,9 +83,7 @@ function VoiesList({
void router.push(`/bal/${balId}/voies/${id}`);
};

const [filtered, setFilter] = useFuse(voies, 200, {
keys: ["nom"],
});
const [filtered, setFilter] = useFuse(voies, 200, fuseOptions);

const scrollableItems = useMemo(() => {
const items: ExtendedVoieDTO[] = sortBy(filtered, (v) =>
Expand Down
16 changes: 16 additions & 0 deletions components/base-locale-card/base-locale-card-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface BaseLocaleCardContentProps {
onRemove?: (e: any) => void;
onHide?: (e: any) => void;
isShownHabilitationStatus?: boolean;
pendingSignalementsCount?: number;
}

function BaseLocaleCardContent({
Expand All @@ -50,6 +51,7 @@ function BaseLocaleCardContent({
onSelect,
onRemove,
onHide,
pendingSignalementsCount,
}: BaseLocaleCardContentProps) {
const { status, createdAt, emails } = baseLocale;
const { isMobile } = useContext(LayoutContext);
Expand Down Expand Up @@ -120,6 +122,20 @@ function BaseLocaleCardContent({
</Pane>
)}

{pendingSignalementsCount && (
<Pane
flex={1}
textAlign="center"
margin="auto"
{...(isMobile && { marginBottom: 10 })}
>
<Text display="block">Signalements</Text>
<Text fontWeight="bold" whiteSpace="nowrap">
{pendingSignalementsCount}
</Text>
</Pane>
)}

{commune && (
<Pane
flex={1}
Expand Down
23 changes: 23 additions & 0 deletions components/base-locale-card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import { ApiGeoService } from "@/lib/geo-api";
import StatusBadge from "@/components/status-badge";
import BaseLocaleCardContent from "@/components/base-locale-card/base-locale-card-content";
import {
BaseLocale,
ExtendedBaseLocaleDTO,
HabilitationDTO,
HabilitationService,
OpenAPI,
} from "@/lib/openapi";
import { CommuneApiGeoType } from "@/lib/geo-api/type";
import { Signalement, SignalementsService } from "@/lib/openapi-signalement";

const ADRESSE_URL =
process.env.NEXT_PUBLIC_ADRESSE_URL || "https://adresse.data.gouv.fr";
Expand Down Expand Up @@ -52,6 +54,8 @@ function BaseLocaleCard({
const [habilitation, setHabilitation] = useState<HabilitationDTO | null>(
null
);
const [pendingSignalementsCount, setPendingSignalementsCount] =
useState<number>();
const [isHabilitationValid, setIsHabilitationValid] =
useState<boolean>(false);
const [isOpen, setIsOpen] = useState(isAdmin ? isDefaultOpen : false);
Expand Down Expand Up @@ -100,11 +104,29 @@ function BaseLocaleCard({
Object.assign(OpenAPI, { TOKEN: null });
};

const fetchPendingSignalementsCount = async () => {
const paginatedSignalements = await SignalementsService.getSignalements(
undefined,
undefined,
[Signalement.status.PENDING],
undefined,
undefined,
[baseLocale.commune]
);
setPendingSignalementsCount(paginatedSignalements.total);
};

void fetchCommune();

if (!baseLocale.token) {
void fetchHabilitationIsValid();
} else {
if (
baseLocale.status === BaseLocale.status.PUBLISHED &&
process.env.NEXT_PUBLIC_API_SIGNALEMENT
) {
void fetchPendingSignalementsCount();
}
void fetchHabilitation();
}
}, [baseLocale]);
Expand Down Expand Up @@ -181,6 +203,7 @@ function BaseLocaleCard({
onSelect={onSelect}
onRemove={onRemove}
onHide={onHide}
pendingSignalementsCount={pendingSignalementsCount}
/>
)}
</Card>
Expand Down
8 changes: 5 additions & 3 deletions components/bases-locales-list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ interface BasesLocalesListProps {
basesLocales: ExtendedBaseLocaleDTO[];
}

const fuseOptions = {
keys: ["nom", "commune"],
};

function BasesLocalesList({ basesLocales }: BasesLocalesListProps) {
const { removeBAL, getHiddenBal, addHiddenBal } =
useContext(LocalStorageContext);
Expand All @@ -33,9 +37,7 @@ function BasesLocalesList({ basesLocales }: BasesLocalesListProps) {
Router.push(`/bal/${bal.id}`);
};

const [filtered, onFilter] = useFuse(basesLocales, 200, {
keys: ["nom", "commune"],
});
const [filtered, onFilter] = useFuse(basesLocales, 200, fuseOptions);

const onRemove = useCallback(async () => {
await removeBAL(toRemove);
Expand Down
Loading
Loading