-
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.
Merge pull request #29 from mseng10/light_model_support
ENH: Light creation endpoint
- Loading branch information
Showing
5 changed files
with
259 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import Modal from '@mui/material/Modal'; | ||
import TextField from '@mui/material/TextField'; | ||
import Box from '@mui/material/Box'; | ||
import IconButton from '@mui/material/IconButton'; | ||
import ButtonGroup from '@mui/material/ButtonGroup'; | ||
import CheckSharpIcon from '@mui/icons-material/CheckSharp'; | ||
import CloseSharpIcon from '@mui/icons-material/CloseSharp'; | ||
import Autocomplete from '@mui/material/Autocomplete'; | ||
import TungstenSharpIcon from '@mui/icons-material/TungstenSharp'; | ||
|
||
|
||
const NewLightForm = ({ isOpen, onRequestClose }) => { | ||
|
||
const [name, setName] = useState(''); | ||
const [system, setSystem] = useState(null); | ||
const [cost, setCost] = useState(0); | ||
|
||
const [allSystems, setAllSystems] = useState([]); | ||
|
||
const [submitted, setSubmitted] = useState(false); | ||
|
||
useEffect(() => { | ||
fetch('http://127.0.0.1:5000/system') | ||
.then((response) => response.json()) | ||
.then((data) => setAllSystems(data)) | ||
.catch((error) => console.error('Error fetching all system data:', error)); | ||
|
||
}, []); | ||
|
||
useEffect(() => { | ||
if (submitted) { | ||
const requestOptions = { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({name: name, cost: cost, system_id: system.id }) | ||
}; | ||
fetch('http://127.0.0.1:5000/light', requestOptions) | ||
.then(response => response.json()) | ||
.then(data => { | ||
// handle the response data if needed | ||
// maybe update some state based on the response | ||
console.log(data); | ||
}) | ||
.catch(error => console.error('Error posting plants data:', error)); | ||
clearForm(); | ||
onRequestClose(); | ||
} | ||
}, [submitted, name, cost, system, onRequestClose]); | ||
|
||
const handleSubmit = (event) => { | ||
event.preventDefault(); | ||
setSubmitted(true); // Update submitted state | ||
}; | ||
|
||
const handleCancel = () => { | ||
clearForm(); | ||
onRequestClose(); | ||
}; | ||
|
||
const clearForm = () => { | ||
setName(''); | ||
setCost(0); | ||
setSystem(null); | ||
setSubmitted(false); | ||
}; | ||
|
||
return ( | ||
<Modal | ||
open={isOpen} | ||
onClose={onRequestClose} | ||
aria-labelledby="new-bobby-form" | ||
disableAutoFocus={true} | ||
style={{ | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
backgroundColor: 'inherit', | ||
border: 'none', | ||
}} | ||
> | ||
<Box sx={{ width: 560, bgcolor: 'background.paper', borderRadius: 2 }}> | ||
<form onSubmit={handleSubmit}> | ||
<div className='left'> | ||
<TungstenSharpIcon sx={{ color: '#ffeb3b'}} className={submitted ? 'home_icon_form_submit' : 'home_icon_form'}/> | ||
<ButtonGroup> | ||
<IconButton className="left_button" type="submit" color="primary"> | ||
<CheckSharpIcon className="left_button"/> | ||
</IconButton> | ||
<IconButton className="left_button" color="error" onClick={handleCancel}> | ||
<CloseSharpIcon className="left_button"/> | ||
</IconButton> | ||
</ButtonGroup> | ||
</div> | ||
<div className='right'> | ||
<TextField | ||
margin="normal" | ||
fullWidth | ||
required | ||
label="Name" | ||
value={name} | ||
variant="standard" | ||
onChange={(event) => setName(event.target.value)} | ||
/> | ||
<Autocomplete | ||
freeSolo | ||
disableClearable | ||
value={system ? system.name : ''} | ||
options={allSystems.map((option) => option.name)} | ||
onChange={(event) => setSystem(allSystems[event.target.value])} | ||
renderInput={(params) => ( | ||
<TextField | ||
variant="standard" | ||
{...params} | ||
label="System" | ||
InputProps={{ | ||
...params.InputProps, | ||
type: 'search', | ||
}} | ||
/> | ||
)} | ||
/> | ||
<TextField | ||
margin="normal" | ||
fullWidth | ||
required | ||
type="number" | ||
label="Cost" | ||
value={cost} | ||
onChange={(event) => setCost(event.target.value)} | ||
variant="standard" | ||
/> | ||
</div> | ||
</form> | ||
</Box> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default NewLightForm; |
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