Skip to content

Instantly share code, notes, and snippets.

@cassiozen
Created February 16, 2016 21:52
Show Gist options
  • Save cassiozen/1eb2418ab75e4df43afa to your computer and use it in GitHub Desktop.
Save cassiozen/1eb2418ab75e4df43afa to your computer and use it in GitHub Desktop.
import React, { Component, PropTypes } from 'react';
import { render } from 'react-dom';
import bankStore from './bankStore';
class BankApp extends Component {
handleDeposit() {
bankStore.dispatch({type:"deposit", amount: this.refs.amount.value})
this.refs.amount.value = ""
}
handleWithdraw() {
bankStore.dispatch({type:"withdraw", amount: this.refs.amount.value})
this.refs.amount.value = ""
}
componentDidMount(){
bankStore.subscribe(()=>{
this.forceUpdate();
})
}
render() {
return (
<div>
<header><img src="//www.pro-react.com/logos/redux-bank.svg" width="150" /><br/>Redux Bank</header>
<br />
<h1>Your balance is {bankStore.getState()} </h1>
<div className="atm">
<input type="text" placeholder="Enter Ammount" ref="amount" />
<br />
<button onClick={this.handleWithdraw.bind(this)}>Withdraw</button>
<button onClick={this.handleDeposit.bind(this)}>Deposit</button>
</div>
</div>
);
}
}
BankApp.propTypes = {
balance: PropTypes.number,
onDeposit: PropTypes.func,
onWithdraw: PropTypes.func
};
render(
<BankApp />,
document.getElementById('root')
);
import { createStore } from 'redux'
let myReducer = (state = 0, action) => {
console.log(action)
switch (action.type) {
case "deposit":
return state + Number(action.amount)
case "withdraw":
return state - Number(action.amount)
default:
return state
}
}
const bankStore = createStore(myReducer);
export default bankStore;
body {
margin: 0;
font-family: "Myriad Pro", Helvetica, Arial, sans-serif;
background-color: #00703B;
color: #fff;
text-align: center;
}
header {
width:100%;
padding: 15px;
background-color: #D8D8BF;
color: #00703B;
font-size: 30px;
}
h1{
font-size: 30px;
}
.atm {
width: 300px;
height: 115px;
border-radius: 10px;
background-color: #1D4F27;
margin: 10px auto 0 auto;
padding: 20px;
}
.atm input {
font-size:25px;
width: 272px
}
.atm button {
margin: 20px 10px;
padding: 20px 40px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment