Added initial loading setting
Enabled pushing to history on filter setting
This commit is contained in:
parent
9b4ef41030
commit
abe3e64883
@ -19,16 +19,16 @@ type UseFetchErrored<T> = {
|
|||||||
data: undefined
|
data: undefined
|
||||||
} & UseFetchShared<T>
|
} & UseFetchShared<T>
|
||||||
|
|
||||||
const gotError = <T>(res: UseFetchErrored<T> | UseFetchSucced<T>): res is UseFetchErrored<T> => (
|
type UseFetchReturn<T> = UseFetchSucced<T> | UseFetchErrored<T>
|
||||||
|
|
||||||
|
const gotError = <T>(res: UseFetchReturn<T>): res is UseFetchErrored<T> => (
|
||||||
typeof res.error === 'string'
|
typeof res.error === 'string'
|
||||||
)
|
)
|
||||||
|
|
||||||
const fallbackError = <T>(res: UseFetchSucced<T> | UseFetchErrored<T>) => (
|
const fallbackError = <T>(res: UseFetchReturn<T>) => (
|
||||||
gotError(res) ? res.error : res.data
|
gotError(res) ? res.error : res.data
|
||||||
)
|
)
|
||||||
|
|
||||||
type UseFetchReturn<T> = UseFetchSucced<T> | UseFetchErrored<T>
|
|
||||||
|
|
||||||
function useFetch<R, T>(
|
function useFetch<R, T>(
|
||||||
url: string,
|
url: string,
|
||||||
method: RequestInit['method'],
|
method: RequestInit['method'],
|
||||||
@ -40,7 +40,7 @@ function useFetch<R, T>(
|
|||||||
): UseFetchReturn<T> {
|
): UseFetchReturn<T> {
|
||||||
const [data, setData] = useState(initialData)
|
const [data, setData] = useState(initialData)
|
||||||
|
|
||||||
const { doSend, loading, error } = useSend(url, method, needAuth, guardResponse, processResponse, params)
|
const { doSend, loading, error } = useSend(url, method, needAuth, guardResponse, processResponse, true, params)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
doSend().then(
|
doSend().then(
|
||||||
|
@ -8,11 +8,11 @@ function useFilters(initialFilters: FiltersType = defaultFilters): [FiltersType,
|
|||||||
|
|
||||||
const [filters, setFilters] = useState(initialFilters)
|
const [filters, setFilters] = useState(initialFilters)
|
||||||
|
|
||||||
const appendFiltersSearchParams = (filters: FiltersType) => (
|
const appendFiltersSearchParams = (filters: FiltersType, replace = false) => (
|
||||||
setSearchParams(params => ({
|
setSearchParams(params => ({
|
||||||
...Object.fromEntries(params),
|
...Object.fromEntries(params),
|
||||||
...URLEncodeFilters(filters)
|
...URLEncodeFilters(filters)
|
||||||
}), { replace: true })
|
}), { replace })
|
||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -26,7 +26,7 @@ function useFilters(initialFilters: FiltersType = defaultFilters): [FiltersType,
|
|||||||
appendFiltersSearchParams({
|
appendFiltersSearchParams({
|
||||||
...URLEncodeFilters(initialFilters),
|
...URLEncodeFilters(initialFilters),
|
||||||
...URLEncodeFilters(urlFilters),
|
...URLEncodeFilters(urlFilters),
|
||||||
})
|
}, true)
|
||||||
// searchParams have actual query string at first render
|
// searchParams have actual query string at first render
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [])
|
}, [])
|
||||||
|
@ -10,9 +10,10 @@ function useSend<R, T>(
|
|||||||
needAuth: boolean,
|
needAuth: boolean,
|
||||||
guardResponse: (data: unknown) => data is R,
|
guardResponse: (data: unknown) => data is R,
|
||||||
processResponse: (data: R) => T,
|
processResponse: (data: R) => T,
|
||||||
|
startWithLoading = false,
|
||||||
defaultParams?: Omit<RequestInit, 'method'>
|
defaultParams?: Omit<RequestInit, 'method'>
|
||||||
) {
|
) {
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(startWithLoading)
|
||||||
const [error, setError] = useState<string | null>(null)
|
const [error, setError] = useState<string | null>(null)
|
||||||
|
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
@ -15,7 +15,7 @@ const URLEncodeFilters = (filters: FiltersType) => (
|
|||||||
fName => {
|
fName => {
|
||||||
const v = filters[fName]
|
const v = filters[fName]
|
||||||
if (v) {
|
if (v) {
|
||||||
return [fName, encodeURIComponent(v)]
|
return [fName, v.toString()]
|
||||||
}
|
}
|
||||||
return [fName, undefined]
|
return [fName, undefined]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user