瀏覽代碼

redux toolkit

amenpunk 2 年之前
父節點
當前提交
1071f910d1
共有 8 個文件被更改,包括 46 次插入35 次删除
  1. 1 0
      package.json
  2. 0 9
      src/Actions/index.js
  3. 0 1
      src/Actions/types.js
  4. 7 7
      src/Pages/Logincs.jsx
  5. 8 0
      src/Reducers/index.js
  6. 0 12
      src/Reducers/token.js
  7. 21 0
      src/Slices/tokenSlice.js
  8. 9 6
      src/index.js

+ 1 - 0
package.json

@@ -12,6 +12,7 @@
12 12
         "@mui/icons-material": "^5.1.0",
13 13
         "@mui/lab": "^5.0.0-alpha.59",
14 14
         "@mui/material": "^5.1.0",
15
+        "@reduxjs/toolkit": "^1.8.6",
15 16
         "@testing-library/jest-dom": "^5.16.1",
16 17
         "@testing-library/react": "^11.2.7",
17 18
         "@testing-library/react-hooks": "^7.0.2",

+ 0 - 9
src/Actions/index.js

@@ -1,9 +0,0 @@
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
-})

+ 0 - 1
src/Actions/types.js

@@ -1 +0,0 @@
1
-export const SET_TOKEN = 'SET_TOKEN';

+ 7 - 7
src/Pages/Logincs.jsx

@@ -2,10 +2,9 @@ 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
+import { useSelector, useDispatch } from "react-redux";
6
+
7
+import { setToken } from '../Slices/tokenSlice'
9 8
 
10 9
 import {
11 10
   Paper, Box, Grid, Typography, TextField, Button, Avatar,
@@ -22,7 +21,7 @@ import { useFormik } from 'formik';
22 21
 import * as Yup from 'yup';
23 22
 
24 23
 import { Service } from '../Utils/HTTP.js'
25
-import { setToken } from '../Actions/index.js';
24
+// import { setToken } from '../Actions/index.js';
26 25
 
27 26
 const LoginSchema = Yup.object().shape({
28 27
   email: Yup
@@ -43,7 +42,7 @@ export function LoginCs() {
43 42
   let navigate = useNavigate()
44 43
 
45 44
   const [open, setOpen] = React.useState(false);
46
-  // const token = useSelector(state => state.token)
45
+  const token = useSelector(state => state.token)
47 46
   const dispatch = useDispatch();
48 47
   const handleClose = () => false
49 48
 
@@ -71,7 +70,7 @@ export function LoginCs() {
71 70
           let { token, nombre, apelidos, id: pass_id } = response;
72 71
           toast.success(`Bienvenido ${nombre} ${apelidos}!!`)
73 72
           token = token.replace("Bearer ", "")
74
-          console.log(token);
73
+          // console.log(token);
75 74
 
76 75
           // let { exp } = jwt_decode(token);
77 76
           let body_token = jwt_decode(token);
@@ -108,6 +107,7 @@ export function LoginCs() {
108 107
   });
109 108
 
110 109
   React.useEffect(() => {
110
+    console.log('TOKEN: ', token)
111 111
     if (auth.isLogged()) {
112 112
       return navigate('/user/home')
113 113
     }

+ 8 - 0
src/Reducers/index.js

@@ -0,0 +1,8 @@
1
+import { combineReducers } from 'redux'
2
+import tokenReducer from '../Slices/tokenSlice.js';
3
+
4
+const rootReducer = combineReducers({
5
+  token : tokenReducer
6
+});
7
+
8
+export default rootReducer;

+ 0 - 12
src/Reducers/token.js

@@ -1,12 +0,0 @@
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
-}

+ 21 - 0
src/Slices/tokenSlice.js

@@ -0,0 +1,21 @@
1
+import { createSlice } from '@reduxjs/toolkit';
2
+
3
+const initialState = { token: null };
4
+
5
+export const tokenSlice = createSlice({
6
+  name: "token",
7
+  initialState,
8
+  reducers: {
9
+    setToken: (state, action) => {
10
+      state.token = action.payload;
11
+    },
12
+    removeToken: (state, _action) => {
13
+      state.token = null;
14
+    }
15
+  }
16
+})
17
+
18
+export const { setToken, removeToken } = tokenSlice.actions;
19
+console.log(tokenSlice)
20
+
21
+export default tokenSlice.reducer;

+ 9 - 6
src/index.js

@@ -3,18 +3,21 @@ 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';
6
+import rootReducer from '../src/Reducers/';
7 7
 import { Provider } from 'react-redux';
8 8
 import { legacy_createStore as createStore } from 'redux';
9 9
 
10
-const store = createStore(TokenReducer)
10
+const store =
11
+  createStore(rootReducer,
12
+    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
13
+  )
11 14
 
12 15
 ReactDOM.render(
13
-  <React.StrictMode>
14
-    <Provider store={store}>
16
+  <Provider store={store}>
17
+    <React.StrictMode>
15 18
       <App />
16
-    </Provider>
17
-  </React.StrictMode>,
19
+    </React.StrictMode>
20
+  </Provider>,
18 21
   document.getElementById('root')
19 22
 );
20 23