Skip to content

Commit 917177b

Browse files
committed
refactor(Context): refactor context and add routing
1 parent e3f2121 commit 917177b

File tree

25 files changed

+479
-35
lines changed

25 files changed

+479
-35
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
"@testing-library/jest-dom": "^4.2.4",
77
"@testing-library/react": "^9.3.2",
88
"@testing-library/user-event": "^7.1.2",
9+
"axios": "^0.19.2",
910
"react": "^16.13.1",
1011
"react-dom": "^16.13.1",
11-
"react-scripts": "3.4.1"
12+
"react-router-dom": "^5.2.0",
13+
"react-scripts": "3.4.1",
14+
"react-toastify": "^6.0.8",
15+
"react-transition-group": "^4.4.1"
1216
},
1317
"scripts": {
1418
"start": "react-scripts start",

src/App.js

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,70 @@
11
import React, { useContext } from "react";
2-
import { store, dispatch, actions } from "./store";
2+
import { TransitionGroup, CSSTransition } from "react-transition-group";
33

4-
function App() {
5-
const { state, dispatch } = useContext(store);
4+
import { store, actions } from "./store";
5+
import { useTrivia, useStore } from "./hooks";
66

7-
const handleReverse = () => {
8-
dispatch(actions.setUserFetching(false));
9-
};
7+
import {
8+
BrowserRouter as Router,
9+
Switch,
10+
} from "react-router-dom";
11+
12+
import { ProtectedRoute, PublicRoute } from "./navigations";
13+
import Dashboard from "./pages/Dashboard";
14+
import Account from "./pages/Account";
15+
import Login from "./pages/Login";
16+
17+
const App = () => {
18+
19+
const { state } = useStore();
20+
21+
const {
22+
authenticationState: { isAuthenticated },
23+
} = state;
24+
25+
const AuthenticatedNavigations = () => (
26+
<TransitionGroup>
27+
<CSSTransition classNames="fade" timeout={3000}>
28+
<Switch>
29+
<ProtectedRoute
30+
path="/dashboard"
31+
component={Dashboard}
32+
isAuthenticated={isAuthenticated}
33+
/>
34+
<ProtectedRoute
35+
path="/account"
36+
component={Account}
37+
isAuthenticated={isAuthenticated}
38+
/>
39+
</Switch>
40+
</CSSTransition>
41+
</TransitionGroup>
42+
);
43+
44+
const PublicNavigations = () => (
45+
<TransitionGroup>
46+
<CSSTransition classNames="fade" timeout={3000}>
47+
<Switch>
48+
<PublicRoute
49+
path="/login"
50+
exact
51+
isAuthenticated={isAuthenticated}
52+
component={Login}
53+
/>
54+
</Switch>
55+
</CSSTransition>
56+
</TransitionGroup>
57+
);
1058

1159
return (
12-
<div className="App">
13-
authenticated:isLoading:
14-
{state.authenticationState.isLoading ? " true\n" : "false\n"}
15-
<br />
16-
<button
17-
onClick={() => {
18-
dispatch(actions.setLoadingLogin());
19-
dispatch(actions.setUserFetching(true));
20-
}}
21-
>
22-
State Management
23-
</button>
24-
<br />
25-
user:isFetching: {state.userState.isFetching ? " true\n" : "false\n"}
26-
<br />
27-
<button onClick={handleReverse}>Reverse fetching</button>
28-
</div>
60+
<>
61+
<>{JSON.stringify(state)}</>
62+
<Router>
63+
<AuthenticatedNavigations />
64+
<PublicNavigations />
65+
</Router>
66+
</>
2967
);
30-
}
68+
};
3169

3270
export default App;

src/hooks/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { useTrivia } from './useTrivia'
2+
export { useStore } from './useStore'

src/hooks/useStore.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useContext } from "react";
2+
import { store, actions } from "../store";
3+
4+
export const useStore = () => {
5+
const { state, dispatch } = useContext(store);
6+
const dispatchAction = (action) => dispatch(action);
7+
8+
return { state, dispatchAction, actions };
9+
};

src/hooks/useTrivia.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useState, useEffect } from "react";
2+
import { Axios } from "../utils";
3+
import { toast } from "react-toastify";
4+
5+
6+
export const useTrivia = (initialData = []) => {
7+
const [trivias, setTrivias] = useState(initialData);
8+
9+
useEffect(() => {
10+
async function fetchTrivias() {
11+
try {
12+
const { data } = await Axios.get("", {
13+
params: {
14+
amount: 15,
15+
},
16+
});
17+
18+
if (data) {
19+
setTrivias(data.result);
20+
}
21+
} catch (error) {
22+
setTrivias([]);
23+
toast.error(error.message)
24+
}
25+
}
26+
27+
fetchTrivias();
28+
}, []);
29+
30+
return [trivias, setTrivias];
31+
};

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3-
import App from "./App";
3+
import { toast } from 'react-toastify';
44
import * as serviceWorker from "./serviceWorker";
5+
import App from "./App";
56
import { GlobalStateProvider } from "./store";
7+
import 'react-toastify/dist/ReactToastify.css';
8+
9+
toast.configure({
10+
position: "top-right",
11+
autoClose: 3000,
12+
});
613

714
ReactDOM.render(
815
<React.StrictMode>

src/navigations/ProtectedRoute.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from "react";
2+
import { Route, Redirect } from "react-router-dom";
3+
4+
export const ProtectedRoute = ({
5+
component: Component,
6+
isAuthenticated = false,
7+
...rest
8+
}) => (
9+
<Route
10+
{...rest}
11+
render={(props) =>
12+
isAuthenticated ? <Component {...props} /> : <Redirect to="/login" />
13+
}
14+
/>
15+
);

src/navigations/PublicRoute.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from "react";
2+
import { Route, Redirect } from "react-router-dom";
3+
4+
export const PublicRoute = ({
5+
component: Component,
6+
isAuthenticated = false,
7+
...rest
8+
}) => (
9+
<Route
10+
{...rest}
11+
render={(props) =>
12+
isAuthenticated ? <Redirect to="/dashboard" /> : <Component {...props} />
13+
}
14+
/>
15+
);

src/navigations/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { ProtectedRoute } from './ProtectedRoute'
2+
export { PublicRoute } from './PublicRoute'

src/pages/Account/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from "react";
2+
3+
const Account = () => {
4+
return <div> account </div>;
5+
};
6+
7+
export default Account;
8+

0 commit comments

Comments
 (0)