const React = window.React; class SyncPolicy extends React.Component { constructor(props) { super(props); this.state = {editMode: false}; this.accountId = props.accountId; } edit() { this.setState({editMode: true}) } save() { const req = new XMLHttpRequest(); const url = `${window.location.protocol}/sync-policy/${this.accountId}`; req.open("POST", url, true); req.setRequestHeader("Content-type", "application/json"); req.onreadystatechange = () => { if (req.readyState === XMLHttpRequest.DONE) { console.log(req.responseText); if (req.status === 200) { this.setState({editMode: false}); } } } const newPolicy = document.getElementById(`sync-policy-${this.accountId}`).value; req.send(JSON.stringify({sync_policy: newPolicy})); } cancel() { this.setState({editMode: false}); } render() { if (this.state.editMode) { const id = `sync-policy-${this.props.accountId}`; return (
Sync Policy
this.cancel.call(this)}> Cancel
) } return (
Sync Policy
{this.props.stringifiedSyncPolicy}
this.edit.call(this)}> Edit
) } } SyncPolicy.propTypes = { accountId: React.PropTypes.number, stringifiedSyncPolicy: React.PropTypes.string, } window.SyncPolicy = SyncPolicy;