diff --git a/mon-entreprise/source/actions/companyStatusActions.ts b/mon-entreprise/source/actions/companyStatusActions.ts index 79714bcdf..717cc0401 100644 --- a/mon-entreprise/source/actions/companyStatusActions.ts +++ b/mon-entreprise/source/actions/companyStatusActions.ts @@ -1,8 +1,15 @@ +import { fetchCompanyDetails } from '../api/sirene' +import { ApiCommuneJson } from 'Components/conversation/select/SelectCommune' import { useEffect, useState } from 'react' import { useDispatch } from 'react-redux' import { useHistory } from 'react-router' import { useNextQuestionUrl } from 'Selectors/companyStatusSelectors' import { Action } from './actions' +import { + addCommuneDetails, + setCompanyDetails, + setSiren, +} from './existingCompanyActions' export type CompanyStatusAction = ReturnType< | typeof isSoleProprietorship @@ -67,3 +74,28 @@ export const resetCompanyStatusChoice = (answersToReset?: string[]) => type: 'RESET_COMPANY_STATUS_CHOICE', answersToReset, } as const) + +const fetchCommuneDetails = async function (codeCommune: string) { + const response = await fetch( + `https://geo.api.gouv.fr/communes/${codeCommune}?fields=departement,region` + ) + return await response.json() +} + +export const useSetEntreprise = () => { + const dispatch = useDispatch() + return async (siren: string) => { + dispatch(setSiren(siren)) + const companyDetails = await fetchCompanyDetails(siren) + dispatch( + setCompanyDetails( + companyDetails.categorie_juridique, + companyDetails.date_creation + ) + ) + const communeDetails: ApiCommuneJson = await fetchCommuneDetails( + companyDetails.etablissement_siege.code_commune + ) + dispatch(addCommuneDetails(communeDetails)) + } +} diff --git a/mon-entreprise/source/components/FindCompany.tsx b/mon-entreprise/source/components/FindCompany.tsx index 5e82a4fd2..910d32dcc 100644 --- a/mon-entreprise/source/components/FindCompany.tsx +++ b/mon-entreprise/source/components/FindCompany.tsx @@ -1,26 +1,9 @@ -import { - addCommuneDetails, - setCompanyDetails, - setSiren, -} from 'Actions/existingCompanyActions' +import { useSetEntreprise } from 'Actions/companyStatusActions' import CompanyDetails from 'Components/CompanyDetails' import { useCallback, useMemo, useState } from 'react' import { Trans } from 'react-i18next' -import { useDispatch } from 'react-redux' -import { - Etablissement, - fetchCompanyDetails, - searchDenominationOrSiren, -} from '../api/sirene' +import { Etablissement, searchDenominationOrSiren } from '../api/sirene' import { debounce } from '../utils' -import { ApiCommuneJson } from './conversation/select/SelectCommune' - -const fetchCommuneDetails = async function (codeCommune: string) { - const response = await fetch( - `https://geo.api.gouv.fr/communes/${codeCommune}?fields=departement,region` - ) - return await response.json() -} export default function Search() { const [ @@ -41,22 +24,8 @@ export default function Search() { const debouncedHandleSearch = useMemo(() => debounce(300, handleSearch), [ handleSearch, ]) - const dispatch = useDispatch() + const setEntreprise = useSetEntreprise() - const setEntreprise = async (siren: string) => { - dispatch(setSiren(siren)) - const companyDetails = await fetchCompanyDetails(siren) - dispatch( - setCompanyDetails( - companyDetails.categorie_juridique, - companyDetails.date_creation - ) - ) - const communeDetails: ApiCommuneJson = await fetchCommuneDetails( - companyDetails.etablissement_siege.code_commune - ) - dispatch(addCommuneDetails(communeDetails)) - } return ( <>