-
Notifications
You must be signed in to change notification settings - Fork 3
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 commune deleguee to voie #960
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,16 +24,20 @@ import { | |
VoiesService, | ||
} from "@/lib/openapi"; | ||
import LayoutContext from "@/contexts/layout"; | ||
import { CommuneType } from "@/types/commune"; | ||
import SelectCommune from "../select-commune"; | ||
|
||
interface VoieEditorProps { | ||
initialValue?: Voie; | ||
commune: CommuneType; | ||
closeForm: () => void; | ||
formInputRef?: React.RefObject<HTMLDivElement>; | ||
onSubmitted?: () => void; | ||
} | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. à supprimer |
||
// 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 && ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. même remarque |
||
<SelectCommune | ||
communes={commune.communesDeleguees} | ||
selectedCodeCommune={communeDeleguee} | ||
setSelectedCodeCommune={setCommuneDeleguee} | ||
withOptionNull={true} | ||
label="Commune déléguée" | ||
/> | ||
)} | ||
|
||
<Checkbox | ||
checked={isMetric} | ||
label="Cette voie utilise la numérotation métrique" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { CommuneType } from "@/types/commune"; | ||
import { Pane, Select } from "evergreen-ui"; | ||
import { Pane, Select, SelectField } from "evergreen-ui"; | ||
|
||
interface SelectCommuneProps { | ||
communes: { | ||
|
@@ -8,25 +7,31 @@ interface SelectCommuneProps { | |
}[]; | ||
selectedCodeCommune: string; | ||
setSelectedCodeCommune: (code: string) => void; | ||
withOptionNull?: boolean; | ||
label?: string; | ||
} | ||
|
||
function SelectCommune({ | ||
communes, | ||
selectedCodeCommune, | ||
setSelectedCodeCommune, | ||
withOptionNull = false, | ||
label = null, | ||
}: SelectCommuneProps) { | ||
return ( | ||
<Pane marginTop={8}> | ||
<Select | ||
<SelectField | ||
value={selectedCodeCommune} | ||
onChange={(event) => setSelectedCodeCommune(event.target.value)} | ||
label={label} | ||
> | ||
{withOptionNull && <option key="null" value={null}></option>} | ||
{communes.map(({ code, nom }) => ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C'est bizarre de pouvoir sélectionner l'option vide? Je vois pas trop l'intéret à partir du moment où il y'a des communes déléguées, la voie et le toponyme doivent forcément pouvoir matcher avec une d'entre elles non? Nit : aussi pourquoi a-t-on besoin d'un select dédié aux communes, j'ai l'impression qu'un générique pourrait faire le taff |
||
<option key={code} value={code}> | ||
{nom} ({code}) | ||
</option> | ||
))} | ||
</Select> | ||
</SelectField> | ||
</Pane> | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CommuneApiGeoType[]> { | ||
|
||
public static async searchCommunes( | ||
search: string, | ||
options = {} | ||
): Promise<CommuneApiGeoType[]> { | ||
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<CommuneApiGeoType> { | ||
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<CommuneApiGeoType> { | ||
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` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On est obligé de récupérer toutes les communes déléguées de France et filtrer sur le chef lieu? Y'a pas moyen de passer un query param chefLieu à l'api géo? |
||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.length > 0 ?
J'ai un switch vide qui s'affiche