2018-03-04 16:49:24 +00:00
|
|
|
import React from 'react';
|
|
|
|
import {withNotifier} from '../components/SnackbarNotification';
|
|
|
|
|
2018-03-04 17:36:04 +00:00
|
|
|
/*
|
|
|
|
* It is unlikely this application will grow complex enough to require redux.
|
|
|
|
*
|
|
|
|
* This HOC acts as an interface to a REST service, providing data and change
|
|
|
|
* event callbacks to the wrapped components along with a function to persist the
|
|
|
|
* changes.
|
|
|
|
*/
|
2018-03-04 16:49:24 +00:00
|
|
|
export const restComponent = (endpointUrl, FormComponent) => {
|
|
|
|
|
|
|
|
return withNotifier(
|
|
|
|
class extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2018-03-04 17:36:04 +00:00
|
|
|
this.state={
|
|
|
|
data:null,
|
|
|
|
fetched: false,
|
|
|
|
errorMessage:null
|
|
|
|
};
|
|
|
|
|
2018-03-04 16:49:24 +00:00
|
|
|
this.setState = this.setState.bind(this);
|
|
|
|
this.loadData = this.loadData.bind(this);
|
|
|
|
this.saveData = this.saveData.bind(this);
|
|
|
|
this.setData = this.setData.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(data) {
|
|
|
|
this.setState({
|
|
|
|
data:data,
|
|
|
|
fetched: true,
|
|
|
|
errorMessage:null
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
loadData() {
|
|
|
|
this.setState({
|
|
|
|
data:null,
|
|
|
|
fetched: false,
|
|
|
|
errorMessage:null
|
|
|
|
});
|
|
|
|
fetch(endpointUrl)
|
|
|
|
.then(response => {
|
|
|
|
if (response.status === 200) {
|
|
|
|
return response.json();
|
|
|
|
}
|
|
|
|
throw Error("Invalid status code: " + response.status);
|
|
|
|
})
|
|
|
|
.then(json => {this.setState({data: json, fetched:true})})
|
|
|
|
.catch(error =>{
|
|
|
|
this.props.raiseNotification("Problem fetching: " + error.message);
|
|
|
|
this.setState({data: null, fetched:true, errorMessage:error.message});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
saveData(e) {
|
|
|
|
this.setState({fetched: false});
|
|
|
|
fetch(endpointUrl, {
|
|
|
|
method: 'POST',
|
2018-03-04 17:36:04 +00:00
|
|
|
body: JSON.stringify(this.state.data),
|
2018-03-04 16:49:24 +00:00
|
|
|
headers: new Headers({
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
if (response.status === 200) {
|
|
|
|
return response.json();
|
|
|
|
}
|
|
|
|
throw Error("Invalid status code: " + response.status);
|
|
|
|
})
|
|
|
|
.then(json => {
|
|
|
|
this.props.raiseNotification("Changes successfully applied.");
|
|
|
|
this.setState({data: json, fetched:true});
|
|
|
|
}).catch(error => {
|
|
|
|
this.props.raiseNotification("Problem saving: " + error.message);
|
|
|
|
this.setState({data: null, fetched:true, errorMessage:error.message});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-04 17:36:04 +00:00
|
|
|
handleValueChange = name => event => {
|
2018-03-04 16:49:24 +00:00
|
|
|
const { data } = this.state;
|
|
|
|
data[name] = event.target.value;
|
|
|
|
this.setState({data});
|
|
|
|
};
|
|
|
|
|
2018-03-04 17:36:04 +00:00
|
|
|
handleCheckboxChange = name => event => {
|
2018-03-04 16:49:24 +00:00
|
|
|
const { data } = this.state;
|
|
|
|
data[name] = event.target.checked;
|
|
|
|
this.setState({data});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <FormComponent
|
2018-03-04 17:36:04 +00:00
|
|
|
handleValueChange={this.handleValueChange}
|
|
|
|
handleCheckboxChange={this.handleCheckboxChange}
|
2018-03-04 16:49:24 +00:00
|
|
|
setData={this.setData}
|
|
|
|
saveData={this.saveData}
|
|
|
|
loadData={this.loadData}
|
|
|
|
{...this.state}
|
|
|
|
{...this.props}
|
|
|
|
/>;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|