Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Introduce redux store #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"dependencies": {
"domready": "^1.0.8",
"react": "^15.1.0",
"react-dom": "^15.1.0"
"react-dom": "^15.1.0",
"react-redux": "^4.4.5",
"redux": "^3.5.1",
"object-assign": "4.0.1",
"redux-actions": "^0.9.1"
}
}
5 changes: 5 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createAction } from 'redux-actions';

export const activateApplication = createAction('ACTIVATE_APPLICATION');
export const updateFormValue = createAction('UPDATE_FORM_VALUE', (propertyName, value) => ({propertyName, value}));

93 changes: 28 additions & 65 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,34 @@
import React from 'react';
import {findDOMNode} from 'react-dom';

import Form from './form.js';
import { connect } from 'react-redux';
import * as actions from './actions.js';
import { bindActionCreators } from 'redux';

const fields = [
{
id: 'name',
inputComponentName: 'TextInput',
placeholder: 'Enter your name'
},
{
id: 'age',
inputComponentName: 'TextInput',
placeholder: 'Enter your age'
},
{
id: 'gender',
inputComponentName: 'Radio',
options: ['male', 'female', 'other']
}
];

export default class App extends React.Component {
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
isActive: false,
formData: {
name: '',
age: ''
}
};
this.toggleActiveState = this.toggleActiveState.bind(this);
this.handleFormFieldChange = this.handleFormFieldChange.bind(this);
}

render() {
const style = this.state.isActive
const style = this.props.appState.isActive
?
{backgroundColor: 'teal', color: 'white'}
:
{};
return (
<div style={style} onClick={this.toggleActiveState}>
<div style={style} onClick={this.props.actions.activateApplication}>
<h1>Marathon Estimate</h1>
<Form
fields={fields}
formData={this.state.formData}
fields={this.props.appState.fields}
onChange={this.handleFormFieldChange}
/>
<div
ref='google-map'
style={{width: '100px', height: '100px', backgroundColor: '#bada55'}}
/>
</div>
);
}

componentDidMount() {
const node = findDOMNode(this.refs['google-map']);
const el = document.createElement('p');
el.innerHTML = 'hello!';
node.appendChild(el);
el.addEventListener('click', this.onCustomElementClick);
this.el = el;
}

componentWillUnmount() {
this.el.removeEventListener('click', this.onCustomElementClick);
}

onCustomElementClick() {
alert('hello!');
}

toggleActiveState() {
this.setState({
isActive: !this.state.isActive
});
}

handleFormFieldChange(value, id) {
this.setState({
formData: Object.assign({}, this.state.formData, {
[id]: value
})
});
this.props.actions.updateFormValue(id, value);
}

// TODO: implement
Expand All @@ -96,3 +39,23 @@ export default class App extends React.Component {
});
}
}

function mapStateToProps(state) {
return {
appState: state
};
}

function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(actions, dispatch),
};
}

export default connect(
mapStateToProps,
mapDispatchToProps
)(App);



1 change: 0 additions & 1 deletion src/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default function Form(props) {
<Comp
{...field}
key={i}
value={props.formData[field.id]}
onChange={props.onChange}
/>
);
Expand Down
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import React from 'react';
import {render} from 'react-dom';
import domReady from 'domready';

import App from './app.js';
import { Provider } from 'react-redux';
import configureStore from './store.js';

const store = configureStore();

domReady(() => {
const container = document.getElementById('app');
render(<App/>, container);
render(
<Provider store={store}>
<App/>
</Provider>, container);
});
43 changes: 43 additions & 0 deletions src/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import objectAssign from 'object-assign';
import { handleActions } from 'redux-actions';

const initialState = {
fields: [
{
id: 'name',
inputComponentName: 'TextInput',
placeholder: 'Enter your name',
value: ''
},
{
id: 'age',
inputComponentName: 'TextInput',
placeholder: 'Enter your age',
value: ''
},
{
id: 'gender',
inputComponentName: 'Radio',
options: ['male', 'female', 'other'],
value: []
}
],
isActive: false
};

const appState = handleActions({
'UPDATE_FORM_VALUE': function (state, action) {
let newState = objectAssign({}, state);
newState.fields = newState.fields.map(function(field) {
console.log(field);
if (field.id == action.payload.propertyName) {
field.value = action.payload.value;
}
return field;
});
return newState;
},
'ACTIVATE_APPLICATION': (state, action) => objectAssign({}, state, {isActive: true})
}, initialState);

export default appState;
8 changes: 8 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createStore, applyMiddleware, compose } from 'redux';
import reducer from './reducer.js';

export default function configureStore(initialState) {
return createStore(reducer, initialState,
window.devToolsExtension ? window.devToolsExtension() : f => f
);
}