|
| 1 | +import React, {Component, PropTypes} from 'react'; |
| 2 | +import {connect} from 'react-redux'; |
| 3 | +import {bindActionCreators} from 'redux'; |
| 4 | +import reduxForm from 'redux-form'; |
| 5 | +import widgetValidation, {colors} from '../validation/widgetValidation'; |
| 6 | +import * as widgetActions from '../actions/widgetActions'; |
| 7 | + |
| 8 | +@connect( |
| 9 | + state => ({ |
| 10 | + form: state.widgetForm, |
| 11 | + saveError: state.widgets.saveError |
| 12 | + }), |
| 13 | + dispatch => ({ |
| 14 | + ...bindActionCreators(widgetActions, dispatch), |
| 15 | + dispatch |
| 16 | + }) |
| 17 | +) |
| 18 | +@reduxForm('widgetForm', widgetValidation) |
| 19 | +export default class WidgetForm extends Component { |
| 20 | + static propTypes = { |
| 21 | + data: PropTypes.object.isRequired, |
| 22 | + errors: PropTypes.object.isRequired, |
| 23 | + editStop: PropTypes.func.isRequired, |
| 24 | + handleBlur: PropTypes.func.isRequired, |
| 25 | + handleChange: PropTypes.func.isRequired, |
| 26 | + handleSubmit: PropTypes.func.isRequired, |
| 27 | + invalid: PropTypes.bool.isRequired, |
| 28 | + pristine: PropTypes.bool.isRequired, |
| 29 | + save: PropTypes.func.isRequired, |
| 30 | + submitting: PropTypes.bool.isRequired, |
| 31 | + saveError: PropTypes.string, |
| 32 | + sliceKey: PropTypes.string.isRequired, |
| 33 | + touched: PropTypes.object.isRequired |
| 34 | + }; |
| 35 | + |
| 36 | + render() { |
| 37 | + const {sliceKey} = this.props; |
| 38 | + const { data, editStop, errors, handleBlur, handleChange, handleSubmit, invalid, |
| 39 | + pristine, save, submitting, saveError: { [sliceKey]: saveError }, touched } = this.props; |
| 40 | + const styles = require('../views/Widgets.scss'); |
| 41 | + return ( |
| 42 | + <tr className={submitting ? styles.saving : ''}> |
| 43 | + <td className={styles.idCol}>{data.id}</td> |
| 44 | + <td className={styles.colorCol}> |
| 45 | + <select name="color" |
| 46 | + className="form-control" |
| 47 | + value={data.color} |
| 48 | + onChange={handleChange('color')} |
| 49 | + onBlur={handleBlur('color')}> |
| 50 | + {colors.map(color => <option value={color} key={color}>{color}</option>)} |
| 51 | + </select> |
| 52 | + {errors.color && touched.color && <div className="text-danger">{errors.color}</div>} |
| 53 | + </td> |
| 54 | + <td className={styles.sprocketsCol}> |
| 55 | + <input type="text" |
| 56 | + className="form-control" |
| 57 | + value={data.sprocketCount} |
| 58 | + onChange={handleChange('sprocketCount')} |
| 59 | + onBlur={handleBlur('sprocketCount')}/> |
| 60 | + {errors.sprocketCount && touched.sprocketCount && <div className="text-danger">{errors.sprocketCount}</div>} |
| 61 | + </td> |
| 62 | + <td className={styles.ownerCol}> |
| 63 | + <input type="text" |
| 64 | + className="form-control" |
| 65 | + value={data.owner} |
| 66 | + onChange={handleChange('owner')} |
| 67 | + onBlur={handleBlur('owner')}/> |
| 68 | + {errors.owner && touched.owner && <div className="text-danger">{errors.owner}</div>} |
| 69 | + </td> |
| 70 | + <td className={styles.buttonCol}> |
| 71 | + <button className="btn btn-default" |
| 72 | + onClick={() => editStop(sliceKey)} |
| 73 | + disabled={submitting}> |
| 74 | + <i className="fa fa-ban"/> Cancel |
| 75 | + </button> |
| 76 | + <button className="btn btn-success" |
| 77 | + onClick={handleSubmit(() => save(data))} |
| 78 | + disabled={pristine || invalid || submitting}> |
| 79 | + <i className={'fa '+(submitting ? 'fa-cog fa-spin' : 'fa-cloud')}/> Save |
| 80 | + </button> |
| 81 | + {saveError && <div className="text-danger">{saveError}</div>} |
| 82 | + </td> |
| 83 | + </tr> |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
0 commit comments