Przeglądaj źródła

implement redux

amenpunk 2 lat temu
rodzic
commit
09efcdeeed
6 zmienionych plików z 73 dodań i 34 usunięć
  1. 9 0
      src/Actions/index.js
  2. 25 26
      src/App.js
  3. 3 0
      src/Pages/HomeUser.jsx
  4. 12 4
      src/Pages/Logincs.jsx
  5. 12 0
      src/Reducers/token.js
  6. 12 4
      src/index.js

+ 9 - 0
src/Actions/index.js

@@ -0,0 +1,9 @@
1
+import { SET_TOKEN } from "./types";
2
+
3
+// NOTE:
4
+// lista de acciones -> as reducer
5
+
6
+export const setToken = (payload) =>  ({
7
+  type : SET_TOKEN,
8
+  payload
9
+})

+ 25 - 26
src/App.js

@@ -8,39 +8,38 @@ import { AuthProvider } from './Auth/AuthProvider'
8 8
 import { QueryClient, QueryClientProvider } from 'react-query'
9 9
 
10 10
 import Routes from './Components/Routes'
11
+
11 12
 const queryClient = new QueryClient({
12
-    defaultOptions: {
13
-        queries: {
14
-            refetchOnWindowFocus: false,
15
-        },
13
+  defaultOptions: {
14
+    queries: {
15
+      refetchOnWindowFocus: false,
16 16
     },
17
+  },
17 18
 })
18 19
 
19 20
 const GATEWAY = {
20
-    dev: {
21
-        API: 'http://localhost:8000',
22
-    },
23
-    pro: {
24
-        API: '',
25
-    }
21
+  dev: {
22
+    API: 'http://localhost:8000',
23
+  },
24
+  pro: {
25
+    API: '',
26
+  }
26 27
 }
27 28
 
28 29
 export const API_GATEWAY = createContext(GATEWAY)
29 30
 
30
-function App() {
31
-    return (
32
-        <QueryClientProvider client={queryClient}>
33
-            <Router>
34
-                <AuthProvider>
35
-                    <API_GATEWAY.Provider value={GATEWAY.dev}>
36
-                        <Container fluid>
37
-                            <Routes />
38
-                        </Container>
39
-                    </API_GATEWAY.Provider>
40
-                </AuthProvider>
41
-            </Router>
42
-        </QueryClientProvider>
43
-    );
31
+export default function App() {
32
+  return (
33
+    <QueryClientProvider client={queryClient}>
34
+      <Router>
35
+        <AuthProvider>
36
+          <API_GATEWAY.Provider value={GATEWAY.dev}>
37
+            <Container fluid>
38
+              <Routes />
39
+            </Container>
40
+          </API_GATEWAY.Provider>
41
+        </AuthProvider>
42
+      </Router>
43
+    </QueryClientProvider>
44
+  );
44 45
 }
45
-
46
-export default App;

+ 3 - 0
src/Pages/HomeUser.jsx

@@ -5,6 +5,7 @@ import useAuth from '../Auth/useAuth.js'
5 5
 import { Service } from '../Utils/HTTP.js';
6 6
 
7 7
 import { TestCard } from '../Components/HomeUser/TestCard';
8
+import { useSelector } from 'react-redux';
8 9
 
9 10
 export function HomeUser(){
10 11
 
@@ -12,6 +13,8 @@ export function HomeUser(){
12 13
     const token = useRef(auth.getToken());
13 14
     const [tests, setTests] = useState([]);
14 15
     const [password, setPasword] = useState({});
16
+    const R_token = useSelector( state => state.token);
17
+    console.log("Redux Token: ", R_token)
15 18
 
16 19
     useEffect(() => {
17 20
 

+ 12 - 4
src/Pages/Logincs.jsx

@@ -2,25 +2,27 @@ import * as React from 'react';
2 2
 import toast, { Toaster } from 'react-hot-toast';
3 3
 import { useNavigate } from 'react-router-dom'
4 4
 import jwt_decode from "jwt-decode";
5
+import { 
6
+  // useSelector,
7
+  useDispatch } from "react-redux";
8
+// import { connect } from 'react-redux';
5 9
 
6 10
 import {
7
-  Paper, Box, Grid, Typography,
8
-  TextField, Button, Avatar,
11
+  Paper, Box, Grid, Typography, TextField, Button, Avatar,
9 12
   Backdrop, CircularProgress,
10 13
 } from '@mui/material'
11 14
 
12 15
 import { createTheme, ThemeProvider } from '@mui/material/styles';
13 16
 
14 17
 import PersonIcon from '@mui/icons-material/Person';
15
-// import { useNavigate } from 'react-router-dom'
16 18
 import { Copyright } from '../Components/Footer.js'
17
-// import { Link } from 'react-router-dom'
18 19
 import useAuth from '../Auth/useAuth';
19 20
 
20 21
 import { useFormik } from 'formik';
21 22
 import * as Yup from 'yup';
22 23
 
23 24
 import { Service } from '../Utils/HTTP.js'
25
+import { setToken } from '../Actions/index.js';
24 26
 
25 27
 const LoginSchema = Yup.object().shape({
26 28
   email: Yup
@@ -41,6 +43,8 @@ export function LoginCs() {
41 43
   let navigate = useNavigate()
42 44
 
43 45
   const [open, setOpen] = React.useState(false);
46
+  // const token = useSelector(state => state.token)
47
+  const dispatch = useDispatch();
44 48
   const handleClose = () => false
45 49
 
46 50
   const formik = useFormik({
@@ -74,6 +78,7 @@ export function LoginCs() {
74 78
           body_token['pass'] = password;
75 79
           body_token['passid'] = pass_id;
76 80
           console.log("BODY TOKEN: ", body_token)
81
+          dispatch(setToken(token))
77 82
           // let timestamp = exp * 1000;
78 83
           // let restante = timestamp - Date.now();
79 84
 
@@ -190,3 +195,6 @@ export function LoginCs() {
190 195
 
191 196
   );
192 197
 }
198
+
199
+
200
+// export default connect(mapStateToProps, mapDispatchToProps)(LoginCs);

+ 12 - 0
src/Reducers/token.js

@@ -0,0 +1,12 @@
1
+import { SET_TOKEN } from "../Actions/types"
2
+
3
+const initialState = { token : null }
4
+
5
+export const TokenReducer  = (state = initialState, action) => {
6
+  switch(action.type){
7
+    case SET_TOKEN:
8
+      return {...state, token: action.payload }
9
+    default:
10
+    return state;
11
+  }
12
+}

+ 12 - 4
src/index.js

@@ -3,10 +3,18 @@ import ReactDOM from 'react-dom';
3 3
 import './index.css';
4 4
 import App from './App';
5 5
 
6
+import { TokenReducer  } from '../src/Reducers/token.js';
7
+import { Provider } from 'react-redux';
8
+import { legacy_createStore as createStore } from 'redux';
9
+
10
+const store = createStore(TokenReducer)
11
+
6 12
 ReactDOM.render(
7
-    <React.StrictMode>
8
-        <App />
9
-    </React.StrictMode>,
10
-    document.getElementById('root')
13
+  <React.StrictMode>
14
+    <Provider store={store}>
15
+      <App />
16
+    </Provider>
17
+  </React.StrictMode>,
18
+  document.getElementById('root')
11 19
 );
12 20