Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bodysch-frontend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
JetBrains YouTrack
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
KSZK
DevTeam
org
BodySCH
bodysch-frontend
Commits
6c8866e5
Commit
6c8866e5
authored
4 years ago
by
Chif Gergő
Browse files
Options
Downloads
Patches
Plain Diff
Create useRequest hook and type definitions
parent
10077ee7
Branches
Branches containing commit
No related tags found
1 merge request
!7
update master to dev
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
.eslintrc.js
+4
-0
4 additions, 0 deletions
.eslintrc.js
src/hooks/types.ts
+28
-0
28 additions, 0 deletions
src/hooks/types.ts
src/hooks/useRequest.ts
+44
-0
44 additions, 0 deletions
src/hooks/useRequest.ts
with
76 additions
and
0 deletions
.eslintrc.js
+
4
−
0
View file @
6c8866e5
...
...
@@ -11,4 +11,8 @@ module.exports = {
env
:
{
browser
:
true
,
},
rules
:
{
'
implicit-arrow-linebreak
'
:
0
,
},
};
This diff is collapsed.
Click to expand it.
src/hooks/types.ts
0 → 100644
+
28
−
0
View file @
6c8866e5
import
{
AxiosPromise
}
from
'
axios
'
;
export
interface
ResponseData
<
T
>
{
isLoading
:
boolean
;
isError
:
boolean
;
data
?:
T
;
}
export
interface
RequestArgs
<
T
=
any
>
{
request
:
ApiRequest
<
T
>
;
initialData
?:
T
;
initialParams
?:
RequestParams
;
}
export
interface
RequestParams
{
body
?:
any
;
params
?:
any
;
args
?:
any
;
}
export
type
ApiRequest
<
T
>
=
(
params
:
RequestParams
)
=>
AxiosPromise
<
T
>
;
export
type
Refetch
=
(
params
?:
RequestParams
)
=>
void
;
export
interface
INews
{
title
:
string
;
text
:
string
;
publishedAt
:
string
;
}
This diff is collapsed.
Click to expand it.
src/hooks/useRequest.ts
0 → 100644
+
44
−
0
View file @
6c8866e5
import
{
useCallback
,
useEffect
,
useState
}
from
'
react
'
;
import
{
Refetch
,
RequestArgs
,
ResponseData
}
from
'
./types
'
;
function
useRequest
<
T
=
any
>
({
request
,
initialData
,
initialParams
,
}:
RequestArgs
<
T
>
):
[
ResponseData
<
T
>
,
Refetch
]
{
const
[
data
,
setData
]
=
useState
(
initialData
);
const
[
isLoading
,
setIsLoading
]
=
useState
(
false
);
const
[
isError
,
setIsError
]
=
useState
(
false
);
const
[
fetching
,
setFetching
]
=
useState
(
false
);
const
[
requestParams
,
setRequestParams
]
=
useState
(
initialParams
??
{});
const
refetch
=
useCallback
((
newParams
?)
=>
{
setRequestParams
(
newParams
??
{});
setFetching
(
true
);
},
[]);
useEffect
(()
=>
{
const
fetchData
=
async
()
=>
{
setIsError
(
false
);
setIsLoading
(
true
);
try
{
const
result
=
await
request
(
requestParams
);
setData
(
result
.
data
);
}
catch
(
error
)
{
setIsError
(
true
);
}
setFetching
(
false
);
setIsLoading
(
false
);
};
if
(
fetching
&&
!
isLoading
)
{
fetchData
();
}
},
[
request
,
fetching
,
isLoading
,
requestParams
]);
return
[{
data
,
isLoading
,
isError
},
refetch
];
}
export
default
useRequest
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment