-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reflect server's running state on load (#26)
* Add ui call to backend for running state * Refactor StatusSwitch to use hooks * Store running state (#27) * Add running state into postgres for persistence * Rename paused to isRunning and refactor store * Use atomicBool for concurrency safety * Use async/await pattern
- Loading branch information
Showing
8 changed files
with
153 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,54 @@ | ||
import React from "react"; | ||
import React, { useState, useEffect } from "react"; | ||
import FormControlLabel from "@material-ui/core/FormControlLabel"; | ||
import Switch from "@material-ui/core/Switch"; | ||
import axios from "axios"; | ||
|
||
class StatusSwitch extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
const StatusSwitch = () => { | ||
const [isRunning, setIsRunning] = useState(true); | ||
|
||
this.state = { | ||
checked: true, | ||
status: "Running" | ||
}; | ||
useEffect(() => { | ||
const stateUrl = new URL( | ||
"/state", | ||
window.config.env.REACT_APP_NOPAS_ENDPOINT | ||
); | ||
|
||
this.handleChange = this.handleChange.bind(this); | ||
} | ||
axios.get(stateUrl) | ||
.then(rsp => setIsRunning(rsp.data)) | ||
.catch(function (error) { | ||
console.log(error); | ||
}) | ||
}, []); | ||
|
||
handleChange(event) { | ||
if (event.target.checked) { | ||
const resumeUrl = new URL( | ||
"/resume", | ||
window.config.env.REACT_APP_NOPAS_ENDPOINT | ||
); | ||
axios.put(resumeUrl); | ||
} else { | ||
const pauseUrl = new URL( | ||
"/pause", | ||
window.config.env.REACT_APP_NOPAS_ENDPOINT | ||
); | ||
axios.put(pauseUrl); | ||
const handleChange = async (event) => { | ||
try { | ||
if (event.target.checked) { | ||
const resumeUrl = new URL( | ||
"/state/resume", | ||
window.config.env.REACT_APP_NOPAS_ENDPOINT | ||
); | ||
await axios.put(resumeUrl) | ||
setIsRunning(true) | ||
} else { | ||
const pauseUrl = new URL( | ||
"/state/pause", | ||
window.config.env.REACT_APP_NOPAS_ENDPOINT | ||
); | ||
await axios.put(pauseUrl) | ||
setIsRunning(false) | ||
} | ||
} catch(err) { | ||
console.log(err) | ||
} | ||
|
||
this.setState({ | ||
checked: event.target.checked, | ||
status: event.target.checked ? "Running" : "Paused" | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<FormControlLabel | ||
control={ | ||
<Switch checked={this.state.checked} onChange={this.handleChange} /> | ||
} | ||
label={this.state.status} | ||
/> | ||
); | ||
} | ||
return ( | ||
<FormControlLabel | ||
control={ | ||
<Switch checked={isRunning} onChange={handleChange} /> | ||
} | ||
label={isRunning ? "Running" : "Paused"} | ||
/> | ||
); | ||
} | ||
|
||
export default StatusSwitch; |