-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add react, react-redux and extract logic of the calculator to components
- Loading branch information
Showing
11 changed files
with
272 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React, { Component } from 'react' | ||
|
||
interface ButtonProps { | ||
onClick: () => void; | ||
title: string; | ||
} | ||
|
||
export default class Button extends Component<ButtonProps, {}> { | ||
render() { | ||
const {onClick, title} = this.props; | ||
return ( | ||
<button onClick={onClick}> | ||
{title} | ||
</button> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React, { Component } from 'react' | ||
import { connect } from 'react-redux' | ||
|
||
interface DisplayProps { | ||
value: number; | ||
} | ||
|
||
export class Display extends Component<DisplayProps, {}> { | ||
render() { | ||
return ( | ||
<div style={{border: "1px solid black"}}> | ||
{this.props.value} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
const mapStateToProps = (state: any) => ({ | ||
value: state.total | ||
}); | ||
|
||
export default connect(mapStateToProps, null)(Display) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { Component } from 'react' | ||
import { connect } from 'react-redux' | ||
import { setCurrent } from './store' | ||
|
||
interface InputProps { | ||
value: number | ||
setCurrent: (value: number) => void; | ||
} | ||
|
||
export class Input extends Component<InputProps, {}> { | ||
|
||
onChange = (e: React.FormEvent<HTMLInputElement>) => { | ||
this.props.setCurrent(parseInt(e.currentTarget.value)); | ||
} | ||
|
||
render() { | ||
const {value} = this.props; | ||
|
||
return ( | ||
<input value={value} onChange={this.onChange } /> | ||
) | ||
} | ||
} | ||
|
||
const mapStateToProps = (state: any) => ({ | ||
value: state.inputValue | ||
}) | ||
|
||
const mapDispatchToProps = dispatch => ({ | ||
setCurrent: (value: number) => dispatch(setCurrent(value)) | ||
}); | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(Input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as React from "react"; | ||
import * as ReactDOM from "react-dom" | ||
import { Provider } from 'react-redux' | ||
|
||
import Button from "./Button"; | ||
import Input from "./Input"; | ||
import Display from "./Display"; | ||
|
||
import { add, subtract, reset, store } from './store' | ||
|
||
class App extends React.Component<{}, {}> { | ||
render() { | ||
return ( | ||
<Provider store={store}> | ||
<div style={{ margin: "20px" }}> | ||
<Input /> | ||
<Button title="Add" onClick={() => store.dispatch(add(store.getState().current))} /> | ||
<Button title="Subtract" onClick={() => store.dispatch(subtract(store.getState().current))} /> | ||
<Button title="Reset" onClick={() => store.dispatch(reset())} /> | ||
<Display /> | ||
</div> | ||
</Provider> | ||
); | ||
} | ||
} | ||
|
||
ReactDOM.render( | ||
<App />, | ||
document.getElementById("app") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { createStore } from 'redux'; | ||
|
||
// SET-UP | ||
const reducer = (state = {total: 0, current: 0}, action: {type: string, payload?: any}) => { | ||
console.log(action); | ||
|
||
switch(action.type) { | ||
case 'SET_CURRENT': | ||
return {...state, current: action.payload.value}; | ||
case 'ADD': | ||
return {...state, total: state.total + action.payload.value}; | ||
case 'SUBTRACT': | ||
return {...state, total: state.total - action.payload.value}; | ||
case 'RESET': | ||
return {...state, total: 0}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const enhancer = ((window as any).__REDUX_DEVTOOLS_EXTENSION__ && (window as any).__REDUX_DEVTOOLS_EXTENSION__()) | ||
|
||
// ACTION-CREATORS | ||
export const add = (value: number) => ({ | ||
type: 'ADD', | ||
payload: { value }, | ||
}); | ||
|
||
export const subtract = (value: number) => ({ | ||
type: 'SUBTRACT', | ||
payload: { value }, | ||
}); | ||
|
||
export const setCurrent = (value: number) => ({ | ||
type: 'SET_CURRENT', | ||
payload: { value }, | ||
}); | ||
|
||
export const reset = () => ({ type: 'RESET' }); | ||
|
||
export const store = createStore(reducer, enhancer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.