/* Usage: <%= react_component("PrivacyToggle", { content: content.attributes.slice('id', 'name', 'page_type', 'privacy'), content_icon: content.class.icon, content_color: content.class.color, submit_path: polymorphic_path(content), csrf_token: form_authenticity_token }) %> */ import React from "react" import PropTypes from "prop-types" import Radio from '@material-ui/core/Radio'; import RadioGroup from '@material-ui/core/RadioGroup'; import FormHelperText from '@material-ui/core/FormHelperText'; import FormControlLabel from '@material-ui/core/FormControlLabel'; import FormControl from '@material-ui/core/FormControl'; import FormLabel from '@material-ui/core/FormLabel'; import axios from 'axios'; class PrivacyToggle extends React.Component { constructor(props) { super(props); var initial_privacy = this.props.content.privacy; if (initial_privacy == null) { // Default null privacy settings to private initial_privacy = 'private'; } this.state = { privacy: initial_privacy }; console.log('Privacy: ' + this.state.privacy); axios.defaults.headers.common['X-CSRF-TOKEN'] = this.props.csrf_token; } async autosave() { var new_privacy_setting = (this.state.privacy == 'public' ? 'private' : 'public'); console.log('changing privacy to --> ' + new_privacy_setting); await axios.patch( this.props.submit_path, { [this.props.content.page_type.toLowerCase()]: { privacy: new_privacy_setting } }, { headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } } ); this.setState({ privacy: new_privacy_setting }) } render () { return ( {this.props.content_icon} {this.props.content.name}'s privacy: this.autosave()} > } label="Private (default)" labelPlacement="start" className="black-text" /> } label="Visible to anyone with the URL" labelPlacement="start" className="black-text" /> {/* } label="Discoverable" labelPlacement="start" /> */} {/* You can change this page's privacy setting at any time. */} ); } } PrivacyToggle.propTypes = { content: PropTypes.exact({ id: PropTypes.number.isRequired, name: PropTypes.string.isRequired, page_type: PropTypes.string.isRequired, privacy: PropTypes.string.isRequired }).isRequired, content_icon: PropTypes.string.isRequired, content_color: PropTypes.string.isRequired, submit_path: PropTypes.string.isRequired }; export default PrivacyToggle;