Skip to content

Instantly share code, notes, and snippets.

@aray12
Last active January 20, 2016 18:07
Show Gist options
  • Save aray12/4b9f3001d6a6c755fdff to your computer and use it in GitHub Desktop.
Save aray12/4b9f3001d6a6c755fdff to your computer and use it in GitHub Desktop.
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import NavBar from './Navbar';
import Login from './Login';
export default class App extends Component {
constructor(props, context) {
super(props, context);
}
render() {
return (
<div>
<NavBar />
<div className="content">
{this.props.children}
</div>
</div>
);
}
}
App.propTypes = {
children: PropTypes.any,
};
import 'babel-polyfill';
import React from 'react';
import { render } from 'react-dom';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import { Router, Route, browserHistory } from 'react-router';
import { syncHistory, routeReducer } from 'redux-simple-router';
import createLogger from 'redux-logger';
import thunkMiddleware from 'redux-thunk';
import reducers from './reducers';
import { App, Login } from './components';
const reducer = combineReducers(Object.assign({}, reducers, {
routing: routeReducer,
}));
const loggerMiddleware = createLogger({collapsed: true});
const reduxRouterMiddleware = syncHistory(browserHistory);
const middleware = [
reduxRouterMiddleware,
thunkMiddleware,
loggerMiddleware,
];
const createStoreWithMiddleware = applyMiddleware(...middleware)(createStore);
const store = createStoreWithMiddleware(reducer);
// Required for replaying actions from devtools to work
// reduxRouterMiddleware.listenForReplays(store);
const rootElement = document.getElementById('app');
render((
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="login" component={Login}/>
</Route>
</Router>
</Provider>
), rootElement);
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { fetchLogin } from '../actions/AccountActions';
class Login extends Component {
constructor(props, context) {
super(props, context);
this.state = {
email: '',
password: '',
};
}
handleChange(field, event) {
const nextState = this.state;
nextState[field] = event.target.value;
this.setState(nextState);
}
handleSubmit(event) {
event.preventDefault();
this.props.handleSubmit(this.state);
}
render() {
return (
<form onSubmit={event => this.handleSubmit(event)}>
<input
type="text" placeholder="Email"
value={this.state.email}
onChange={this.handleChange.bind(this, 'email')}
/>
<input
type="password" placeholder="Password"
value={this.state.password}
onChange={this.handleChange.bind(this, 'password')}
/>
<input type="submit" value="Submit"/>
</form>
);
}
}
Login.propTypes = {
handleSubmit: PropTypes.func.isRequired,
};
function mapStateToProps(state) {
return {
redirect: state.redirect,
};
}
function mapDispatchToProps(dispatch) {
return {
handleSubmit: user => dispatch(fetchLogin(user)),
};
}
export default connect(mapStateToProps, mapDispatchToProps)(Login);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment