From a61943ecd161b0f26330acb24756e71f83fa006b Mon Sep 17 00:00:00 2001 From: Fufeck Date: Wed, 9 Oct 2024 17:13:34 +0200 Subject: [PATCH 1/3] feat: add commune deleguee to voie --- components/bal/toponyme-editor.tsx | 16 ++++ components/bal/voie-editor.tsx | 21 ++++- components/select-commune.tsx | 13 ++- .../voie/signalement-update-voie.tsx | 2 + components/voie/voie-heading.tsx | 10 ++- layouts/editor.tsx | 13 ++- lib/geo-api/index.ts | 83 +++++++++++-------- lib/geo-api/type.ts | 7 ++ lib/openapi/index.ts | 1 + lib/openapi/models/CreateToponymeDTO.ts | 1 + lib/openapi/models/CreateVoieDTO.ts | 1 + lib/openapi/models/ExtendedVoieDTO.ts | 1 + lib/openapi/models/ExtentedToponymeDTO.ts | 1 + lib/openapi/models/Toponyme.ts | 1 + lib/openapi/models/UpdateToponymeDTO.ts | 1 + lib/openapi/models/UpdateVoieDTO.ts | 1 + lib/openapi/models/Voie.ts | 1 + lib/openapi/services/AdminService.ts | 35 ++++++++ pages/bal/[balId]/index.tsx | 1 + pages/bal/[balId]/voies/[idVoie].tsx | 2 +- types/commune.ts | 10 ++- 21 files changed, 176 insertions(+), 46 deletions(-) create mode 100644 lib/openapi/services/AdminService.ts diff --git a/components/bal/toponyme-editor.tsx b/components/bal/toponyme-editor.tsx index f2980a990..d897a6087 100644 --- a/components/bal/toponyme-editor.tsx +++ b/components/bal/toponyme-editor.tsx @@ -21,6 +21,7 @@ import LanguesRegionalesForm from "@/components/langues-regionales-form"; import { BasesLocalesService, Toponyme, ToponymesService } from "@/lib/openapi"; import { CommuneType } from "@/types/commune"; import LayoutContext from "@/contexts/layout"; +import SelectCommune from "../select-commune"; interface ToponymeEditorProps { initialValue?: Toponyme; @@ -38,6 +39,9 @@ function ToponymeEditor({ onSubmitted, }: ToponymeEditorProps) { const [isLoading, setIsLoading] = useState(false); + const [communeDeleguee, setCommuneDeleguee] = useState( + initialValue ? initialValue.communeDeleguee : "" + ); const [nom, onNomChange, resetNom] = useInput(initialValue?.nom || ""); const { getValidationMessage, setValidationMessages } = useValidationMessage(); @@ -65,6 +69,7 @@ function ToponymeEditor({ const body = { nom, nomAlt: Object.keys(nomAlt).length > 0 ? nomAlt : null, + communeDeleguee: communeDeleguee, positions: [], parcelles: selectedParcelles, }; @@ -132,6 +137,7 @@ function ToponymeEditor({ initialValue, nom, nomAlt, + communeDeleguee, markers, selectedParcelles, setToponyme, @@ -186,6 +192,16 @@ function ToponymeEditor({ validationMessage={getValidationMessage("nom")} /> + {commune.communesDeleguees && ( + + )} + void; formInputRef?: React.RefObject; onSubmitted?: () => void; @@ -34,6 +37,7 @@ interface VoieEditorProps { function VoieEditor({ initialValue, + commune, closeForm, formInputRef, onSubmitted, @@ -43,6 +47,9 @@ function VoieEditor({ initialValue ? initialValue.typeNumerotation === "metrique" : false ); const [nom, onNomChange] = useInput(initialValue ? initialValue.nom : ""); + const [communeDeleguee, setCommuneDeleguee] = useState( + initialValue ? initialValue.communeDeleguee : "" + ); const { getValidationMessage, setValidationMessages } = useValidationMessage(); const [nomAlt, setNomAlt] = useState(initialValue?.nomAlt); @@ -65,9 +72,10 @@ function VoieEditor({ nom, nomAlt: Object.keys(nomAlt).length > 0 ? nomAlt : null, typeNumerotation: isMetric ? "metrique" : "numerique", + communeDeleguee: communeDeleguee, trace: data ? data.geometry : null, }; - + console.log(body); // Add or edit a voie const submit = initialValue ? toaster( @@ -124,6 +132,7 @@ function VoieEditor({ }, [ baseLocale.id, + communeDeleguee, initialValue, nom, isMetric, @@ -189,6 +198,16 @@ function VoieEditor({ validationMessage={getValidationMessage("nom")} /> + {commune.communesDeleguees && ( + + )} + void; + withOptionNull?: boolean; + label?: string; } function SelectCommune({ communes, selectedCodeCommune, setSelectedCodeCommune, + withOptionNull = false, + label = null, }: SelectCommuneProps) { return ( - + ); } diff --git a/components/signalement/voie/signalement-update-voie.tsx b/components/signalement/voie/signalement-update-voie.tsx index 849e58af0..8ef4e7da8 100644 --- a/components/signalement/voie/signalement-update-voie.tsx +++ b/components/signalement/voie/signalement-update-voie.tsx @@ -20,6 +20,7 @@ function SignalementUpdateVoie({ existingLocation, handleSubmit, handleClose, + commune, }: SignalementUpdateVoieProps) { const { nom } = signalement.changesRequested; const formInputRef = useRef(null); @@ -52,6 +53,7 @@ function SignalementUpdateVoie({ - setIsFormOpen(false)} /> + setIsFormOpen(false)} + /> ) : ( diff --git a/layouts/editor.tsx b/layouts/editor.tsx index 08771e142..8bb25cff4 100644 --- a/layouts/editor.tsx +++ b/layouts/editor.tsx @@ -28,7 +28,10 @@ import { ExtendedVoieDTO, ExtentedToponymeDTO, } from "@/lib/openapi"; -import { CommuneApiGeoType } from "@/lib/geo-api/type"; +import { + CommuneApiGeoType, + CommuneDelegueeApiGeoType, +} from "@/lib/geo-api/type"; import { MobileControls } from "@/components/mobile-layout/mobile-controls"; import LayoutContext from "@/contexts/layout"; @@ -142,6 +145,14 @@ export async function getBaseEditorProps( ); const commune: CommuneType = { ...geoCommune, ...communeExtras }; + + const communesDeleguees: CommuneDelegueeApiGeoType[] = + await ApiGeoService.getCommunesDeleguee(); + commune.communesDeleguees = communesDeleguees.filter( + ({ chefLieu, type }) => + chefLieu === commune.code && type === "commune-deleguee" + ); + const voies: ExtendedVoieDTO[] = await BasesLocalesService.findBaseLocaleVoies(balId); const toponymes: ExtentedToponymeDTO[] = diff --git a/lib/geo-api/index.ts b/lib/geo-api/index.ts index ff76c5dcd..a074a1c3b 100644 --- a/lib/geo-api/index.ts +++ b/lib/geo-api/index.ts @@ -1,54 +1,67 @@ /* eslint no-restricted-imports: off */ -import qs from 'querystring' -import {toaster} from 'evergreen-ui' -import { CommuneApiGeoType } from './type' +import qs from "querystring"; +import { toaster } from "evergreen-ui"; +import { CommuneApiGeoType, CommuneDelegueeApiGeoType } from "./type"; -const GEO_API_URL = process.env.NEXT_PUBLIC_GEO_API_URL || 'https://geo.api.gouv.fr' +const GEO_API_URL = + process.env.NEXT_PUBLIC_GEO_API_URL || "https://geo.api.gouv.fr"; export class ApiGeoService { - private static async request(url: string) { try { - const res = await fetch(`${GEO_API_URL}${url}`) - return res.json() + const res = await fetch(`${GEO_API_URL}${url}`); + return res.json(); } catch (error) { - toaster.danger('Erreur inattendue', { - description: error.message - }) + toaster.danger("Erreur inattendue", { + description: error.message, + }); } - - return null + + return null; } private static isCodeDep(token: string) { - return ['2A', '2B'].includes(token) || token.match(/^\d{2,3}$/) + return ["2A", "2B"].includes(token) || token.match(/^\d{2,3}$/); } - - public static async searchCommunes(search: string, options = {}): Promise { + + public static async searchCommunes( + search: string, + options = {} + ): Promise { const query: any = { - nom: search - } - - const codeDep: string = search.split(' ').find(token => this.isCodeDep(token)) + nom: search, + }; + + const codeDep: string = search + .split(" ") + .find((token) => this.isCodeDep(token)); if (codeDep) { - query.codeDepartement = codeDep + query.codeDepartement = codeDep; } - - const res = await this.request(`/communes?${qs.stringify({ - ...options, - ...query - })}`) - return res || [] - } - public static async getCommune(code: string, options = {}): Promise { - return this.request(`/communes/${code.toUpperCase()}?${qs.stringify(options)}`) + const res = await this.request( + `/communes?${qs.stringify({ + ...options, + ...query, + })}` + ); + return res || []; } - -} - - - - + public static async getCommune( + code: string, + options = {} + ): Promise { + return this.request( + `/communes/${code.toUpperCase()}?${qs.stringify(options)}` + ); + } + public static async getCommunesDeleguee(): Promise< + CommuneDelegueeApiGeoType[] + > { + return this.request( + `/communes_associees_deleguees?fields=nom,code,chefLieu,type` + ); + } +} diff --git a/lib/geo-api/type.ts b/lib/geo-api/type.ts index ae2799f12..be632423e 100644 --- a/lib/geo-api/type.ts +++ b/lib/geo-api/type.ts @@ -17,3 +17,10 @@ export type CommuneApiGeoType = { siren?: string; _score: number; }; + +export type CommuneDelegueeApiGeoType = { + nom: string; + code: string; + chefLieu: string; + type: string; +}; diff --git a/lib/openapi/index.ts b/lib/openapi/index.ts index 3f71fd0c9..22027900a 100644 --- a/lib/openapi/index.ts +++ b/lib/openapi/index.ts @@ -44,6 +44,7 @@ export { UpdateVoieDTO } from './models/UpdateVoieDTO'; export type { ValidatePinCodeDTO } from './models/ValidatePinCodeDTO'; export { Voie } from './models/Voie'; +export { AdminService } from './services/AdminService'; export { BasesLocalesService } from './services/BasesLocalesService'; export { CommuneService } from './services/CommuneService'; export { ExportCsvService } from './services/ExportCsvService'; diff --git a/lib/openapi/models/CreateToponymeDTO.ts b/lib/openapi/models/CreateToponymeDTO.ts index 0edaabf7a..f1b2bbdd4 100644 --- a/lib/openapi/models/CreateToponymeDTO.ts +++ b/lib/openapi/models/CreateToponymeDTO.ts @@ -8,6 +8,7 @@ import type { Position } from './Position'; export type CreateToponymeDTO = { nom: string; nomAlt?: Record | null; + communeDeleguee?: string | null; parcelles?: Array | null; positions?: Array; }; diff --git a/lib/openapi/models/CreateVoieDTO.ts b/lib/openapi/models/CreateVoieDTO.ts index 4eaeb2fe1..2214f14f6 100644 --- a/lib/openapi/models/CreateVoieDTO.ts +++ b/lib/openapi/models/CreateVoieDTO.ts @@ -8,6 +8,7 @@ import type { LineString } from './LineString'; export type CreateVoieDTO = { nom: string; nomAlt?: Record | null; + communeDeleguee?: string | null; typeNumerotation?: CreateVoieDTO.typeNumerotation; trace?: LineString; }; diff --git a/lib/openapi/models/ExtendedVoieDTO.ts b/lib/openapi/models/ExtendedVoieDTO.ts index 3bade0af8..a3363fe0d 100644 --- a/lib/openapi/models/ExtendedVoieDTO.ts +++ b/lib/openapi/models/ExtendedVoieDTO.ts @@ -15,6 +15,7 @@ export type ExtendedVoieDTO = { balId: string; nom: string; nomAlt: Record; + communeDeleguee: string; typeNumerotation: ExtendedVoieDTO.typeNumerotation; centroid: Record; trace: Record; diff --git a/lib/openapi/models/ExtentedToponymeDTO.ts b/lib/openapi/models/ExtentedToponymeDTO.ts index 7f3b55b8d..a522367a1 100644 --- a/lib/openapi/models/ExtentedToponymeDTO.ts +++ b/lib/openapi/models/ExtentedToponymeDTO.ts @@ -16,6 +16,7 @@ export type ExtentedToponymeDTO = { balId: string; nom: string; nomAlt: Record; + communeDeleguee: string; parcelles: Array; positions: Array; baseLocale: BaseLocale; diff --git a/lib/openapi/models/Toponyme.ts b/lib/openapi/models/Toponyme.ts index 583f3a8a2..57757128f 100644 --- a/lib/openapi/models/Toponyme.ts +++ b/lib/openapi/models/Toponyme.ts @@ -16,6 +16,7 @@ export type Toponyme = { balId: string; nom: string; nomAlt: Record; + communeDeleguee: string; parcelles: Array; positions: Array; baseLocale: BaseLocale; diff --git a/lib/openapi/models/UpdateToponymeDTO.ts b/lib/openapi/models/UpdateToponymeDTO.ts index d20b27310..130fa9384 100644 --- a/lib/openapi/models/UpdateToponymeDTO.ts +++ b/lib/openapi/models/UpdateToponymeDTO.ts @@ -8,6 +8,7 @@ import type { Position } from './Position'; export type UpdateToponymeDTO = { nom?: string; nomAlt?: Record | null; + communeDeleguee?: string | null; parcelles?: Array | null; positions?: Array; }; diff --git a/lib/openapi/models/UpdateVoieDTO.ts b/lib/openapi/models/UpdateVoieDTO.ts index 0e632a180..e482297f1 100644 --- a/lib/openapi/models/UpdateVoieDTO.ts +++ b/lib/openapi/models/UpdateVoieDTO.ts @@ -8,6 +8,7 @@ import type { LineString } from './LineString'; export type UpdateVoieDTO = { nom?: string; nomAlt?: Record | null; + communeDeleguee?: string | null; typeNumerotation?: UpdateVoieDTO.typeNumerotation; trace?: LineString; }; diff --git a/lib/openapi/models/Voie.ts b/lib/openapi/models/Voie.ts index 50d24f8f2..7a256db11 100644 --- a/lib/openapi/models/Voie.ts +++ b/lib/openapi/models/Voie.ts @@ -15,6 +15,7 @@ export type Voie = { balId: string; nom: string; nomAlt: Record; + communeDeleguee: string; typeNumerotation: Voie.typeNumerotation; centroid: Record; trace: Record; diff --git a/lib/openapi/services/AdminService.ts b/lib/openapi/services/AdminService.ts new file mode 100644 index 000000000..7c81c45d2 --- /dev/null +++ b/lib/openapi/services/AdminService.ts @@ -0,0 +1,35 @@ +/* generated using openapi-typescript-codegen -- do no edit */ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +import type { CancelablePromise } from '../core/CancelablePromise'; +import { OpenAPI } from '../core/OpenAPI'; +import { request as __request } from '../core/request'; + +export class AdminService { + + /** + * download email.csv + * @returns any + * @throws ApiError + */ + public static downloadEmailCsv(): CancelablePromise { + return __request(OpenAPI, { + method: 'GET', + url: '/v2/admin/emails.csv', + }); + } + + /** + * Get filaires voies from the published BALs + * @returns any[] + * @throws ApiError + */ + public static getFilairesVoies(): CancelablePromise> { + return __request(OpenAPI, { + method: 'GET', + url: '/v2/admin/filaires-voies', + }); + } + +} diff --git a/pages/bal/[balId]/index.tsx b/pages/bal/[balId]/index.tsx index 1a6523808..b6070f973 100644 --- a/pages/bal/[balId]/index.tsx +++ b/pages/bal/[balId]/index.tsx @@ -235,6 +235,7 @@ function BaseLocalePage({ commune }: BaseLocalePageProps) { > {isFormOpen && selectedTabIndex === 1 && ( { onEdit(null); diff --git a/pages/bal/[balId]/voies/[idVoie].tsx b/pages/bal/[balId]/voies/[idVoie].tsx index 64abcf2bf..3feb4f375 100644 --- a/pages/bal/[balId]/voies/[idVoie].tsx +++ b/pages/bal/[balId]/voies/[idVoie].tsx @@ -37,7 +37,7 @@ function VoiePage({ commune }: VoiePageProps) { return ( <> - + Date: Wed, 27 Nov 2024 11:01:08 +0100 Subject: [PATCH 2/3] openapi: delete adminService --- lib/openapi/index.ts | 1 - lib/openapi/services/AdminService.ts | 35 ---------------------------- 2 files changed, 36 deletions(-) delete mode 100644 lib/openapi/services/AdminService.ts diff --git a/lib/openapi/index.ts b/lib/openapi/index.ts index aa9643dbe..fec439f08 100644 --- a/lib/openapi/index.ts +++ b/lib/openapi/index.ts @@ -45,7 +45,6 @@ export { UpdateVoieDTO } from './models/UpdateVoieDTO'; export type { ValidatePinCodeDTO } from './models/ValidatePinCodeDTO'; export { Voie } from './models/Voie'; -export { AdminService } from './services/AdminService'; export { BasesLocalesService } from './services/BasesLocalesService'; export { CommuneService } from './services/CommuneService'; export { ExportCsvService } from './services/ExportCsvService'; diff --git a/lib/openapi/services/AdminService.ts b/lib/openapi/services/AdminService.ts deleted file mode 100644 index 7c81c45d2..000000000 --- a/lib/openapi/services/AdminService.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* generated using openapi-typescript-codegen -- do no edit */ -/* istanbul ignore file */ -/* tslint:disable */ -/* eslint-disable */ -import type { CancelablePromise } from '../core/CancelablePromise'; -import { OpenAPI } from '../core/OpenAPI'; -import { request as __request } from '../core/request'; - -export class AdminService { - - /** - * download email.csv - * @returns any - * @throws ApiError - */ - public static downloadEmailCsv(): CancelablePromise { - return __request(OpenAPI, { - method: 'GET', - url: '/v2/admin/emails.csv', - }); - } - - /** - * Get filaires voies from the published BALs - * @returns any[] - * @throws ApiError - */ - public static getFilairesVoies(): CancelablePromise> { - return __request(OpenAPI, { - method: 'GET', - url: '/v2/admin/filaires-voies', - }); - } - -} From 9153bc525880b61f749e03bb41b583bea20cfe3a Mon Sep 17 00:00:00 2001 From: Fufeck Date: Mon, 2 Dec 2024 15:59:22 +0100 Subject: [PATCH 3/3] return gfay --- layouts/editor.tsx | 2 +- lib/geo-api/index.ts | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/layouts/editor.tsx b/layouts/editor.tsx index 8bb25cff4..1d1a76056 100644 --- a/layouts/editor.tsx +++ b/layouts/editor.tsx @@ -147,7 +147,7 @@ export async function getBaseEditorProps( const commune: CommuneType = { ...geoCommune, ...communeExtras }; const communesDeleguees: CommuneDelegueeApiGeoType[] = - await ApiGeoService.getCommunesDeleguee(); + await ApiGeoService.getCommunesDeleguee(baseLocale.commune); commune.communesDeleguees = communesDeleguees.filter( ({ chefLieu, type }) => chefLieu === commune.code && type === "commune-deleguee" diff --git a/lib/geo-api/index.ts b/lib/geo-api/index.ts index a074a1c3b..54cc07961 100644 --- a/lib/geo-api/index.ts +++ b/lib/geo-api/index.ts @@ -57,11 +57,9 @@ export class ApiGeoService { ); } - public static async getCommunesDeleguee(): Promise< - CommuneDelegueeApiGeoType[] - > { - return this.request( - `/communes_associees_deleguees?fields=nom,code,chefLieu,type` - ); + public static async getCommunesDeleguee( + chefLieu: string + ): Promise { + return this.request(`/communes_associees_deleguees?chefLieu=${chefLieu}`); } }