Ajoute des alias et refactor des hooks

pull/1928/head
Alexandre Valsamou-Stanislawski 2021-12-07 15:30:02 +01:00 committed by Johan Girod
parent aa63dd4511
commit 13a8332d29
7 changed files with 81 additions and 1 deletions

View File

@ -0,0 +1,5 @@
{
"message": "no results found",
"spellcheck": null,
"suggestions": []
}

View File

@ -0,0 +1,16 @@
{
"message": "no results found",
"spellcheck": null,
"suggestions": [
"menoz",
"menozzi*albertini/marie agnes/",
"menozzi*dominique emile/",
"menoz compagnie yado",
"menozzi champagne",
"menozzi cite",
"menozzi holding",
"menozzi industrie",
"menozzi vaise",
"menozzi*amerio/laurence/"
]
}

View File

@ -0,0 +1,22 @@
import { useEffect, useState } from 'react'
export function useDebounce<T>(value: T, delay: number) {
const [debouncedValue, setDebouncedValue] = useState(value)
useEffect(
() => {
// Update debounced value after delay
const handler = setTimeout(() => {
setDebouncedValue(value)
}, delay)
// Cancel the timeout if value changes (also on delay change or unmount)
// This is how we prevent debounced value from updating if value is changed ...
// .. within the delay period. Timeout gets cleared and restarted.
return () => {
clearTimeout(handler)
}
},
[value, delay] // Only re-call effect if value or delay changes
)
return debouncedValue
}

View File

@ -0,0 +1,32 @@
import { Etablissement, searchDenominationOrSiren } from 'api/sirene'
import { useEffect, useState } from 'react'
import { useDebounce } from './useDebounce'
export default function useSearchCompany(
value: string
): [boolean, Array<Etablissement>] {
const [result, setResult] = useState<Array<Etablissement>>([])
const [searchPending, setSearchPending] = useState(!!value)
const debouncedValue = useDebounce(value, 300)
useEffect(() => {
setSearchPending(!!value)
if (!value) {
setResult([])
}
}, [value, setResult, setSearchPending])
useEffect(() => {
if (!debouncedValue) {
return
}
searchDenominationOrSiren(debouncedValue).then((établissements) => {
setResult(établissements || [])
setSearchPending(false)
})
}, [debouncedValue, setResult, setSearchPending])
return [searchPending && result.length <= 0, result.slice(0, 6)]
}

View File

@ -19,7 +19,9 @@
"Selectors/*": ["selectors/*"],
"Types/*": ["types/*"],
"DesignSystem/*": ["design-system/*"],
"Data/*": ["data/*"]
"Data/*": ["data/*"],
"Hooks/*": ["hooks/*"],
"API/*": ["api/*"]
},
"typeRoots": ["./types/", "./node_modules/@types"],
"noEmit": true,

View File

@ -114,6 +114,8 @@ module.exports.default = {
Images: path.resolve('source/static/images/'),
DesignSystem: path.resolve('source/design-system'),
Data: path.resolve('source/data'),
Hooks: path.resolve('source/hooks'),
API: path.resolve('source/api'),
},
extensions: ['.js', '.ts', '.tsx'],
},