Skip to content

Commit

Permalink
refacto codeCommune + api adresse
Browse files Browse the repository at this point in the history
  • Loading branch information
Ornella452 committed Sep 30, 2024
1 parent 94a5b61 commit 0d3339b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/views/candidature-conseiller/AddressChooser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export default function AddressChooser({ setCodeCommune }) {
id="lieuHabitation"
list="resultatsRecherche"
isRequired={false}
onChange={debounce(event => {
onChange={debounce(async event => {
searchByName(event.target.value);
const codeCommune = villes.find(({ codesPostaux, nom }) =>
const codeCommune = await villes.find(({ codesPostaux, nom }) =>
`${codesPostaux[0]} ${nom}` === event.target.value)?.code;
setCodeCommune(codeCommune);
})}
Expand Down
69 changes: 26 additions & 43 deletions src/views/candidature-conseiller/CandidatureConseiller.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -584,46 +584,26 @@ describe('candidature conseiller', () => {
vi.useRealTimers();
});

it('quand je valide le formulaire alors j’envoi toute les données nescessaire', async () => {
it.only('quand je valide le formulaire alors j’envoi toute les données nescessaire', async () => {

Check warning on line 587 in src/views/candidature-conseiller/CandidatureConseiller.test.jsx

View workflow job for this annotation

GitHub Actions / lint

Focused tests are not allowed
// GIVEN
const geoApiResponse = [
{
code: '75056',
nom: 'Paris',
nom: 'Montreuil',
code: '93048',
codesPostaux: [
'75001',
'75002',
'75003',
'75004',
'75005',
'75006',
'75007',
'75008',
'75009',
'75010',
'75011',
'75012',
'75013',
'75014',
'75015',
'75016',
'75017',
'75018',
'75019',
'75020',
'75116'
'93100'
],
},
{
code: '82137',
nom: 'Parisot',
codesPostaux: [
'82160'
],
},
centre: {
type: 'Point',
coordinates: [2.4491, 48.8637]
},
codeDepartement: '93',
codeRegion: '11'
}
];
vi.spyOn(useGeoApi, 'useGeoApi').mockResolvedValueOnce({
villes: async () => Promise.resolve(geoApiResponse)
vi.spyOn(useGeoApi, 'useGeoApi').mockReturnValue({
villes: geoApiResponse,
getVilleParCode: async () => Promise.resolve(geoApiResponse),
});

vi.useFakeTimers();
Expand Down Expand Up @@ -654,6 +634,9 @@ describe('candidature conseiller', () => {
const email = screen.getByLabelText('Adresse e-mail * Format attendu : [email protected]');
fireEvent.change(email, { target: { value: '[email protected]' } });

const adresse = screen.getByLabelText('Votre lieu d’habitation Saississez le nom ou le code postal de votre commune.');
fireEvent.change(adresse, { target: { value: '93100 Montreuil' } });

const enEmploi = screen.getByRole('checkbox', { name: 'En emploi' });
fireEvent.click(enEmploi);

Expand Down Expand Up @@ -706,16 +689,16 @@ describe('candidature conseiller', () => {
'estDemandeurEmploi': false,
'estEnFormation': false,
'estDiplomeMedNum': false,
'codeCom': null,
// TODO A rajouter
// 'nomCommune': 'Montreuil',
// 'codePostal': '93100',
// 'codeCommune': '93048',
// 'location': { 'type': 'Point',
// 'coordinates': [2.4491,
// 48.8637] },
// 'codeDepartement': '93',
// 'codeRegion': '11',
'nomCommune': 'Montreuil',
'codePostal': '93100',
'codeCommune': '93048',
'location': { 'type': 'Point',
'coordinates': [2.4491,
48.8637] },
'codeDepartement': '93',
'codeRegion': '11',
'codeCom': null,
}));

vi.useRealTimers();
Expand Down
22 changes: 18 additions & 4 deletions src/views/candidature-conseiller/useGeoApi.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { useState } from 'react';

export const useGeoApi = () => {
const baseUrlSearch = new URL('https://api-adresse.data.gouv.fr/search');
baseUrlSearch.searchParams.set('type', 'municipality');
baseUrlSearch.searchParams.set('autocomplete', '1');

const baseUrl = new URL('https://geo.api.gouv.fr/communes');
baseUrl.searchParams.set('limit', '10');
baseUrl.searchParams.set('fields', 'nom,code,codesPostaux,centre,codeDepartement,codeRegion');

const [villes, setVilles] = useState([]);

const distinctVilles = villes => [...new Map(villes.map(item => [item.code, item])).values()];

const searchByName = async rechercheUtilisateur => {
const url = `${baseUrl.toString()}&nom=${rechercheUtilisateur}`;
const url = `${baseUrlSearch.toString()}&q=${rechercheUtilisateur}`;
const villes = await fetch(url);
const resultat = await villes.json();
setVilles(resultat);

const resultat = await villes?.features.map(result => ({
'nom': result.properties.municipality,
'code': result.properties.citycode,
'codesPostaux': [
result.properties.postcode
]
}));
const propositionVilles = await distinctVilles(resultat);
setVilles(propositionVilles);
};

const getVilleParCode = async (codePostal, codeCommune) => {
const url = `${baseUrl.toString()}&codePostal=${codePostal}&codeCommune=${codeCommune}`;
const url = `${baseUrl.toString()}&codePostal=${codePostal}&code=${codeCommune}`;
const ville = await fetch(url);
return await ville.json();
};
Expand Down

0 comments on commit 0d3339b

Please sign in to comment.