From 5d1c32e3dbf3e66b16333bda9264fa973ce0a59d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rialland?= Date: Tue, 19 Apr 2022 20:51:19 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20du=20parametre=20siren=20dans=20l'url?= =?UTF-8?q?=20de=20la=20page=20g=C3=A9rer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/source/api/fabrique-social.ts | 2 - site/source/api/sirene.ts | 74 ---------------------------- site/source/pages/gerer/index.tsx | 78 ++++++++++++++++++++++++++++-- site/source/sitePaths.ts | 2 + 4 files changed, 75 insertions(+), 81 deletions(-) delete mode 100644 site/source/api/sirene.ts diff --git a/site/source/api/fabrique-social.ts b/site/source/api/fabrique-social.ts index e07094cd4..27c89b69e 100644 --- a/site/source/api/fabrique-social.ts +++ b/site/source/api/fabrique-social.ts @@ -1,5 +1,3 @@ -export { fetchCompanyDetails } from './sirene' - export async function searchDenominationOrSiren(value: string) { return searchFullText(value) } diff --git a/site/source/api/sirene.ts b/site/source/api/sirene.ts deleted file mode 100644 index d974cf54d..000000000 --- a/site/source/api/sirene.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { FabriqueSocialEntreprise } from './fabrique-social' - -const isSIREN = (input: string) => /^[\s]*([\d][\s]*){9}$/.exec(input) -const isSIRET = (input: string) => /^[\s]*([\d][\s]*){14}$/.exec(input) - -export async function fetchCompanyDetails( - siren: string -): Promise { - // Le paramètre `statut_diffusion` filtre les SIREN non diffusibles, cf. - // https://github.com/betagouv/mon-entreprise/issues/1399#issuecomment-770736525 - const response = await fetch( - `https://entreprise.data.gouv.fr/api/sirene/v3/unites_legales/${siren.replace( - /[\s]/g, - '' - )}?statut_diffusion=O` - ) - if (!response.ok) { - return null - } - const json = await response.json() - - return json.unite_legale as FabriqueSocialEntreprise -} - -export async function searchDenominationOrSiren(value: string) { - if (isSIRET(value)) { - value = value.replace(/[\s]/g, '').slice(0, 9) - } - if (isSIREN(value)) { - return [{ siren: value }] - } - - return searchFullText(value) -} - -type SireneData = { - etablissement: Array<{ - siren: string - is_siege: string - categorie_entreprise: string - activite_principale: string - l1_normalisee: string - }> -} - -export type Etablissement = { - siren: string - denomination?: string -} - -async function searchFullText( - text: string -): Promise | null> { - const response = await fetch( - `https://entreprise.data.gouv.fr/api/sirene/v1/full_text/${text}?per_page=5` - ) - if (!response.ok) { - return null - } - const json = (await response.json()) as SireneData - const etablissements = json.etablissement - .filter( - (data) => - data.categorie_entreprise !== 'ETI' && - data.is_siege === '1' && - data.activite_principale !== '8411Z' - ) - .map(({ l1_normalisee: denomination, siren }) => ({ - denomination, - siren, - })) - - return etablissements -} diff --git a/site/source/pages/gerer/index.tsx b/site/source/pages/gerer/index.tsx index c4574bff8..b4fb29573 100644 --- a/site/source/pages/gerer/index.tsx +++ b/site/source/pages/gerer/index.tsx @@ -1,4 +1,4 @@ -import { DottedName } from '@/../../modele-social' +import { searchDenominationOrSiren } from '@/api/fabrique-social' import { CompanyDetails } from '@/components/company/Details' import RuleInput from '@/components/conversation/RuleInput' import { @@ -22,10 +22,12 @@ import { Link } from '@/design-system/typography/link' import { Li, Ul } from '@/design-system/typography/list' import { Body, Intro } from '@/design-system/typography/paragraphs' import { useQuestionList } from '@/hooks/useQuestionList' +import { useSetEntreprise } from '@/hooks/useSetEntreprise' import { evaluateQuestion } from '@/utils' import { Grid } from '@mui/material' +import { DottedName } from 'modele-social' import Engine, { Evaluation } from 'publicodes' -import { useContext } from 'react' +import { useContext, useEffect, useState } from 'react' import { Helmet } from 'react-helmet-async' import { Trans, useTranslation } from 'react-i18next' import { @@ -33,6 +35,7 @@ import { Route, Switch, useLocation, + useParams, useRouteMatch, } from 'react-router' import styled from 'styled-components' @@ -67,7 +70,11 @@ export default function Gérer() { - + } @@ -185,7 +195,15 @@ function Home() { aux simulateurs adaptés à votre situation. - + {entreprisePending ? ( + + + Chargement en cours... + + + ) : ( + + )} @@ -398,6 +416,56 @@ export const AskCompanyMissingDetails = () => { ) } +const useSirenFromParams = () => { + const { siren } = useParams<{ siren?: string }>() + const setEntreprise = useSetEntreprise() + const engine = useEngine() + const engineSiren = engine.evaluate('entreprise . SIREN').nodeValue + + const [entreprisePending, setEntreprisePending] = useState( + (siren != null && siren !== engineSiren) ?? false + ) + const [entrepriseNotFound, setEntrepriseNotFound] = useState(false) + + useEffect(() => { + let canceled = false + if (!siren) { + return + } + setEntreprisePending(true) + searchDenominationOrSiren(siren) + .then((entreprises) => { + if (canceled) { + return + } + setEntreprisePending(false) + if (!entreprises || !entreprises.length) { + return setEntrepriseNotFound(true) + } + setEntreprise(entreprises[0]) + }) + .catch((error) => { + if (canceled) { + return + } + setEntrepriseNotFound(true) + setEntreprisePending(false) + // eslint-disable-next-line no-console + console.error(error) + }) + + return () => { + canceled = true + } + }, [setEntreprise, siren]) + + return { + siren, + entreprisePending, + entrepriseNotFound, + } +} + const FormsImage = styled.img` position: absolute; height: 25rem; diff --git a/site/source/sitePaths.ts b/site/source/sitePaths.ts index 24b81cd23..71b91a5b3 100644 --- a/site/source/sitePaths.ts +++ b/site/source/sitePaths.ts @@ -34,6 +34,7 @@ const rawSitePathsFr = { }, gérer: { index: '/gérer', + siren: '/:siren', embaucher: '/embaucher', sécuritéSociale: '/sécurité-sociale', 'déclaration-charges-sociales-indépendant': @@ -112,6 +113,7 @@ const rawSitePathsEn = { }, gérer: { index: '/manage', + siren: '/:siren', embaucher: '/hiring', sécuritéSociale: '/social-security', 'déclaration-charges-sociales-indépendant':