From 9f32e83a514fe1e3b8317df75ad41e95edb701a7 Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Mon, 11 Jan 2021 16:20:17 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9place=20la=20m=C3=A9thode=20setEntrepris?= =?UTF-8?q?e=20dans=20un=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit L'idée est de regrouper la logique métier dans un fichier unique séparé des vues. Il reste un peu de travail pour généraliser ce principe. --- .../source/actions/companyStatusActions.ts | 32 ++++++++++++++++ .../source/components/FindCompany.tsx | 37 ++----------------- 2 files changed, 35 insertions(+), 34 deletions(-) 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 ( <>