var React = require('react/addons') var Hq = require('../Services/Hq') var LinkedState = require('../Mixins/LinkedState') var KeyCaster = require('../Mixins/KeyCaster') var PlanetStore = require('../Stores/PlanetStore') module.exports = React.createClass({ mixins: [LinkedState, KeyCaster('planetSettingModal')], propTypes: { close: React.PropTypes.func, planet: React.PropTypes.shape({ name: React.PropTypes.string, public: React.PropTypes.bool, Owner: React.PropTypes.shape({ name: React.PropTypes.string }) }) }, getInitialState: function () { var deleteTextCandidates = [ 'Confirm', 'Exterminatus', 'Avada Kedavra' ] var random = Math.round(Math.random() * 10) % 10 var randomDeleteText = random > 1 ? deleteTextCandidates[0] : random === 1 ? deleteTextCandidates[1] : deleteTextCandidates[2] return { currentTab: 'profile', planet: { name: this.props.planet.name, public: this.props.planet.public }, randomDeleteText: randomDeleteText, deleteConfirmation: '' } }, onKeyCast: function (e) { switch (e.status) { case 'closeModal': this.props.close() break } }, activePlanetProfile: function () { this.setState({currentTab: 'profile'}) }, activePlanetDelete: function () { this.setState({currentTab: 'delete'}) }, handlePublicChange: function (value) { return function () { this.state.planet.public = value this.setState({planet: this.state.planet}) }.bind(this) }, handleSavePlanetProfile: function (e) { var planet = this.props.planet this.setState({profileFormStatus: 'sending', profileFormError: null}, function () { Hq.updatePlanet(planet.Owner.name, planet.name, this.state.planet) .then(function (res) { var planet = res.body console.log(planet) this.setState({profileFormStatus: 'done'}) PlanetStore.Actions.update(planet) this.props.close() }.bind(this)) .catch(function (err) { console.error(err) var newState = { profileFormStatus: 'error' } if (err.status == null) { newState.profileFormError = {message: 'Check your network connection'} return this.setState(newState) } switch (err.status) { case 403: newState.profileFormError = err.response.body this.setState(newState) break case 422: newState.profileFormError = {message: 'Planet name should be Alphanumeric with _, -'} this.setState(newState) break case 409: newState.profileFormError = {message: 'The entered name already in use'} this.setState(newState) break default: newState.profileFormError = {message: 'Undefined error please try again'} this.setState(newState) } }.bind(this)) }) }, handleDeletePlanetClick: function () { var planet = this.props.planet this.setState({deleteSubmitStatus: 'sending'}, function () { Hq.destroyPlanet(planet.Owner.name, planet.name) .then(function (res) { var planet = res.body PlanetStore.Actions.destroy(planet) this.setState({deleteSubmitStatus: 'done'}, function () { this.props.close() }) }.bind(this)) .catch(function (err) { this.setState({deleteSubmitStatus: 'error'}) console.error(err) }.bind(this)) }) }, render: function () { var content content = this.state.currentTab === 'profile' ? this.renderPlanetProfileTab() : this.renderPlanetDeleteTab() return (

Planet setting

{content}
) }, renderPlanetProfileTab: function () { return (
on Sending...
{this.state.profileFormError != null ? this.state.profileFormError.message : 'Unexpected error occured! please try again'}
Successfully done!!
) }, renderPlanetDeleteTab: function () { var disabled = !this.state.deleteConfirmation.match(new RegExp('^' + this.props.planet.Owner.name + '/' + this.props.planet.name + '$')) return (

Are you sure to destroy '{this.props.planet.Owner.name + '/' + this.props.planet.name}'?

If you are sure, write '{this.props.planet.Owner.name + '/' + this.props.planet.name}' to input below and click '{this.state.randomDeleteText}' button.

on Sending...
Connection failed.. Try again.
Successfully done!!
) } })