Ajout du parametre siren dans l'url de la page gérer

pull/2105/head
Jérémy Rialland 2022-04-19 20:51:19 +02:00 committed by Johan Girod
parent efc422609a
commit 5d1c32e3db
4 changed files with 75 additions and 81 deletions

View File

@ -1,5 +1,3 @@
export { fetchCompanyDetails } from './sirene'
export async function searchDenominationOrSiren(value: string) {
return searchFullText(value)
}

View File

@ -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<FabriqueSocialEntreprise | null> {
// 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<Array<Etablissement> | 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
}

View File

@ -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() {
<TrackChapter chapter1="gerer">
<Switch>
<Route exact path={sitePaths.gérer.index} component={Home} />
<Route
exact
path={[sitePaths.gérer.index, sitePaths.gérer.siren]}
component={Home}
/>
<Route
path={sitePaths.gérer.sécuritéSociale}
component={SocialSecurity}
@ -163,7 +170,10 @@ function Home() {
const simulateurs = useSimulatorsData()
const sitePaths = useContext(SitePathsContext)
const engine = useEngine()
if (!engine.evaluate('entreprise . SIREN').nodeValue) {
const engineSiren = engine.evaluate('entreprise . SIREN').nodeValue
const { siren, entrepriseNotFound, entreprisePending } = useSirenFromParams()
if ((!siren && !engineSiren) || (siren && entrepriseNotFound)) {
return <Redirect to={sitePaths.index} />
}
@ -185,7 +195,15 @@ function Home() {
aux simulateurs adaptés à votre situation.
</Trans>
</Intro>
<AskCompanyMissingDetails />
{entreprisePending ? (
<Message type="info" border={false}>
<Intro>
<Trans i18nKey="loading">Chargement en cours...</Trans>
</Intro>
</Message>
) : (
<AskCompanyMissingDetails />
)}
<Spacing xl />
</PageHeader>
@ -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;

View File

@ -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':