Skip to content

Commit

Permalink
Ajout des informations de la ville
Browse files Browse the repository at this point in the history
  • Loading branch information
Alezco committed Sep 19, 2024
1 parent 8ad53cb commit 9e19bda
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 52 deletions.
5 changes: 2 additions & 3 deletions src/components/commun/Checkbox.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import PropTypes from 'prop-types';

export default function Checkbox({ children, id, name, onCheck, required = true }) {
export default function Checkbox({ children, id, onCheck, required = true }) {
return (
<div className="fr-fieldset__element">
<div className="fr-checkbox-group">
<input id={id} type="checkbox" name={name} onChange={onCheck} required={required} />
<input id={id} type="checkbox" name={id} onChange={onCheck} required={required} />
<label className="fr-label" htmlFor={id}>
{children}
</label>
Expand All @@ -17,7 +17,6 @@ export default function Checkbox({ children, id, name, onCheck, required = true
Checkbox.propTypes = {
children: PropTypes.node,
id: PropTypes.string,
name: PropTypes.string,
onCheck: PropTypes.func,
required: PropTypes.bool
};
2 changes: 1 addition & 1 deletion src/views/candidature-conseiller/SituationEtExperience.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function SituationEtExperience({ isSituationValid }) {
Êtes-vous actuellement dans l’une des situations suivantes ? <span className="cc-obligatoire">*</span>
</p>
{situations.map(({ id, libelle }) =>
<Checkbox id={id} key={id} name={id} onCheck={handleCheck} required={false}>
<Checkbox id={id} key={id} onCheck={handleCheck} required={false}>
{libelle}
</Checkbox>
)}
Expand Down
56 changes: 31 additions & 25 deletions src/views/candidature-conseiller/useApiAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,42 @@ export const useApiAdmin = () => {
}
};

const handleSituations = (conseillerData, key) => {
conseillerData[key] = conseillerData[key] === 'on';
const convertValueToBoolean = (conseillerData, key) => {
conseillerData[key] = conseillerData[key] === 'on' || conseillerData[key] === 'oui';
};

const getInformationsVille = async lieuHabitation => {
const codePostal = lieuHabitation.split(' ')?.[0];
const getInformationsVille = async codePostal => {
if (codePostal) {
return await getVilleParCode(codePostal);
}
};

const handleExperienceMedNum = conseillerData => {
conseillerData.aUneExperienceMedNum = conseillerData.aUneExperienceMedNum === 'oui';
const handleInformationsVille = async (formulaireData, codePostal) => {
const informationsVille = (await getInformationsVille(codePostal))?.[0];
formulaireData.nomCommune = informationsVille?.nom;
formulaireData.codePostal = informationsVille?.code;
formulaireData.codeCommune = informationsVille?.code;
formulaireData.location = informationsVille?.centre;
formulaireData.codeDepartement = informationsVille?.codeDepartement;
formulaireData.codeRegion = informationsVille?.codeRegion;
formulaireData.codeCom = informationsVille?.code;
};

const buildConseillerData = async formData => {
const conseillerData = Object.fromEntries(formData);
handleSituations(conseillerData, 'estDemandeurEmploi');
handleSituations(conseillerData, 'estEnEmploi');
handleSituations(conseillerData, 'estEnFormation');
handleSituations(conseillerData, 'estDiplomeMedNum');
handleExperienceMedNum(conseillerData);
const informationsVille = (await getInformationsVille(conseillerData.lieuHabitation))?.[0];
conseillerData.nomCommune = informationsVille?.nom;
conseillerData.codePostal = informationsVille?.code;
conseillerData.codeCommune = informationsVille?.code;
conseillerData.location = informationsVille?.centre;
conseillerData.codeDepartement = informationsVille?.codeDepartement;
conseillerData.codeRegion = informationsVille?.codeRegion;
conseillerData.codeCom = informationsVille?.code;
convertValueToBoolean(conseillerData, 'estDemandeurEmploi');
convertValueToBoolean(conseillerData, 'estEnEmploi');
convertValueToBoolean(conseillerData, 'estEnFormation');
convertValueToBoolean(conseillerData, 'estDiplomeMedNum');
convertValueToBoolean(conseillerData, 'aUneExperienceMedNum');
const codePostal = conseillerData.lieuHabitation.split(' ')?.[0];
await handleInformationsVille(conseillerData, codePostal);
delete conseillerData.lieuHabitation;
delete conseillerData['g-recaptcha-response'];

return JSON.stringify(conseillerData);
};

const handleIdentificationCandidat = structureData => {
structureData.aIdentifieCandidat = structureData.aIdentifieCandidat === 'oui';
};

const handleContact = structureData => {
structureData.contact = {
prenom: structureData.prenom,
Expand All @@ -90,14 +86,24 @@ export const useApiAdmin = () => {

const handleInformationsStructure = structureData => {
structureData.nom = structureData.denomination;
delete structureData.denomination;
};

const handleAdresse = async structureData => {
const codePostal = structureData.adresse.match(/\d{5}/)?.[0];
await handleInformationsVille(structureData, codePostal);
delete structureData.adresse;
};

const buildStructureData = (formData, geoLocation) => {
const buildStructureData = async (formData, geoLocation) => {
const structureData = Object.fromEntries(formData);
structureData.location = geoLocation;
handleIdentificationCandidat(structureData);
convertValueToBoolean(structureData, 'aIdentifieCandidat');
handleContact(structureData);
handleInformationsStructure(structureData);
await handleAdresse(structureData);
convertValueToBoolean(structureData, 'confirmationEngagement');
delete structureData['g-recaptcha-response'];
return JSON.stringify(structureData);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe('candidature coordinateur', () => {
expect(sousTitreVotreMotvation).toBeInTheDocument();

const votreMessage = within(etapeMotivation).getByLabelText('Votre message *');
expect(votreMessage).toHaveAttribute('id', 'votreMessage');
expect(votreMessage).toHaveAttribute('id', 'motivation');
expect(votreMessage).toBeRequired();

const questionsMotivation = within(etapeMotivation).getByRole('list');
Expand Down
2 changes: 1 addition & 1 deletion src/views/candidature-coordinateur/Motivation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function Motivation() {
En quelques lignes, décrivez le motif de votre besoin en recrutement.{' '}
Indiquez les actions prévues, la justification du poste, ainsi que le public ciblé.
</p>
<ZoneDeTexte id="votreMessage">
<ZoneDeTexte id="motivation">
Votre message <span className="cc-obligatoire">*</span>
</ZoneDeTexte>
<ul>
Expand Down
2 changes: 1 addition & 1 deletion src/views/candidature-structure/CandidatureStructure.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function CandidatureStructure() {
event.preventDefault();

const formData = new FormData(event.currentTarget);
const structureData = buildStructureData(formData, geoLocation);
const structureData = await buildStructureData(formData, geoLocation);
const resultatCreation = await creerCandidatureStructure(structureData);
if (resultatCreation.status >= 400) {
const error = await resultatCreation.json();
Expand Down
6 changes: 3 additions & 3 deletions src/views/candidature-structure/CandidatureStructure.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe('candidature structure', () => {
expect(sousTitreVotreMotvation).toBeInTheDocument();

const votreMessage = within(etapeMotivation).getByLabelText('Votre message *');
expect(votreMessage).toHaveAttribute('id', 'votreMessage');
expect(votreMessage).toHaveAttribute('id', 'motivation');
expect(votreMessage).toBeRequired();
});

Expand Down Expand Up @@ -232,7 +232,7 @@ describe('candidature structure', () => {
vi.spyOn(global, 'fetch').mockImplementation();
const mockApiResponse = {
nomStructure: 'AGENCE NATIONALE DE LA COHESION DES TERRITOIRES',
adressStructure: '20 AVENUE DE SEGUR, 75007 PARIS',
adresseStructure: '20 AVENUE DE SEGUR, 75007 PARIS',
isRidet: false,
};
global.fetch.mockResolvedValueOnce({
Expand Down Expand Up @@ -324,7 +324,7 @@ describe('candidature structure', () => {
ok: true,
json: async () => ({
nomStructure: 'AGENCE NATIONALE DE LA COHESION DES TERRITOIRES',
adressStructure: '20 AVENUE DE SEGUR, 75007 PARIS',
adresseStructure: '20 AVENUE DE SEGUR, 75007 PARIS',
isRidet: false
})
});
Expand Down
3 changes: 1 addition & 2 deletions src/views/candidature-structure/InformationsDeStructure.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ export default function InformationsDeContact({ setGeoLocation }) {
value={denomination}
isLoading={loading}
ariaBusy={loading}
onChange={e => setDenomination(e.target.value)}
onChange={event => setDenomination(event.target.value)}
>
Dénomination <span className="cc-obligatoire">*</span>
</Input>
<div className="adresse-container">
<Input
id="adresse"
name="adresse"
value={adresse}
onChange={handleAdresseChange}
readOnly={!entreprise?.isRidet}
Expand Down
2 changes: 1 addition & 1 deletion src/views/candidature-structure/Motivation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function Motivation() {
En quelques lignes, décrivez le motif de votre besoin en recrutement.{' '}
Indiquez les actions prévues, la justification du poste, ainsi que le public ciblé.
</p>
<ZoneDeTexte id="votreMessage">
<ZoneDeTexte id="motivation">
Votre message <span className="cc-obligatoire">*</span>
</ZoneDeTexte>
</fieldset>
Expand Down
26 changes: 12 additions & 14 deletions src/views/candidature-structure/useEntrepriseFinder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState } from 'react';
import axios from 'axios';

const TAILLE_SIRET = 14;
const TAILLE_RIDET = [6, 7];
Expand Down Expand Up @@ -28,13 +27,12 @@ export const useEntrepriseFinder = setGeoLocation => {
};

const getGeoLocationFromAddress = async address => {
console.log('>>>>>>>>>>>>');
try {
const urlAPI = `https://api-adresse.data.gouv.fr/search/?q=${encodeURIComponent(address)}`;
const response = await axios.get(urlAPI);
if (response.data.features && response.data.features.length > 0) {
const geoLocation = response.data.features[0].geometry;
return geoLocation;
const response = await fetch(urlAPI);
const result = await response.json();
if (result.features && result.features.length > 0) {
return result.features[0].geometry;
}
} catch (error) {
setError('Impossible de trouver la localisation de l\'adresse.');
Expand All @@ -54,16 +52,15 @@ export const useEntrepriseFinder = setGeoLocation => {
setLoading(true);
try {
const response = await fetch(`http://localhost:8080/structure/verify-siret-or-ridet/${siretOrRidet}`);
console.log('===========');
if (!response.ok) {
throw new Error(`Error fetching data: ${response.statusText}`);
}
const result = await response.json();
setEntreprise(result);
setDenomination(result.nomStructure || '');
if (siretOrRidet.length === TAILLE_SIRET && result.adressStructure) {
setAdresse(result.adressStructure);
const geoLocation = await getGeoLocationFromAddress(result.adressStructure);
if (siretOrRidet.length === TAILLE_SIRET && result.adresseStructure) {
setAdresse(result.adresseStructure);
const geoLocation = await getGeoLocationFromAddress(result.adresseStructure);
if (geoLocation) {
setGeoLocation(geoLocation);
}
Expand All @@ -85,10 +82,11 @@ export const useEntrepriseFinder = setGeoLocation => {
try {
setAddressLoading(true);
const urlAPI = `https://api-adresse.data.gouv.fr/search/?q=${encodeURIComponent(adressePostale)}`;
const response = await axios.get(urlAPI);
const suggestions = response.data.features.map(feature => ({
label: feature.properties.label,
geometry: feature.geometry
const response = await fetch(urlAPI);
const result = await response.json();
const suggestions = result.features.map(feature => ({
label: `${feature.properties.citycode} ${feature.properties.city}`,
geometry: feature.geometry,
}));
setAddressSuggestions(suggestions);
} catch (error) {
Expand Down

0 comments on commit 9e19bda

Please sign in to comment.