Created
April 3, 2021 17:57
-
-
Save code08-ind/6893fbb4a2c3163b58f2d7f87099f8e7 to your computer and use it in GitHub Desktop.
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
import React, { useState } from 'react'; | |
import Typography from '@material-ui/core/Typography'; | |
import Button from '@material-ui/core/Button'; | |
import Container from '@material-ui/core/Container'; | |
import SendIcon from '@material-ui/icons/Send'; | |
import { FormControlLabel, makeStyles } from '@material-ui/core'; | |
import TextField from '@material-ui/core/TextField'; | |
import Radio from '@material-ui/core/Radio'; | |
import RadioGroup from '@material-ui/core/RadioGroup'; | |
import FormControl from '@material-ui/core/FormControl'; | |
import FormLabel from '@material-ui/core/FormLabel'; | |
import { useHistory } from 'react-router-dom'; | |
const useStyles = makeStyles({ | |
field: { | |
marginTop: 20, | |
marginBottom: 20, | |
display: "block" | |
} | |
}) | |
const Create = () => { | |
const classes = useStyles(); | |
let history = useHistory(); | |
const [title, setTitle] = useState(''); | |
const [details, setDetails] = useState(''); | |
const [titleError, setTitleError] = useState(false); | |
const [detailsError, setDetailsError] = useState(false); | |
const [category, setCategory] = useState('todos'); | |
const handleSubmit = (e) => { | |
e.preventDefault(); | |
setTitleError(false); | |
setDetailsError(false); | |
if (title === '') { | |
setTitleError(true); | |
} | |
if (details === '') { | |
setDetailsError(true); | |
} | |
if (title && details) { | |
fetch('https://my-json-server.typicode.com/code08-ind/mockJson/notes', { | |
method: 'POST', | |
headers: { 'Content-type': 'application/json' }, | |
body: JSON.stringify({ | |
title: title, | |
details: details, | |
category: category | |
}) | |
}).then((response) => response.json()) | |
.then(() => history.push("/")) | |
} | |
} | |
return ( | |
<Container> | |
<Typography | |
variant="h6" | |
component="h2" | |
color="textSecondary" | |
gutterBottom | |
> | |
Create A New Note | |
</Typography> | |
<form noValidate autoComplete="off" onSubmit={handleSubmit}> | |
<TextField | |
onChange={(e) => setTitle(e.target.value)} | |
label="Notes Title" | |
placeholder="Enter The Notes Title" | |
variant="outlined" | |
color="secondary" | |
fullWidth | |
required | |
className={classes.field} | |
error={titleError} | |
/> | |
<TextField | |
onChange={(e) => setDetails(e.target.value)} | |
label="Notes Details" | |
placeholder="Enter The Notes Details" | |
variant="outlined" | |
color="secondary" | |
fullWidth | |
multiline | |
rows={5} | |
required | |
className={classes.field} | |
error={detailsError} | |
/> | |
<FormControl className={classes.field}> | |
<FormLabel>Note Category</FormLabel> | |
<RadioGroup value={category} onChange={(e) => { setCategory(e.target.value) }}> | |
<FormControlLabel | |
control={<Radio />} | |
value="money" | |
label="Money" | |
/> | |
<FormControlLabel | |
control={<Radio />} | |
value="todos" | |
label="Todos" | |
/> | |
<FormControlLabel | |
control={<Radio />} | |
value="reminders" | |
label="Reminders" | |
/> | |
<FormControlLabel | |
control={<Radio />} | |
value="work" | |
label="Work" | |
/> | |
</RadioGroup> | |
</FormControl> | |
<Button | |
type="submit" | |
color="secondary" | |
variant="contained" | |
endIcon={<SendIcon />} | |
> | |
Submit | |
</Button> | |
</form> | |
</Container> | |
); | |
} | |
export default Create; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment