Skip to content

Commit

Permalink
Merge pull request #119 from SocialGouv/fix-simplifier-dept
Browse files Browse the repository at this point in the history
feat(demographie): ajout dom
  • Loading branch information
alebret authored Dec 13, 2022
2 parents ca851c8 + 8f98ed8 commit 09c32b7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
9 changes: 9 additions & 0 deletions csv/departments-com.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
id,codeRegion,code,nom
102,COM,975,"Saint-Pierre-et-Miquelon"
103,COM,977,"Saint-Barthélemy"
104,COM,978,"Saint-Martin"
105,COM,984,"Terres australes et antarctiques françaises"
106,COM,986,"Wallis et Futuna"
107,COM,987,"Polynésie française"
108,COM,988,"Nouvelle-Calédonie"
109,COM,989,"Île de Clipperton"
17 changes: 17 additions & 0 deletions pages/api/staticDepartmentComData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from "path"
import { promises as fs } from "fs"

export default async function staticDepartmentComData(_req, res) {
//Find the absolute path of the csv directory
const jsonDirectory = path.join(process.cwd(), "csv")

//Read the csv data file data.csv
const fileContents = await fs.readFile(
jsonDirectory + "/departments-com.csv",
"utf8"
)
console.log(fileContents)

//Return the content of the data file in json format
res.status(200).json(fileContents)
}
45 changes: 39 additions & 6 deletions src/components/DepartmentCodeSelector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useEffect, useState } from "react"
import { Form } from "react-bootstrap"
import useSWR from "swr"
import Papa from "papaparse"

/**
* Departments selector component
Expand All @@ -10,32 +12,63 @@ export function DepartmentCodeSelector({ setSelectedDepartment }) {
const API_DEPT_GOUV_URL = "https://geo.api.gouv.fr/departements"
const API_REGION_GOUV_URL = "https://geo.api.gouv.fr/regions"

const fetcher = (url) => fetch(url).then((res) => res.json())
const { data, error } = useSWR("/api/staticDepartmentComData", fetcher)
if (error) console.warn(error)

const [departments, setDepartments] = useState([])
const [departmentsCom, setDepartmentsCom] = useState([])
const [regions, setRegions] = useState([])

useEffect(() => {
callDepartmentsAPI()
callRegionsAPI()
}, [])

useEffect(() => {
callDepartmentsComCsv()
}, [data])

useEffect(() => {
if (departments && departments.length < 105) {
const departmentsWithCom = departmentsCom.forEach((item) =>
departments.push(item)
)
if (departmentsWithCom) setDepartments(departmentsWithCom)
}
}, [departments])

const callDepartmentsAPI = async () => {
const res = await fetch(API_DEPT_GOUV_URL)
const data = await res.json()
const dataDepartments = await res.json()

data.map((item) => {
dataDepartments.map((item) => {
return { nom: item.nom, code: item.code, codeRegion: item.codeRegion }
})
setDepartments(data)
setDepartments(dataDepartments)
}

const callDepartmentsComCsv = () => {
if (data) {
let com = []
Papa.parse(data, {
header: true,
complete: (results) => {
com = results.data
},
})
setDepartmentsCom(com)
}
}

const callRegionsAPI = async () => {
const res = await fetch(API_REGION_GOUV_URL)
const data = await res.json()
const dataRegions = await res.json()

data.map((item) => {
dataRegions.map((item) => {
return { nom: item.nom, code: item.code }
})
setRegions(data)
setRegions(dataRegions)
}

const handleChangeDepartment = (event, depts) => {
Expand Down

0 comments on commit 09c32b7

Please sign in to comment.