30 lines
703 B
TypeScript
30 lines
703 B
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
import { useAppContext } from '../../contexts/AppContext'
|
|
|
|
type Config = {
|
|
mode: 'list'
|
|
}
|
|
|
|
export const useGet = <T>(path: string, config?: Config) => {
|
|
const appContext = useRef(useAppContext())
|
|
const [data, setData] = useState<T | null>(null)
|
|
|
|
const get = useCallback(async () => {
|
|
const data = await appContext.current.get<T>(path)
|
|
if (!data) return
|
|
if (config?.mode === 'list') {
|
|
// @ts-ignore
|
|
setData(data.results)
|
|
} else {
|
|
setData(data)
|
|
}
|
|
}, [path, config])
|
|
|
|
useEffect(() => {
|
|
get()
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [])
|
|
|
|
return { data, get }
|
|
}
|