|
1 | 1 | var React = require('react/addons') |
2 | 2 |
|
3 | | -var Catalyst = require('../Mixins/Catalyst') |
| 3 | +var Hq = require('../Services/Hq') |
4 | 4 |
|
5 | | -var ProfileImage = require('./ProfileImage') |
| 5 | +var LinkedState = require('../Mixins/LinkedState') |
| 6 | + |
| 7 | +var PlanetStore = require('../Stores/PlanetStore') |
6 | 8 |
|
7 | 9 | module.exports = React.createClass({ |
8 | | - mixins: [Catalyst.LinkedStateMixin], |
| 10 | + mixins: [LinkedState], |
9 | 11 | propTypes: { |
10 | 12 | close: React.PropTypes.func, |
11 | | - currentPlanet: React.PropTypes.object |
| 13 | + planet: React.PropTypes.shape({ |
| 14 | + name: React.PropTypes.string, |
| 15 | + public: React.PropTypes.bool, |
| 16 | + userName: React.PropTypes.string |
| 17 | + }) |
12 | 18 | }, |
13 | 19 | getInitialState: function () { |
| 20 | + var deleteTextCandidates = [ |
| 21 | + 'Confirm', |
| 22 | + 'Exterminatus', |
| 23 | + 'Avada Kedavra' |
| 24 | + ] |
| 25 | + var random = Math.round(Math.random() * 10) % 10 |
| 26 | + var randomDeleteText = random > 1 ? deleteTextCandidates[0] : random === 1 ? deleteTextCandidates[1] : deleteTextCandidates[2] |
| 27 | + |
14 | 28 | return { |
15 | | - currentTab: 'planetProfile', |
16 | | - planetName: this.props.currentPlanet.name, |
17 | | - isDeletePlanetChecked: false, |
18 | | - userName: '' |
| 29 | + currentTab: 'profile', |
| 30 | + planet: { |
| 31 | + name: this.props.planet.name, |
| 32 | + public: this.props.planet.public |
| 33 | + }, |
| 34 | + randomDeleteText: randomDeleteText, |
| 35 | + deleteConfirmation: '' |
19 | 36 | } |
20 | 37 | }, |
21 | 38 | activePlanetProfile: function () { |
22 | | - this.setState({currentTab: 'planetProfile'}) |
| 39 | + this.setState({currentTab: 'profile'}) |
23 | 40 | }, |
24 | | - saveProfile: function () { |
25 | | - var currentPlanet = this.props.currentPlanet |
26 | | - PlanetActions.changeName(currentPlanet.userName, currentPlanet.name, this.state.planetName) |
| 41 | + activePlanetDelete: function () { |
| 42 | + this.setState({currentTab: 'delete'}) |
27 | 43 | }, |
28 | | - handleChange: function (value) { |
29 | | - this.setState({userName: value}) |
30 | | - }, |
31 | | - doubleCheckDeletePlanet: function () { |
32 | | - if (this.state.isDeletePlanetChecked) { |
33 | | - PlanetActions.deletePlanet(this.props.currentPlanet.userName, this.props.currentPlanet.name) |
34 | | - return |
35 | | - } |
36 | | - this.setState({isDeletePlanetChecked: true}) |
37 | | - React.findDOMNode(this.refs.deleteCancelButton).focus() |
| 44 | + handlePublicChange: function (value) { |
| 45 | + return function () { |
| 46 | + this.state.planet.public = value |
| 47 | + this.setState({planet: this.state.planet}) |
| 48 | + }.bind(this) |
38 | 49 | }, |
39 | | - cancelDeletePlanet: function () { |
40 | | - this.setState({isDeletePlanetChecked: false}) |
| 50 | + handleSavePlanetProfile: function (e) { |
| 51 | + var planet = this.props.planet |
| 52 | + |
| 53 | + this.setState({profileSubmitStatus: 'sending'}, function () { |
| 54 | + Hq.updatePlanet(planet.userName, planet.name, this.state.planet) |
| 55 | + .then(function (res) { |
| 56 | + var planet = res.body |
| 57 | + |
| 58 | + this.setState({profileSubmitStatus: 'done'}) |
| 59 | + |
| 60 | + PlanetStore.Actions.update(planet) |
| 61 | + }.bind(this)) |
| 62 | + .catch(function (err) { |
| 63 | + this.setState({profileSubmitStatus: 'error'}) |
| 64 | + console.error(err) |
| 65 | + }.bind(this)) |
| 66 | + }) |
41 | 67 | }, |
42 | | - interceptClick: function (e) { |
43 | | - e.stopPropagation() |
| 68 | + handleDeletePlanetClick: function () { |
| 69 | + var planet = this.props.planet |
| 70 | + |
| 71 | + this.setState({deleteSubmitStatus: 'sending'}, function () { |
| 72 | + Hq.destroyPlanet(planet.userName, planet.name) |
| 73 | + .then(function (res) { |
| 74 | + var planet = res.body |
| 75 | + |
| 76 | + PlanetStore.Actions.destroy(planet) |
| 77 | + this.setState({deleteSubmitStatus: 'done'}, function () { |
| 78 | + this.props.close() |
| 79 | + }) |
| 80 | + }.bind(this)) |
| 81 | + .catch(function (err) { |
| 82 | + this.setState({deleteSubmitStatus: 'error'}) |
| 83 | + console.error(err) |
| 84 | + }.bind(this)) |
| 85 | + }) |
| 86 | + |
44 | 87 | }, |
45 | 88 | render: function () { |
46 | 89 | var content |
47 | 90 |
|
48 | | - content = ( |
49 | | - <div className='planetProfile'> |
50 | | - <div className='planetProfileForm'> |
| 91 | + content = this.state.currentTab === 'profile' ? this.renderPlanetProfileTab() : this.renderPlanetDeleteTab() |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className='PlanetSettingModal modal tabModal'> |
| 95 | + <div className='leftPane'> |
| 96 | + <h1 className='tabLabel'>Planet setting</h1> |
| 97 | + <nav className='tabList'> |
| 98 | + <button onClick={this.activePlanetProfile} className={this.state.currentTab === 'profile' ? 'active' : ''}><i className='fa fa-globe fa-fw'/> Planet profile</button> |
| 99 | + <button onClick={this.activePlanetDelete} className={this.state.currentTab === 'delete' ? 'active' : ''}><i className='fa fa-trash fa-fw'/> Delete Planet</button> |
| 100 | + </nav> |
| 101 | + </div> |
| 102 | + |
| 103 | + <div className='rightPane'> |
| 104 | + {content} |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + ) |
| 108 | + }, |
| 109 | + renderPlanetProfileTab: function () { |
| 110 | + return ( |
| 111 | + <div className='planetProfileTab'> |
| 112 | + <div className='formField'> |
51 | 113 | <label>Planet name </label> |
52 | | - <input valueLink={this.linkState('planetName')} className='inline-input'/> |
53 | | - <button onClick={this.saveProfile} className='saveButton btn-primary'>Save</button> |
| 114 | + <input valueLink={this.linkState('planet.name')}/> |
| 115 | + </div> |
| 116 | + |
| 117 | + <div className='formRadioField'> |
| 118 | + <input id='publicOption' checked={this.state.planet.public} onChange={this.handlePublicChange(true)} name='public' type='radio'/> <label htmlFor='publicOption'>Public</label> |
| 119 | + |
| 120 | + <input id='privateOption' checked={!this.state.planet.public} onChange={this.handlePublicChange(false)} name='public' type='radio'/> <label htmlFor='privateOption'>Private</label> |
54 | 121 | </div> |
| 122 | + <div className='formConfirm'> |
| 123 | + <button onClick={this.handleSavePlanetProfile} className='saveButton btn-primary'>Save</button> |
55 | 124 |
|
56 | | - <div className='planetDeleteForm'> |
57 | | - <div className='planetDeleteControl'> |
58 | | - <div className={'toggle' + (this.state.isDeletePlanetChecked ? '' : ' hide')}> |
59 | | - <div className='planetDeleteLabel'>Are you sure to delete this planet?</div> |
60 | | - <button ref='deleteCancelButton' onClick={this.cancelDeletePlanet} className='cancelButton btn-default'>Cancel</button> |
61 | | - </div> |
62 | | - <button onClick={this.doubleCheckDeletePlanet} className='deleteButton btn-primary'>{!this.state.isDeletePlanetChecked ? 'Delete Planet' : 'Confirm'}</button> |
63 | | - </div> |
| 125 | + <div className={'alertInfo' + (this.state.profileSubmitStatus === 'sending' ? '' : ' hide')}>on Sending...</div> |
| 126 | + |
| 127 | + <div className={'alertError' + (this.state.profileSubmitStatus === 'error' ? '' : ' hide')}>Connection failed.. Try again.</div> |
| 128 | + |
| 129 | + <div className={'alertSuccess' + (this.state.profileSubmitStatus === 'done' ? '' : ' hide')}>Successfully done!!</div> |
64 | 130 | </div> |
65 | 131 | </div> |
66 | 132 | ) |
| 133 | + }, |
| 134 | + renderPlanetDeleteTab: function () { |
| 135 | + var disabled = !this.state.deleteConfirmation.match(new RegExp('^' + this.props.planet.userName + '/' + this.props.planet.name + '$')) |
67 | 136 |
|
68 | 137 | return ( |
69 | | - <div onClick={this.interceptClick} className='PlanetSettingModal modal'> |
70 | | - <div className='settingNav'> |
71 | | - <h1>Planet setting</h1> |
72 | | - <nav> |
73 | | - <button className={this.state.currentTab === 'planetProfile' ? 'active' : ''} onClick={this.activePlanetProfile}><i className='fa fa-globe fa-fw'/> Planet profile</button> |
74 | | - </nav> |
75 | | - </div> |
| 138 | + <div className='planetDeleteTab'> |
| 139 | + <p>Are you sure to destroy <strong>'{this.props.planet.userName + '/' + this.props.planet.name}'</strong>?</p> |
| 140 | + <p>If you are sure, write <strong>'{this.props.planet.userName + '/' + this.props.planet.name}'</strong> to input below and click <strong>'{this.state.randomDeleteText}'</strong> button.</p> |
| 141 | + <input valueLink={this.linkState('deleteConfirmation')} placeholder='userName/planetName'/> |
| 142 | + <div className='formConfirm'> |
| 143 | + <button disabled={disabled} onClick={this.handleDeletePlanetClick}>{this.state.randomDeleteText}</button> |
76 | 144 |
|
77 | | - <div className='settingBody'> |
78 | | - {content} |
| 145 | + <div className={'alertInfo' + (this.state.deleteSubmitStatus === 'sending' ? '' : ' hide')}>on Sending...</div> |
| 146 | + |
| 147 | + <div className={'alertError' + (this.state.deleteSubmitStatus === 'error' ? '' : ' hide')}>Connection failed.. Try again.</div> |
| 148 | + |
| 149 | + <div className={'alertSuccess' + (this.state.deleteSubmitStatus === 'done' ? '' : ' hide')}>Successfully done!!</div> |
79 | 150 | </div> |
80 | 151 | </div> |
81 | 152 | ) |
|
0 commit comments