From ce10a2adc477c46dafd9ab21ad550cf2bc5b84d3 Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Thu, 1 Aug 2019 12:16:08 +0100 Subject: [PATCH] Enable multi-copy for edit --- app/src/frontend/building-edit.js | 183 +++++++++++++++++++++++------- app/src/frontend/sidebar.css | 3 + 2 files changed, 148 insertions(+), 38 deletions(-) diff --git a/app/src/frontend/building-edit.js b/app/src/frontend/building-edit.js index 37f7ce71..8f61a79e 100644 --- a/app/src/frontend/building-edit.js +++ b/app/src/frontend/building-edit.js @@ -50,18 +50,52 @@ BuildingEdit.propTypes = { class EditForm extends Component { constructor(props) { super(props); - this.state = {} + this.state = { + error: this.props.error || undefined, + like: this.props.like || undefined, + copying: false, + keys_to_copy: {} + } for (const field of props.fields) { this.state[field.slug] = props[field.slug] } - this.state.error = this.props.error || undefined; - this.state.like = this.props.like || undefined; this.handleChange = this.handleChange.bind(this); this.handleCheck = this.handleCheck.bind(this); this.handleLike = this.handleLike.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.handleUpdate = this.handleUpdate.bind(this); + + this.toggleCopying = this.toggleCopying.bind(this); + this.toggleCopyAttribute = this.toggleCopyAttribute.bind(this); + } + + /** + * Enter or exit "copying" state - allow user to select attributes to copy + */ + toggleCopying() { + this.setState({ + copying: !this.state.copying + }) + } + + /** + * Keep track of data to copy (accumulate while in "copying" state) + * + * Note that we track keys only - values are already held in state + * + * @param {string} key + */ + toggleCopyAttribute(key) { + const keys = this.state.keys_to_copy; + if(this.state.keys_to_copy[key]){ + delete keys[key]; + } else { + keys[key] = true; + } + this.setState({ + keys_to_copy: keys + }) } /** @@ -175,6 +209,12 @@ class EditForm extends Component { const cat = this.props.cat; const buildingLike = this.props.building_like; + const values_to_copy = {} + for (const key of Object.keys(this.state.keys_to_copy)) { + values_to_copy[key] = this.state[key] + } + const data_string = JSON.stringify(values_to_copy); + return (
@@ -187,14 +227,38 @@ class EditForm extends Component {