Skip to content

Commit

Permalink
:fix refacto nommage retours commentaires
Browse files Browse the repository at this point in the history
  • Loading branch information
dienamo committed Aug 8, 2024
1 parent ed0d2be commit e63ed7a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/hooks/useEntrepriseApi.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { useState, useCallback } from 'react';
import { debounce } from '../views/candidature-conseiller/debounce';
import { useState } from 'react';

const API_URL_NC = 'https://data.gouv.nc/api/explore/v2.1/catalog/datasets/entreprises-actives-au-ridet/records';
const API_URL_FR = 'https://entreprise.api.gouv.fr/v3/';

const TAILLE_SIRET = 14;
const TAILLE_RIDET = [6, 7];
const TAILLES_POSSIBLES = [...TAILLE_RIDET, TAILLE_SIRET];

const getUrlBySiret = siret => {
const params = '?context=cnum&object=checkSiret&recipient=13002603200016';
const params = '?context=cnum&object=checkSiret&recipient=123456789';
return `${API_URL_FR}insee/sirene/etablissements/${siret}${params}`;
};

Expand All @@ -23,26 +26,26 @@ async function fetchRidetData(ridet) {
return data.results[0] ?? null;
}

export const useCompanyFinder = token => {
export const useEntrepriseFinder = token => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [companyData, setCompanyData] = useState(null);
const [entrepriseData, setEntrepriseData] = useState(null);

const search = (async searchValue => {
if (![6, 7, 14].includes(searchValue.length)) {
if (!TAILLES_POSSIBLES.includes(searchValue.length)) {
setError('Veuillez entrer un RIDET (6 ou 7 chiffres) ou un SIRET (14 chiffres) valide.');
return;
}
setLoading(true);
setError(null);
setCompanyData(null);
setEntrepriseData(null);

try {
let data;
if (searchValue.length === 6 || searchValue.length === 7) {
if (searchValue.length === TAILLE_RIDET[0] || searchValue.length === TAILLE_RIDET[1]) {
data = await fetchRidetData(searchValue);
if (data) {
setCompanyData({
setEntrepriseData({
name: data.denomination,
address: `${data.libelle_commune}, ${data.province}`,
activity: data.libelle_naf
Expand All @@ -58,7 +61,7 @@ export const useCompanyFinder = token => {
}
});
data = await response.json();
setCompanyData({
setEntrepriseData({
name: data.unite_legale.personne_morale_attributs?.raison_sociale || data.unite_legale.personne_physique_attributs?.nom_complet,
address: `${data.adresse.numero_voie} ${data.adresse.type_voie} ${data.adresse.libelle_voie},
${data.adresse.code_postal} ${data.adresse.libelle_commune}`,
Expand All @@ -71,7 +74,6 @@ export const useCompanyFinder = token => {
setLoading(false);
}
});
const debouncedSearch = useCallback(debounce(search, 300), [search]);

return { search: debouncedSearch, loading, error, companyData };
return { search, loading, error, entrepriseData };
};

0 comments on commit e63ed7a

Please sign in to comment.