Skip to content

Instantly share code, notes, and snippets.

@henriquejensen
Last active January 14, 2018 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henriquejensen/4c8b4de26faec89ca225ead386a8253a to your computer and use it in GitHub Desktop.
Save henriquejensen/4c8b4de26faec89ca225ead386a8253a to your computer and use it in GitHub Desktop.
Arquitetura baseada em modulos React Redux
import * as constantsLogin from "../constants/constantsLogin"
export const login({email, passaword}) {
return {
type: constantsLogin.LOGIN,
payload: true
}
}
export const logout() {
return {
type: constantsLogin.LOGOUT,
payload: false
}
}
import React from 'react';
import { connect } from 'react-redux';
import { Switch, Route, HashRouter } from 'react-router-dom';
import Home from './modules/home/components';
import Login from './modules/login/components';
const App = props =>
<HashRouter>
<Switch>
<Route exact path="/" component={props.logged ? Home : Login} />
</Switch>
</HashRouter>
));
const mapStateToProps = ({reducerLogin) => ({ logged: reducerLogin.logged });
export default connect(mapStateToProps)(App);
export const LOGIN = "LOGIN"
export const LOGOUT = "LOGOUT"
import React, {Component} from "react"
import * as actionsLogin from "../../login/actions/actionsLogin"
import HomeControllerView from "./HomeControllerView"
class HomeController extends Component {
constructor() {
super()
this.state = {}
}
render() {
return <HomeControllerView {...this.props} />
}
}
export default connect(({reducerLogin}) => {...reducerLogin}, {...actionsLogin})(LoginController)
import React from "react"
const HomeControllerView = (props) => {
const {logout} = props;
return (
<div>
<div>
<button onClick={logout} >
Sair
</button>
</div>
<p>Bem vindo</p>
</div>
)
}
export default HomeControllerView
import view from "./user/LoginControllerView"
import login from "./user/LoginController"
const LoginController = login
export default LoginController
export const LoginControllerView = view
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './App';
import store from './store';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'),
);
import view from "./user/HomeControllerView"
import login from "./user/HomeController"
const HomeController = login
export default HomeController
export const HomeControllerView = view
import React, {Component} from "react"
import * as actionsLogin from "../actions/actionsLogin"
import LoginControllerView from "./LoginControllerView"
class LoginController extends Component {
constructor() {
super()
this.state = {}
}
onChange = evt => {
this.setState({[evt.target.name]: evt.target.value})
}
render() {
return <LoginControllerView {...this.props} {...this.state} onChange={this.onChange} />
}
}
export default connect(({reducerLogin}) => {...reducerLogin}, {...actionsLogin})(LoginController)
import React from "react"
const LoginControllerView = (props) => {
const {email, password, onChange} = props;
return (
<div>
<div>
<label>Email</label>
<input type="email" name="email" placeholder="Digite seu email" onChange={onChange} value={email} />
</div>
<div>
<label>Senha</label>
<input type="password" name="paswword" placeholder="********" onChange={onChange} value={password} />
</div>
</div>
)
}
export default LoginControllerView
import * as constantsLogin from "../constants/constantsLogin"
const reducerLogin = (state={logged:false}, action) => {
switch(action.type) {
case constantsLogin.LOGIN:
return {...state, logged: action.payload}
case constantsLogin.LOGOUT:
return {...state, logged: action.payload}
default
return state
}
}
export default reducerLogin
import { applyMiddleware, createStore, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import reducerLogin from '../modules/login/reducer';
import reducerHome from '../modules/home/reducer';
const rootReducer = combineReducers({
reducerLogin,
reducerHome
});
const store = createStore(
rootReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
applyMiddleware(thunk),
);
export default store;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment