|
| 1 | +import React from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import Modal from '../Modal' |
| 4 | +import {withRouter} from 'react-router-dom' |
| 5 | + |
| 6 | +import modelApp from '../../model/app' |
| 7 | +import modelLayer from '../../model/layer' |
| 8 | +import modelStyle from '../../model/style' |
| 9 | + |
| 10 | +import Property from '../Property' |
| 11 | + |
| 12 | +class LayerEditModalClone extends React.Component { |
| 13 | + |
| 14 | + constructor(props) { |
| 15 | + super(props) |
| 16 | + const {layer, style} = props |
| 17 | + |
| 18 | + const cloneId = modelLayer.helpers.getLayerCloneId({layer, style}) |
| 19 | + |
| 20 | + this.state = { |
| 21 | + id: cloneId, |
| 22 | + placement: '', |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + handleClone = async ()=>{ |
| 27 | + const {history, layer, path, style} = this.props, |
| 28 | + {id, placement} = this.state |
| 29 | + |
| 30 | + try{ |
| 31 | + await modelApp.actions.setLoading(true) |
| 32 | + |
| 33 | + const clone = await modelLayer.actions.clone({cloneId: id, layer, path, placement, style}) |
| 34 | + await modelApp.actions.setLoading(false) |
| 35 | + |
| 36 | + // send user to newly created layer |
| 37 | + const route = `layers/${clone.get('id')}` |
| 38 | + history.push(modelStyle.helpers.getRouteFromPath({path, route})) |
| 39 | + |
| 40 | + } catch(e){ |
| 41 | + await modelApp.actions.setLoading(false) |
| 42 | + await modelApp.actions.setError(e) |
| 43 | + } |
| 44 | + |
| 45 | + this.setState({ |
| 46 | + modal: 'cloneDone' |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + handleChange = ({name, value})=>{ |
| 51 | + let state = {} |
| 52 | + state[name] = value |
| 53 | + |
| 54 | + this.setState(state) |
| 55 | + } |
| 56 | + |
| 57 | + render (){ |
| 58 | + const {handleClose} = this.props, |
| 59 | + {id, placement} = this.state |
| 60 | + |
| 61 | + //const stylePath = modelStyle.helpers.getRouteFromPath({path}) |
| 62 | + |
| 63 | + const handle = { |
| 64 | + change: this.handleChange |
| 65 | + } |
| 66 | + |
| 67 | + const options = [ |
| 68 | + {name:'at the bottom', value:'bottom'}, |
| 69 | + {name:'after cloned layer', value:'after'}, |
| 70 | + ] |
| 71 | + |
| 72 | + return ( |
| 73 | + <Modal> |
| 74 | + <div className="modal-header text-dark"> |
| 75 | + <h5 className="modal-title">CLONE LAYER</h5> |
| 76 | + <button type="button" className="close" data-dismiss="modal" aria-label="Close" onClick={handleClose}> |
| 77 | + <span aria-hidden="true">×</span> |
| 78 | + </button> |
| 79 | + </div> |
| 80 | + <div className="modal-body text-left text-dark"> |
| 81 | + |
| 82 | + <Property |
| 83 | + handle={handle} |
| 84 | + info={'id for the layer clone'} |
| 85 | + key={'id'} |
| 86 | + label={'new layer id'} |
| 87 | + name={'id'} |
| 88 | + path={null} |
| 89 | + required={true} |
| 90 | + type={'string'} |
| 91 | + value={id} |
| 92 | + /> |
| 93 | + <Property |
| 94 | + handle={handle} |
| 95 | + info={'placement of the cloned layer'} |
| 96 | + key={'placement'} |
| 97 | + label={'placement'} |
| 98 | + name={'placement'} |
| 99 | + options={options} |
| 100 | + path={null} |
| 101 | + required={true} |
| 102 | + type={'enum'} |
| 103 | + value={placement} |
| 104 | + /> |
| 105 | + |
| 106 | + </div> |
| 107 | + <div className="modal-footer"> |
| 108 | + <button onClick={this.handleClone} className="btn btn-outline-dark">Create</button> |
| 109 | + </div> |
| 110 | + </Modal> |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +LayerEditModalClone.propTypes = { |
| 117 | + handleClose: PropTypes.func.isRequired, |
| 118 | + handleDone: PropTypes.func, |
| 119 | + history: PropTypes.object, |
| 120 | + layer: PropTypes.object, |
| 121 | + path: PropTypes.array, |
| 122 | + style: PropTypes.object, |
| 123 | +} |
| 124 | + |
| 125 | +export default withRouter(LayerEditModalClone) |
| 126 | + |
0 commit comments