rjwats@gmail.com
6 years ago
6 changed files with 18 additions and 108 deletions
-
2interface/src/containers/NTPSettings.js
-
2interface/src/containers/OTASettings.js
-
21interface/src/containers/WiFiSettings.js
-
24interface/src/forms/WiFiSettingsForm.js
-
35interface/src/helpers/SimpleGet.js
-
38interface/src/helpers/SimplePost.js
@ -1,35 +0,0 @@ |
|||
/** |
|||
* Executes a get request for an endpoint, updating the local state of the calling |
|||
* component. The calling component must bind setState before using this |
|||
* function. |
|||
* |
|||
* This is designed for re-use in simple situations, we arn't using redux here! |
|||
*/ |
|||
export const simpleGet = ( |
|||
endpointUrl, |
|||
setState, |
|||
raiseNotification = null, |
|||
dataKey="status", |
|||
fetchedKey="fetched", |
|||
errorMessageKey = "errorMessage" |
|||
) => { |
|||
setState({ |
|||
[dataKey]:null, |
|||
[fetchedKey]: false, |
|||
[errorMessageKey]:null |
|||
}); |
|||
fetch(endpointUrl) |
|||
.then(response => { |
|||
if (response.status === 200) { |
|||
return response.json(); |
|||
} |
|||
throw Error("Invalid status code: " + response.status); |
|||
}) |
|||
.then(json => {setState({[dataKey]: json, [fetchedKey]:true})}) |
|||
.catch(error =>{ |
|||
if (raiseNotification) { |
|||
raiseNotification("Problem fetching: " + error.message); |
|||
} |
|||
setState({[dataKey]: null, [fetchedKey]:true, [errorMessageKey]:error.message}); |
|||
}); |
|||
} |
@ -1,38 +0,0 @@ |
|||
/** |
|||
* Executes a post request for saving data to an endpoint, updating the local |
|||
* state with the response. The calling component must bind setState before |
|||
* using this function. |
|||
* |
|||
* This is designed for re-use in simple situations, we arn't using redux here! |
|||
*/ |
|||
export const simplePost = ( |
|||
endpointUrl, |
|||
state, |
|||
setState, |
|||
raiseNotification = null, |
|||
dataKey="settings", |
|||
fetchedKey="fetched", |
|||
errorMessageKey = "errorMessage" |
|||
) => { |
|||
setState({[fetchedKey]: false}); |
|||
fetch(endpointUrl, { |
|||
method: 'POST', |
|||
body: JSON.stringify(state[dataKey]), |
|||
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 => { |
|||
raiseNotification("Changes successfully applied."); |
|||
setState({[dataKey]: json, [fetchedKey]:true}); |
|||
}).catch(error => { |
|||
raiseNotification("Problem saving: " + error.message); |
|||
setState({[dataKey]: null, [fetchedKey]:true, [errorMessageKey]:error.message}); |
|||
}); |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue