Advertisement
Guest User

Untitled

a guest
Apr 14th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //actions/userActions.js ================================================================================
  2. import axios from 'axios';
  3.  
  4. export const USER_SIGN_UP_RESPONSE = 'USER_SIGN_UP_RESPONSE';
  5.  
  6. const SIGN_UP_URL = 'http://localhost:8080/api/user/new';
  7.  
  8. export function userSignupResponse(response) {
  9.     return {
  10.         type: USER_SIGN_UP_RESPONSE,
  11.         payload: response
  12.     };
  13. }
  14.  
  15. export function userSignupRequest(userData) {
  16.     return (dispatch) => {
  17.         axios.post(SIGN_UP_URL, userData)
  18.             .then((res) => {
  19.                 dispatch(userSignupResponse(res));
  20.             })
  21.             .catch(() => console.log('err'));
  22.     };
  23. }
  24.  
  25.  
  26. //reducers/users.js ================================================================================
  27. import { USER_SIGN_UP_RESPONSE } from './../actions/userActions';
  28.  
  29. export function userSignupResponse(state = {}, action) {
  30.     switch (action.type) {
  31.  
  32.         case USER_SIGN_UP_RESPONSE:
  33.             return { ...state, userSignupResponse: action.payload }
  34.        
  35.         default:
  36.             return state;    
  37.     }
  38. }
  39.  
  40.  
  41. //SignupForm ================================================================================
  42.  
  43. import React, { Component } from 'react';
  44. import { connect } from 'react-redux';
  45.  
  46. import { userSignupRequest } from './../../actions/userActions';
  47.  
  48. import './SignupForm.css';
  49.  
  50.  
  51. class SignupForm extends Component {
  52.    
  53.     constructor(props) {
  54.         super(props);
  55.  
  56.         this.state = {};
  57.  
  58.  
  59.         this.onSignupFormSubmit = this.onSignupFormSubmit.bind(this);
  60.         this.onSignupFormFieldsChange = this.onSignupFormFieldsChange.bind(this);
  61.     }
  62.  
  63.  
  64.  
  65.     onSignupFormSubmit(e) {
  66.         e.preventDefault();
  67.         this.props.userSignupRequest(this.state);
  68.         console.log(this.props);
  69.     }
  70.  
  71.     onSignupFormFieldsChange(e) {
  72.         this.setState({[e.target.name]: e.target.value});
  73.     }
  74.  
  75.     render() {
  76.         return (
  77.             <form className="signupForm" onSubmit={ this.onSignupFormSubmit }>
  78.                 <button
  79.                     type="button"
  80.                     className="signupForm__closeBtn"
  81.                     onClick={ this.props.closeSignupFormModal } >
  82.                         <span></span>
  83.                         <span></span>
  84.                 </button>
  85.                 <h2 className="signupForm__title">Sign up</h2>
  86.                 <p className="signupForm__subtitle">Already have an account? <span onClick={this.props.showSigninModalForm}>Sign in</span></p>
  87.                 <input  type="email"
  88.                         className="signupForm__input"
  89.                         placeholder="Email address..."
  90.                         name="email"
  91.                         onChange={ this.onSignupFormFieldsChange }/>
  92.                 <input  type="password"
  93.                         className="signupForm__input"
  94.                         placeholder="Password..."
  95.                         name="password"
  96.                         onChange={ this.onSignupFormFieldsChange }/>
  97.                 <input  type="password"
  98.                         className="signupForm__input"
  99.                         placeholder="Repeat password..." />
  100.                 <button type="submit"
  101.                         className="signupForm__button">Sign up</button>
  102.  
  103.             </form>
  104.         );
  105.     }
  106. }
  107.  
  108.  
  109. const mapStateToProps = (state) => {
  110.     return {
  111.         userSignupResponse: state.userSignupResponse
  112.     };
  113. }
  114.  
  115. const mapDispatchToProps = (dispatch) => {
  116.     return {
  117.         userSignupRequest: (userData) => {
  118.             dispatch(userSignupRequest(userData))
  119.         }
  120.        
  121.     }
  122. }
  123.  
  124. export default connect(mapStateToProps, mapDispatchToProps)(SignupForm);
  125.  
  126.  
  127. //reducers/index.js ================================================================================
  128.  
  129. import { combineReducers } from 'redux';
  130.  
  131. import { userSignupResponse } from './users';
  132.  
  133. export default combineReducers({
  134.     userSignupResponse
  135. });
  136.  
  137.  
  138. //store/configureStore.js ================================================================================
  139.  
  140. import { createStore, applyMiddleware } from 'redux';
  141. import thunk from 'redux-thunk';
  142. import rootReducer from './../reducers';
  143.  
  144. export default function configureStore(initialState) {
  145.     return createStore(
  146.         rootReducer,
  147.         initialState,
  148.         applyMiddleware(thunk)
  149.     );
  150. }
  151.  
  152. //index.js ================================================================================
  153.  
  154. import React from 'react';
  155. import ReactDOM from 'react-dom';
  156. import { Provider } from 'react-redux';
  157.  
  158. import configureStore from './store/configureStore';
  159. const store = configureStore();
  160.  
  161. import './index.css';
  162.  
  163. import IndexPage from './containers/IndexPage';
  164.  
  165. ReactDOM.render(
  166.   <Provider store={store}>
  167.     <IndexPage />
  168.   </Provider>,
  169.   document.getElementById('root')
  170. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement