Skip to content

Commit 6b04344

Browse files
LuisValgoiMarcusNotheis
authored andcommitted
Merge pull request #31 from LuisValgoi/issue-22
feat: issue#22
1 parent 078d323 commit 6b04344

File tree

5 files changed

+60
-29
lines changed

5 files changed

+60
-29
lines changed

packages/ui5-webcomponents-react-seed/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ The goal of this repository is to increase the boostrap time spent on a new proj
3737

3838
## From a Hook point of view, it includes:
3939

40-
<!-- - `useActions`: Which wraps the redux action. -->
41-
4240
- `useBrowser`: Which identifies the user browser.
4341

4442
- `useIsMobile`: Which identifies the user device perspective.
4543

44+
- `useRequest`: Which includes `get`, `post`, `patch`, `delete`, `put` HTTP helpers.
45+
4646
# Available Scripts
4747

4848
In the project directory, you can run:

packages/ui5-webcomponents-react-seed/src/auth/ComponentValidator.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import React from 'react';
2-
import { useQuery } from 'react-query';
2+
import { useGet } from '../hooks/useRequest';
33

4-
import Request from '../util/Request';
54
import Constants from '../util/Constants';
6-
import UrlProvider from '../util/URLProvider';
7-
8-
const fetchUser = async () => {
9-
const url = UrlProvider.getUrl('GET_USER_LOGGED');
10-
const res = await Request.get(url)
11-
return res.data;
12-
};
135

146
const ComponentValidator = ({ allowedAuthorities, authorityKey, children }) => {
15-
const { data, status } = useQuery('GET_USER_LOGGED', fetchUser);
7+
const { data, status } = useGet(Constants.REACT_QUERY.KEYS.GET_USER_LOGGED, 'GET_USER_LOGGED', null);
168

179
const getRoute = () => {
18-
if (status === Constants.CODES.RQ_SUCCESS) {
10+
if (status === Constants.REACT_QUERY.CODES.SUCCESS) {
1911
const hasAccess = data.data.user[authorityKey].some(permission => allowedAuthorities.includes(permission));
2012
if (hasAccess) {
2113
return children;

packages/ui5-webcomponents-react-seed/src/auth/RouteValidator.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
import React from 'react';
22
import { Route } from "react-router-dom";
3-
import { useQuery } from 'react-query';
3+
import { useGet } from '../hooks/useRequest';
44

5-
import Request from '../util/Request';
6-
import Constants from '../util/Constants';
7-
import UrlProvider from '../util/URLProvider';
85
import NotFound from '../pages/Fallback/NotFound';
9-
106
import Empty from '../pages/Fallback/Empty';
117

12-
const fetchUser = async () => {
13-
const url = UrlProvider.getUrl('GET_USER_LOGGED');
14-
const res = await Request.get(url)
15-
return res.data;
16-
};
8+
import Constants from '../util/Constants';
179

1810
const RouteValidator = ({ allowedAuthorities, authorityKey, path, component, ...props }) => {
19-
const { data, status } = useQuery('GET_USER_LOGGED', fetchUser);
11+
const { data, status } = useGet(Constants.REACT_QUERY.KEYS.GET_USER_LOGGED, 'GET_USER_LOGGED', null);
2012

2113
const getRoute = () => {
22-
if (status === Constants.CODES.RQ_SUCCESS) {
14+
if (status === Constants.REACT_QUERY.CODES.SUCCESS) {
2315
const hasAccess = data.data.user[authorityKey].some(permission => allowedAuthorities.includes(permission));
2416
return <Route path={path} {...props} component={hasAccess ? component : NotFound} />;
2517
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { useQuery } from 'react-query';
2+
3+
import UrlProvider from '../util/URLProvider';
4+
import Request from '../util/Request';
5+
import Constants from '../util/Constants';
6+
7+
function useOperation(reactQueryKey, operation, urlKey, dataParam, config) {
8+
const { data, status } = useQuery(reactQueryKey, async () => {
9+
const url = UrlProvider.getUrl(urlKey);
10+
const res = await Request[operation](url, dataParam, config);
11+
return res.data;
12+
});
13+
14+
return { data, status };
15+
}
16+
17+
export function useGet(reactQueryKey, urlKey, config) {
18+
return useOperation(reactQueryKey, Constants.REQUEST.GET, urlKey, null, config ? config : null);
19+
}
20+
21+
export function usePost(reactQueryKey, urlKey, dataParam, config) {
22+
return useOperation(reactQueryKey, Constants.REQUEST.POST, urlKey, dataParam ? dataParam : null, config ? config : null);
23+
}
24+
25+
export function usePut(reactQueryKey, urlKey, dataParam, config) {
26+
return useOperation(reactQueryKey, Constants.REQUEST.PUT, urlKey, dataParam ? dataParam : null, config ? config : null);
27+
}
28+
29+
export function usePatch(reactQueryKey, urlKey, dataParam, config) {
30+
return useOperation(reactQueryKey, Constants.REQUEST.PATCH, urlKey, dataParam ? dataParam : null, config ? config : null);
31+
}
32+
33+
export function useDelete(reactQueryKey, urlKey, config) {
34+
return useOperation(reactQueryKey, Constants.REQUEST.DELETE, urlKey, null, config ? config : null);
35+
}
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
export default {
2-
CODES: {
3-
RQ_SUCCESS: 'success',
4-
RQ_LOADING: 'loading',
5-
RQ_ERROR: 'error',
2+
REACT_QUERY: {
3+
CODES: {
4+
SUCCESS: 'success',
5+
LOADING: 'loading',
6+
ERROR: 'error',
7+
},
8+
KEYS: {
9+
GET_USER_LOGGED: 'GET_USER_LOGGED'
10+
}
11+
},
12+
REQUEST: {
13+
GET: 'get',
14+
POST: 'get',
15+
PUT: 'put',
16+
PATCH: 'patch',
17+
DELETE: 'delete',
618
}
719
};

0 commit comments

Comments
 (0)