Ata 3 роки тому
батько
коміт
8c711b9a58

Різницю між файлами не показано, бо вона завелика
+ 1 - 1
final-modul-react/.eslintcache


+ 0 - 5
final-modul-react/src/constants/messages.js

@@ -1,5 +0,0 @@
-export const alertConstants = {
-    SUCCESS: 'ALERT_SUCCESS',
-    ERROR: 'ALERT_ERROR',
-    CLEAR: 'ALERT_CLEAR'
-};

+ 6 - 12
final-modul-react/src/constants/users.js

@@ -1,19 +1,13 @@
 export const userConstants = {
+
+    INIT: 'INIT',
+
     REGISTER_REQUEST: 'REGISTER_REQUEST',
-    REGISTER_SUCCESS: 'REGISTER_SUCCESS',
-    REGISTER_FAILURE: 'REGISTER_FAILURE',
+ 
 
     LOGIN_REQUEST: 'LOGIN_REQUEST',
-    LOGIN_SUCCESS: 'LOGIN_SUCCESS',
-    LOGIN_FAILURE: 'LOGIN_FAILURE',
+   
 
     LOGOUT: 'LOGOUT',
-
-    GETALL_REQUEST: 'USERS_GETALL_REQUEST',
-    GETALL_SUCCESS: 'USERS_GETALL_SUCCESS',
-    GETALL_FAILURE: 'USERS_GETALL_FAILURE',
-
-    DELETE_REQUEST: 'USERS_DELETE_REQUEST',
-    DELETE_SUCCESS: 'USERS_DELETE_SUCCESS',
-    DELETE_FAILURE: 'USERS_DELETE_FAILURE'    
+  
 };

+ 7 - 8
final-modul-react/src/container/Login/Login.jsx

@@ -1,4 +1,5 @@
 import './style.css';
+import { userConstants } from '../../constants/users';
 import React from 'react';
 import {connect} from 'react-redux';
 import {authApi, usersApi} from '../../api'; 
@@ -8,14 +9,15 @@ import withAuthWrapper from '../../components/withAuthWrapper';
 
 const mapDispatchToProps = dispatch => {
     return {
-        loginDispatch: token => dispatch({type: 'LOGIN', payload: token}),
-        initDispatch: currentUser => dispatch({type: 'INIT', payload: currentUser}),
+        loginDispatch: token => dispatch({type: userConstants.LOGIN_REQUEST, payload: token}),
+        initDispatch: currentUser => dispatch({type: userConstants.INIT, payload: currentUser}),
+
         startLoader: () => dispatch({type: 'LOADER_START'}),
         stopLoader: () => dispatch({type: 'LOADER_STOP'}),
     }
 }
 
-const Login = ({loginDispatch, initDispatch, startLoader, stopLoader, history}) => {
+const Login = ({loginDispatch, initDispatch, startLoader, failLogin, stopLoader, history}) => {
     const handleSubmit = async (e) => {
         startLoader()
         e.preventDefault();
@@ -32,12 +34,9 @@ const Login = ({loginDispatch, initDispatch, startLoader, stopLoader, history})
             initDispatch(currentUser)
 
         } catch(err) {
-          
-            console.log(err)
-            return (
             
-                <div> You are {err}. Go to <Link to='auth/registration'>Sign Up</Link></div>
-            )
+            alert(`Oops, something went wrong!!!   ${err.response.data} . `)
+            console.log(err.response.data)
 
         } finally  {
             stopLoader()

+ 20 - 13
final-modul-react/src/container/Registration/Registration.jsx

@@ -4,12 +4,12 @@ import { connect } from 'react-redux';
 import {authApi} from '../../api'; 
 import {Link, withRouter} from 'react-router-dom';
 import withAuthWrapper from '../../components/withAuthWrapper';
-
+import { userConstants } from '../../constants/users';
 
 const mapDispatchToProps = dispatch => {
     return {
     
-        regist: id => dispatch({type: 'REGIST', payload: id}),
+        regist: id => dispatch({type: userConstants.REGISTER_REQUEST, payload: id}),
       
     }
 }
@@ -18,7 +18,7 @@ const mapStateToProps = ({reg}) => {
     return {reg}
   }
 const Registration = ({ regist, history}) => {
-    const handleReg =(e)=>{
+    const handleReg = async (e)=>{
         e.preventDefault()
     const {login, email, password} = e.target.elements;
     const body = {
@@ -26,17 +26,24 @@ const Registration = ({ regist, history}) => {
         email: email.value,
         password: password.value
     }
-   debugger;
-    authApi.registration(body)
-    .then(res=> {
-        console.log(res.id);
-        debugger;
-        regist(res.id)
-        history.push('/auth/login')
-    })
+
+    try {
+        const {id } = await authApi.registration(body)
+        regist(id)
+        
+
+    } catch(err) {
+        const error = err.response.data
+        alert(`Oops, something went wrong!!!  ${error} . `)
+        console.log(error)
+
+    } finally  {
        
-    .catch(e=> console.log(e))
+        history.push('/auth/login')
+    }
 
+   
+    
     
     }
     
@@ -54,5 +61,5 @@ const Registration = ({ regist, history}) => {
     </form>
     );
 }
-const withRegistration = withAuthWrapper(Registration)
+
 export default  withAuthWrapper(connect(mapStateToProps, mapDispatchToProps)(withRouter(Registration)));

+ 6 - 3
final-modul-react/src/store/auth/reducer.js

@@ -1,3 +1,5 @@
+import { userConstants } from '../../constants/users';
+
 const initialState = {
     isAuth: localStorage.getItem('token')? true : false,
     token: localStorage.getItem('token'),
@@ -6,20 +8,21 @@ const initialState = {
 
 export const authReducer = (state=initialState, action) => {
     switch (action.type) {
-        case 'INIT':
+        case userConstants.INIT:
             return {
                 ...state,
                 isAuth: true,
                 currentUser: action.payload
             }    
-        case 'LOGIN':
+        case userConstants.LOGIN_REQUEST:
             localStorage.setItem('token', action.payload)
             return {
                 ...state,
                 isAuth: true,
                 token: action.payload
             }
-        case 'LOGOUT':
+   
+        case userConstants.LOGOUT:
             localStorage.removeItem('token');
             return {
                 ...state,

+ 1 - 0
final-modul-react/src/store/auth/thunks.js

@@ -1,6 +1,7 @@
 import {usersApi} from '../../api'
 
 
+
 export const getCurrentUserThunk = () => {
     return async (dispatch, getState) => {
         try {

+ 2 - 0
final-modul-react/src/store/index.js

@@ -6,10 +6,12 @@ import {authReducer} from './auth/reducer';
 import {uiReducer} from './ui/reducer';
 import { initThunk } from './thunks';
 
+
 const rootReducer = combineReducers({
     auth: authReducer,
     ui: uiReducer,
     reg: regReducer,
+   
 })
 
 const middlewares = applyMiddleware(thunk)

+ 6 - 4
final-modul-react/src/store/regist/reducer.js

@@ -1,5 +1,4 @@
-
-
+import { userConstants } from '../../constants/users';
 const initialState = {
     isRegist: localStorage.getItem('id') ? true : false,
     id: localStorage.getItem('id') || null,
@@ -8,14 +7,17 @@ const initialState = {
 
 export const regReducer = (state=initialState, action) => {
     switch (action.type) {
-        case 'REGIST':
+        case userConstants.REGISTER_REQUEST:
             localStorage.setItem('id', action.payload)
             return {
                 ...state,
                 isRegist: true,
                 id: action.payload
             }    
-        
+        case userConstants.REGISTER_SUCCESS:
+                return {};
+        case userConstants.REGISTER_FAILURE:
+                return {};
         default:
             return state    
     }