Skip to content

Commit

Permalink
Updating the Admin container
Browse files Browse the repository at this point in the history
  • Loading branch information
ionofzion committed Nov 2, 2018
1 parent 2182aa8 commit d5fd443
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions app/containers/Admin.jsx
Expand Up @@ -7,40 +7,42 @@ export default class Admin extends Component {
constructor(props) {
super(props);

// Set the default states
this.state = {
exampleSetting: '',
savedExampleSetting: ''
email: '',
savedEmail: ''
};

this.fetchWP = new fetchWP({
restURL: this.props.wpObject.api_url,
restNonce: this.props.wpObject.api_nonce,
});

// Get the currently set email address from our /admin endpoint and update the email state accordingly
this.getSetting();
}

getSetting = () => {
this.fetchWP.get( 'example' )
this.fetchWP.get( 'admin' )
.then(
(json) => this.setState({
exampleSetting: json.value,
savedExampleSetting: json.value
email: json.value,
savedEmail: json.value
}),
(err) => console.log( 'error', err )
);
};

updateSetting = () => {
this.fetchWP.post( 'example', { exampleSetting: this.state.exampleSetting } )
this.fetchWP.post( 'admin', { email: this.state.email } )
.then(
(json) => this.processOkResponse(json, 'saved'),
(err) => console.log('error', err)
);
}

deleteSetting = () => {
this.fetchWP.delete( 'example' )
this.fetchWP.delete( 'admin' )
.then(
(json) => this.processOkResponse(json, 'deleted'),
(err) => console.log('error', err)
Expand All @@ -50,8 +52,8 @@ export default class Admin extends Component {
processOkResponse = (json, action) => {
if (json.success) {
this.setState({
exampleSetting: json.value,
savedExampleSetting: json.value,
email: json.value,
savedEmail: json.value,
});
} else {
console.log(`Setting was not ${action}.`, json);
Expand All @@ -60,13 +62,13 @@ export default class Admin extends Component {

updateInput = (event) => {
this.setState({
exampleSetting: event.target.value,
email: event.target.value,
});
}

handleSave = (event) => {
event.preventDefault();
if ( this.state.exampleSetting === this.state.savedExampleSetting ) {
if ( this.state.email === this.state.savedEmail ) {
console.log('Setting unchanged');
} else {
this.updateSetting();
Expand All @@ -79,16 +81,17 @@ export default class Admin extends Component {
}

render() {
// Here we update the contents of the h1, and the label, type and value of the input.
return (
<div className="wrap">
<form>
<h1>WP Reactivate Settings</h1>
<h1>Contact Form Settings</h1>

<label>
Example Setting:
Notification Email:
<input
type="text"
value={this.state.exampleSetting}
type="email"
value={this.state.email}
onChange={this.updateInput}
/>
</label>
Expand Down

0 comments on commit d5fd443

Please sign in to comment.