Explorar el Código

create request post login

JessyBlue hace 5 años
padre
commit
09bd40168d

+ 5 - 1
src/actionTypes/actionTypes.js

@@ -10,4 +10,8 @@ export const REMOVE_EVENT_REQUEST = "REMOVE_EVENT_REQUEST";
 export const REMOVE_EVENT_REQUEST_SUCCESS = "REMOVE_EVENT_REQUEST_SUCCESS";
 export const REMOVE_EVENT_REQUEST_FAIL = "REMOVE_EVENT_REQUEST_FAIL";
 
-export const SHOW_SIDEBAR = "SHOW_SIDEBAR";
+export const SHOW_SIDEBAR = "SHOW_SIDEBAR";
+
+export const POST_REQUEST_LOGIN = "POST_REQUEST_LOGIN";
+export const POST_REQUEST_SUCCESS_LOGIN = "POST_REQUEST_SUCCESS_LOGIN";
+export const POST_REQUEST_ERROR_LOGIN = "POST_REQUEST_ERROR_LOGIN";

+ 37 - 0
src/actions/login.js

@@ -0,0 +1,37 @@
+import * as types from "../actionTypes/actionTypes";
+
+export const postLogin = payload => ({
+    type: types.POST_REQUEST_LOGIN,
+    payload
+});
+
+export const postLoginSuccess = payload => ({
+    type: types.POST_REQUEST_SUCCESS_LOGIN,
+    payload
+});
+
+export const postLoginError = payload => ({
+    type: types.POST_REQUEST_ERROR_LOGIN,
+    payload
+});
+
+export const postLoginSubmit = payload => {
+    return dispatch => {
+        let promise = fetch("https://api-marathon.herokuapp.com/api/v1/auth/login",
+            {
+                method: 'POST',
+                body: JSON.stringify(payload),
+                headers: {
+                    "Content-type": "application/json"
+                }
+            }
+        )
+
+        dispatch(postLogin())
+
+        promise.then(
+            data => data.json().then(data => dispatch(postLoginSuccess(data))),
+            error => dispatch(postLoginError(error))
+        )
+    }
+}

+ 3 - 2
src/components/login-form/LoginForm.js

@@ -4,10 +4,11 @@ import { Field, reduxForm } from 'redux-form';
 import './login.scss';
 
 let LoginForm = props => {
-    const { handleSubmit } = props;
+    const { handleSubmit, postLoginSubmit } = props;
 
     const submit = value => {
-        console.log(value)
+        //console.log(value);
+        postLoginSubmit(value);
     };
 
     return (

+ 17 - 3
src/conteiners/login/Login.js

@@ -1,9 +1,23 @@
 import React from 'react';
+import { connect } from "react-redux";
+
+import { postLoginSubmit } from "../../actions/login";
 
 import LoginForm from '../../components/login-form/LoginForm';
 
-export default class Login extends React.Component {
+export class Login extends React.Component {
     render() {
-        return ( <LoginForm/> )
+        return ( <LoginForm postLoginSubmit={ this.props.postLoginSubmit }/> )
     }
-}
+}
+
+const mapStateToProps = state => {
+    return {
+        user: state.login.user
+    };
+};
+
+export default connect(
+    mapStateToProps,
+    { postLoginSubmit }
+)(Login);

+ 3 - 2
src/reducers/combineReducers.js

@@ -2,10 +2,11 @@ import { combineReducers } from "redux";
 import { reducer as formReducer } from "redux-form";
 
 import adminMainPageReducer from "./adminMainPageReducer";
-import Sidebar from "./show-sidebar";
+//import Sidebar from "./show-sidebar";
+import login from "./login";
 
 export default combineReducers({
 	form: formReducer,
 	adminMainPageReducer,
-	Sidebar
+	login
 });

+ 25 - 0
src/reducers/login.js

@@ -0,0 +1,25 @@
+import * as types from "../actionTypes/actionTypes";
+
+const initialState = {
+    user: {}
+}
+
+export default (state = initialState, action) => {
+    switch (action.type) {
+        case types.POST_REQUEST_LOGIN: {
+            return state;
+        }
+
+        case types.POST_REQUEST_SUCCESS_LOGIN: {
+            console.log('some',action.payload.user);
+            return state;
+        }
+
+        case types.POST_REQUEST_ERROR_LOGIN: {
+            return state;
+        }
+
+        default:
+            return state;
+    }
+}