From 077fe0b6a9a17e07c750a28c121059056449e8f1 Mon Sep 17 00:00:00 2001 From: Yannick Date: Tue, 30 Jan 2018 14:43:14 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20edit=20functions=20to=20the?= =?UTF-8?q?=20app?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/formItem/index.js | 21 +++++++++++++-- src/routes/edit/index.js | 46 +++++++++++++++++++++++++++----- src/routes/editMedia/index.js | 39 +++++++++++++++++++++++---- 3 files changed, 93 insertions(+), 13 deletions(-) diff --git a/src/components/formItem/index.js b/src/components/formItem/index.js index cfa222a..c680322 100644 --- a/src/components/formItem/index.js +++ b/src/components/formItem/index.js @@ -5,23 +5,39 @@ import RemoveButton from '../../components/removeButton/'; import style from './style'; export default class FormItem extends Component { + constructor() { + super(); + + this.handleChange = this.handleChange.bind(this); + this.removeField = this.removeField.bind(this); + } + + handleChange(e, kind) { + this.props.handleState(kind, e.target.value); + } + + removeField(kind) { + this.props.handleState(kind, null); + } + render() { return (
- this.handleChange(e, this.props.item)} type="number" step="0.00001" value={this.props.value} class={style.addedInputField} /> - +
); } @@ -30,4 +46,5 @@ export default class FormItem extends Component { FormItem.propTypes = { item: PropTypes.string.isRequired, value: PropTypes.string.isRequired, + handleState: PropTypes.func.isRequired, }; diff --git a/src/routes/edit/index.js b/src/routes/edit/index.js index f9a267c..9d10cbe 100644 --- a/src/routes/edit/index.js +++ b/src/routes/edit/index.js @@ -20,6 +20,8 @@ export default class Edit extends Component { this.handleSave = this.handleSave.bind(this); this.handleResetLoc = this.handleResetLoc.bind(this); + this.handleState = this.handleState.bind(this); + this.handleChange = this.handleChange.bind(this); this.handleEditSave = this.handleEditSave.bind(this); this.state = { @@ -29,9 +31,13 @@ export default class Edit extends Component { } componentWillMount(nextProps) { - this.ref = base.bindToState(`/${this.props.uid}/mes/${this.props.measurementId}`, { + this.ref = base.fetch(`/${this.props.uid}/mes/${this.props.measurementId}`, { context: this, - state: 'measurement', + then(data) { + this.setState({ + measurement: data, + }); + }, }); } @@ -52,6 +58,18 @@ export default class Edit extends Component { }); } + handleState(kind, value) { + const measurement = { ...this.state.measurement }; + measurement[kind] = value; + this.setState({ + measurement, + }); + } + + handleChange(e, kind) { + this.handleState(kind, e.target.value); + } + handleSave() { this.setState({ open: false, @@ -59,7 +77,13 @@ export default class Edit extends Component { } handleEditSave() { - route(`/mes/${this.props.measurementId}`); + const measurementId = this.props.measurementId; + base.update(`/${this.props.uid}/mes/${this.props.measurementId}`, { + data: this.state.measurement, + then() { + route(`/mes/${measurementId}`); + }, + }); } render() { @@ -101,9 +125,19 @@ export default class Edit extends Component {
- {measurement.acidity && } - {measurement.salinity && } - {measurement.Temperature && } + {measurement.acidity && ( + + )} + {measurement.salinity && ( + + )} + {measurement.Temperature && ( + + )} - date_range
@@ -124,7 +147,12 @@ export default class EditMedia extends Component { />
- this.handleChange(e, 'category')} + > @@ -137,6 +165,7 @@ export default class EditMedia extends Component { label="Description (optional)" fullWidth placeholder="" + handleState={this.handleState} />