1
- import { useState , useCallback , useEffect } from 'react' ;
2
- import { AxiosRequestConfig } from 'axios' ;
1
+ import { useFetch } from '../useFetch' ;
2
+ import { PaginatedQueryPath } from '../usePaginatedQuery/paths' ;
3
+ import { IUseShelterData } from './types' ;
3
4
4
- import { api } from '@/api' ;
5
- import { IServerResponse } from '@/types' ;
6
- import { IPaginatedResponse } from '../usePaginatedQuery/types' ;
7
- import { IUseShelterOptions , IUseSheltersData } from './types' ;
8
-
9
- const useShelters = ( options : IUseShelterOptions = { } ) => {
10
- const { cache } = options ;
11
- const [ loading , setLoading ] = useState < boolean > ( false ) ;
12
- const [ data , setData ] = useState < IPaginatedResponse < IUseSheltersData > > ( {
13
- count : 0 ,
14
- page : 1 ,
15
- perPage : 20 ,
16
- results : [ ] ,
17
- } ) ;
18
-
19
- const refresh = useCallback (
20
- ( config : AxiosRequestConfig < any > = { } , append : boolean = false ) => {
21
- const { search, ...rest } = ( config ?? { } ) . params ?? { } ;
22
- const headers = config . headers ?? { } ;
23
- if ( cache ) headers [ 'x-app-cache' ] = 'true' ;
24
- if ( ! append ) setLoading ( true ) ;
25
- api
26
- . get < IServerResponse < any > > ( '/shelters' , {
27
- ...config ,
28
- headers,
29
- params : {
30
- < << << << HEAD
31
- orderBy : 'updatedAt' ,
32
- = === ===
33
- orderBy : 'prioritySum' ,
34
- > >>> >>> 3 d3f437 ( merge : develop - > master ( #91 ) )
35
- order : 'desc' ,
36
- search :
37
- search ?? new URLSearchParams ( window . location . search ) . toString ( ) ,
38
- ...rest ,
39
- } ,
40
- } )
41
- . then ( ( { data } ) => {
42
- if ( append ) {
43
- setData ( ( prev ) => ( {
44
- ...prev ,
45
- ...data . data ,
46
- results : [ ...prev . results , ...data . data . results ] ,
47
- } ) ) ;
48
- } else {
49
- setData ( ( prev ) => ( {
50
- ...prev ,
51
- ...data . data ,
52
- results : [ ...data . data . results ] ,
53
- } ) ) ;
54
- }
55
- } )
56
- . finally ( ( ) => {
57
- if ( ! append ) setLoading ( false ) ;
58
- } ) ;
59
- } ,
60
- [ ]
5
+ const useShelter = ( shelterId : string ) => {
6
+ return useFetch < IUseShelterData > (
7
+ `${ PaginatedQueryPath . Shelters } /${ shelterId } `
61
8
) ;
62
-
63
- useEffect ( ( ) => {
64
- refresh ( ) ;
65
- } , [ refresh ] ) ;
66
-
67
- return { data, loading, refresh } ;
68
9
} ;
69
10
70
- export { useShelters } ;
11
+ export { useShelter } ;
0 commit comments