|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { Modal, Button, Dropdown } from 'react-bootstrap'; |
| 3 | +import $ from 'jquery' |
| 4 | +var hypertopic = require("hypertopic") |
| 5 | + |
| 6 | +class Duplicator extends Component { |
| 7 | + |
| 8 | + constructor(props) { |
| 9 | + super(props); |
| 10 | + |
| 11 | + this.state = { |
| 12 | + showModal: false, |
| 13 | + showModalConfirmation: false, |
| 14 | + showToast: false, |
| 15 | + corpora: [], |
| 16 | + viewpoints: [] |
| 17 | + }; |
| 18 | + this.handleShow = this.handleShow.bind(this); |
| 19 | + this.handleClose = this.handleClose.bind(this); |
| 20 | + this.handleValidate = this.handleValidate.bind(this); |
| 21 | + this.handleConfirm = this.handleConfirm.bind(this); |
| 22 | + this.onChangeCheckBox = this.onChangeCheckBox.bind(this); |
| 23 | + this.closeToast = this.closeToast.bind(this) |
| 24 | + this.addUserToEntity = this.addUserToEntity.bind(this) |
| 25 | + } |
| 26 | + |
| 27 | + handleClose() { |
| 28 | + this.setState({ showModal: false, showModalConfirmation: false }); |
| 29 | + } |
| 30 | + |
| 31 | + handleShow() { |
| 32 | + this.setState({ showModal: true }); |
| 33 | + } |
| 34 | + |
| 35 | + handleValidate() { |
| 36 | + if(this.state.viewpoints.length > 0 || this.state.corpora.length > 0) { |
| 37 | + console.log(this.state.viewpoints) |
| 38 | + console.log(this.state.corpora) |
| 39 | + this.nameDuplicatedPortfolio = $('#portfolioDuplicatedName').val() |
| 40 | + this.handleClose(); |
| 41 | + this.setState({ showModalConfirmation: true}) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + async handleConfirm() { |
| 46 | + this.setState({ showModalConfirmation: false, showToast: true }); |
| 47 | + |
| 48 | + var ids = this.state.viewpoints.concat(this.state.corpora) |
| 49 | + let that = this |
| 50 | + |
| 51 | + await Promise.all(ids.map(id => that.addUserToEntity(id))) |
| 52 | + //refresh page |
| 53 | + this.setState({showToast: false }); |
| 54 | + window.location.replace("http://" + this.nameDuplicatedPortfolio + ".local:3000"); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + addUserToEntity(user){ |
| 59 | + let db = hypertopic([ |
| 60 | + "http://localhost", |
| 61 | + "http://steatite.hypertopic.org" |
| 62 | + ]); |
| 63 | + |
| 64 | + const _error = (x) => { |
| 65 | + console.error(x.message) |
| 66 | + return x |
| 67 | + }; |
| 68 | + |
| 69 | + return db.get({_id:user}) |
| 70 | + .then( (x) => { |
| 71 | + if(!x.users.includes(this.nameDuplicatedPortfolio)) |
| 72 | + x.users.push(this.nameDuplicatedPortfolio) |
| 73 | + return x |
| 74 | + }) |
| 75 | + .then(db.post) |
| 76 | + .then((x) => { |
| 77 | + console.log(x) |
| 78 | + }) |
| 79 | + .catch(_error); |
| 80 | + } |
| 81 | + |
| 82 | + closeToast() { |
| 83 | + this.setState(() => { |
| 84 | + return { |
| 85 | + showToast: false |
| 86 | + }; |
| 87 | + }); |
| 88 | + } |
| 89 | + onChangeCheckBox(e) { |
| 90 | + $('.checkCorpora').each(function() { |
| 91 | + if ($(this).is(":checked")) { |
| 92 | + $('.Corpus').each(function() { |
| 93 | + $(this).prop('checked', true); |
| 94 | + $(this).prop('disabled', true); |
| 95 | + }); |
| 96 | + }else{ |
| 97 | + $('.Corpus').each(function() { |
| 98 | + $(this).prop('disabled', false); |
| 99 | + }); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + $('.checkViewPoints').each(function() { |
| 104 | + if ($(this).is(":checked")) { |
| 105 | + $('.ViewPoint').each(function() { |
| 106 | + $(this).prop('checked', true); |
| 107 | + $(this).prop('disabled', true); |
| 108 | + }); |
| 109 | + }else{ |
| 110 | + $('.ViewPoint').each(function() { |
| 111 | + $(this).prop('disabled', false); |
| 112 | + }); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + var selectedViewPoints = []; |
| 117 | + $('.ViewPoint').each(function() { |
| 118 | + if ($(this).is(":checked")) { |
| 119 | + selectedViewPoints.push($(this).attr('value')); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + var selectedCorpora = []; |
| 124 | + $('.Corpus').each(function() { |
| 125 | + if ($(this).is(":checked")) { |
| 126 | + selectedCorpora.push($(this).attr('value')); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + this.setState({ |
| 131 | + corpora: selectedCorpora, |
| 132 | + viewpoints: selectedViewPoints |
| 133 | + }) |
| 134 | + } |
| 135 | + |
| 136 | + render() { |
| 137 | + let name = this.props.userConnected + '-' + this.props.portfolio |
| 138 | + let corpora = this.props.corpora.map((v, i) => |
| 139 | + <div className='Modal-Group' key={v.id}> |
| 140 | + <input className='Modal-CheckBox Corpus' value={v.id} onChange={this.onChangeCheckBox} type="checkbox"/> |
| 141 | + {v.id} |
| 142 | + </div> |
| 143 | + ); |
| 144 | + |
| 145 | + let viewpoints = this.props.viewpoints.map((v, i) => |
| 146 | + <div className='Modal-Group' key={v.id}> |
| 147 | + <input className='Modal-CheckBox ViewPoint' value={v.id} onChange={this.onChangeCheckBox} type="checkbox"/> |
| 148 | + {v.name} |
| 149 | + </div> |
| 150 | + ); |
| 151 | + |
| 152 | + |
| 153 | + return ( |
| 154 | + <div> |
| 155 | + <Modal show={this.state.showToast}> |
| 156 | + <Modal.Body> |
| 157 | + <h3>Redirection à la nouvelle page...</h3> |
| 158 | + </Modal.Body> |
| 159 | + </Modal> |
| 160 | + |
| 161 | + <Modal show={this.state.showModalConfirmation}> |
| 162 | + <Modal.Body> |
| 163 | + <h3>Créer le nouveau portfolio</h3> |
| 164 | + </Modal.Body> |
| 165 | + <Modal.Footer> |
| 166 | + <Button variant="secondary" onClick={this.handleClose}> |
| 167 | + Annuler |
| 168 | + </Button> |
| 169 | + <Button variant="primary" onClick={this.handleConfirm}> |
| 170 | + Confirmer |
| 171 | + </Button> |
| 172 | + </Modal.Footer> |
| 173 | + </Modal> |
| 174 | + |
| 175 | + <Modal show={this.state.showModal}> |
| 176 | + <Modal.Body> |
| 177 | + <h3>Nom du portfolio</h3> |
| 178 | + <input type='text' className='Modal-Input form-control' defaultValue={name} placeholder='Nom du nouveau portfolio' id="portfolioDuplicatedName"/> |
| 179 | + <div className='Modal-Group Modal-Title'> |
| 180 | + <h3>Corpus</h3> |
| 181 | + <div className='Modal-Group'><input className='Modal-CheckBox checkCorpora' type="checkbox" onChange={this.onChangeCheckBox}/>Tout</div> |
| 182 | + </div> |
| 183 | + <hr/> |
| 184 | + <div id="corporaList"> |
| 185 | + {corpora} |
| 186 | + </div> |
| 187 | + <br/> |
| 188 | + <div className='Modal-Group Modal-Title'> |
| 189 | + <h3>Points de vue</h3> |
| 190 | + <div className='Modal-Group'><input className='Modal-CheckBox checkViewPoints' type="checkbox" onChange={this.onChangeCheckBox}/>Tout</div> |
| 191 | + </div> |
| 192 | + <hr/> |
| 193 | + <div id="viewPointsList"> |
| 194 | + {viewpoints} |
| 195 | + </div> |
| 196 | + </Modal.Body> |
| 197 | + <Modal.Footer> |
| 198 | + <Button variant="secondary" onClick={this.handleClose}> |
| 199 | + Annuler |
| 200 | + </Button> |
| 201 | + <Button variant="primary" onClick={this.handleValidate}> |
| 202 | + Valider |
| 203 | + </Button> |
| 204 | + </Modal.Footer> |
| 205 | + </Modal> |
| 206 | + <Dropdown.Item eventKey="2" onClick={this.handleShow}>Dupliquer</Dropdown.Item> |
| 207 | + </div> |
| 208 | + |
| 209 | + ) |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +export default Duplicator; |
0 commit comments