refactor(recherche-entreprise): introduit un repository d’entreprises et un context de repositories

pull/2943/head
Jalil Arfaoui 2024-04-15 15:05:19 +02:00
parent 0cf6f37579
commit 6e4e630d68
7 changed files with 45 additions and 7 deletions

View File

@ -1,10 +1,15 @@
import { codeActivité } from '@/domain/CodeActivite'
import { codeCatégorieJuridique } from '@/domain/CodeCatégorieJuridique'
import { Entreprise } from '@/domain/Entreprise'
import { EntreprisesRepository } from '@/domain/EntreprisesRepository'
import { Établissement } from '@/domain/Établissement'
import { siren, siret } from '@/domain/Siren'
export async function searchDenominationOrSiren(
export const FabriqueSocialEntreprisesRepository: EntreprisesRepository = {
rechercheTexteLibre: searchDenominationOrSiren,
}
async function searchDenominationOrSiren(
searchTerm: string
): Promise<Array<Entreprise> | null> {
return searchFullText(searchTerm).then(

View File

@ -0,0 +1,12 @@
import { createContext } from 'react'
import { FabriqueSocialEntreprisesRepository } from '@/api/RechercheEntreprise/fabrique-social'
import { EntreprisesRepository } from '@/domain/EntreprisesRepository'
interface Repositories {
entreprises: EntreprisesRepository
}
export const RepositoriesContext = createContext<Repositories>({
entreprises: FabriqueSocialEntreprisesRepository,
})

View File

@ -0,0 +1,7 @@
import { Entreprise } from './Entreprise'
export interface EntreprisesRepository {
rechercheTexteLibre: (
termeDeRecherche: string
) => Promise<Array<Entreprise> | null>
}

View File

@ -0,0 +1,7 @@
import { useContext } from 'react'
import { RepositoriesContext } from '@/contexts/RepositoriesContext'
export const useRepositories = () => useContext(RepositoriesContext)
export const useEntreprisesRepository = () => useRepositories().entreprises

View File

@ -1,7 +1,7 @@
import { useEffect, useState } from 'react'
import { searchDenominationOrSiren } from '@/api/fabrique-social'
import { Entreprise } from '@/domain/Entreprise'
import { useEntreprisesRepository } from '@/hooks/useRepositories'
import { useDebounce } from './useDebounce'
@ -11,6 +11,7 @@ export default function useSearchCompany(
const [result, setResult] = useState<Array<Entreprise>>([])
const [searchPending, setSearchPending] = useState(Boolean(value))
const debouncedValue = useDebounce(value, 300)
const entreprisesRepository = useEntreprisesRepository()
useEffect(() => {
setSearchPending(Boolean(value))
@ -24,7 +25,8 @@ export default function useSearchCompany(
return
}
searchDenominationOrSiren(debouncedValue)
entreprisesRepository
.rechercheTexteLibre(debouncedValue)
.then((entreprise: Array<Entreprise> | null) => {
setResult(entreprise || [])
setSearchPending(false)

View File

@ -3,7 +3,6 @@ import { Trans, useTranslation } from 'react-i18next'
import { useDispatch } from 'react-redux'
import { generatePath, useNavigate } from 'react-router-dom'
import { searchDenominationOrSiren } from '@/api/fabrique-social'
import { CompanyDetails } from '@/components/company/Details'
import { EntrepriseSearchField } from '@/components/company/SearchField'
import { useEngine } from '@/components/utils/EngineContext'
@ -14,6 +13,7 @@ import PopoverConfirm from '@/design-system/popover/PopoverConfirm'
import { H3 } from '@/design-system/typography/heading'
import { Body } from '@/design-system/typography/paragraphs'
import { Entreprise } from '@/domain/Entreprise'
import { useEntreprisesRepository } from '@/hooks/useRepositories'
import { useSetEntreprise } from '@/hooks/useSetEntreprise'
import { useSitePaths } from '@/sitePaths'
import { getCookieValue } from '@/storage/readCookie'
@ -120,9 +120,12 @@ function useSetEntrepriseFromUrssafConnection() {
const setEntreprise = useSetEntreprise()
const siret = siretFromUrssafFrConnection()
const companySIREN = useEngine().evaluate('entreprise . SIREN').nodeValue
const entreprisesRepository = useEntreprisesRepository()
useEffect(() => {
if (siret && !companySIREN) {
searchDenominationOrSiren(siret)
entreprisesRepository
.rechercheTexteLibre(siret)
.then((results) => {
if (results?.length !== 1) {
return

View File

@ -14,7 +14,6 @@ import {
} from 'react-router-dom'
import { styled } from 'styled-components'
import { searchDenominationOrSiren } from '@/api/fabrique-social'
import { TrackPage } from '@/components/ATInternetTracking'
import { CompanyDetails } from '@/components/company/Details'
import RuleInput from '@/components/conversation/RuleInput'
@ -35,6 +34,7 @@ import { H2, H3 } from '@/design-system/typography/heading'
import { Body, Intro } from '@/design-system/typography/paragraphs'
import { Entreprise } from '@/domain/Entreprise'
import { useQuestionList } from '@/hooks/useQuestionList'
import { useEntreprisesRepository } from '@/hooks/useRepositories'
import { useSetEntreprise } from '@/hooks/useSetEntreprise'
import useSimulationConfig from '@/hooks/useSimulationConfig'
import useSimulatorsData from '@/hooks/useSimulatorsData'
@ -401,13 +401,15 @@ const useSirenFromParams = (overwrite: boolean) => {
const [entreprisePending, setEntreprisePending] = useState(false)
const [entrepriseNotFound, setEntrepriseNotFound] = useState(false)
const entreprisesRepository = useEntreprisesRepository()
useEffect(() => {
if (!param || !overwrite) {
return
}
setEntreprisePending(true)
searchDenominationOrSiren(param)
entreprisesRepository
.rechercheTexteLibre(param)
.then((entreprises) => {
setEntreprisePending(false)
if (!entreprises || !entreprises.length) {