Déplace la méthode setEntreprise dans un hook

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.
pull/1355/head
Maxime Quandalle 2021-01-11 16:20:17 +01:00
parent 41d20f823d
commit 9f32e83a51
2 changed files with 35 additions and 34 deletions

View File

@ -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))
}
}

View File

@ -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 (
<>
<h1>