Last active
October 12, 2021 12:58
-
-
Save aidiary/be75e7bec3c6d58abffec08ba17de4ff to your computer and use it in GitHub Desktop.
Material UI Tutorial https://www.youtube.com/playlist?list=PL4cUxeGkcC9gjxLvV4VEkZ6H6H4yWuS58
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 { purple, red } from '@mui/material/colors'; | |
import { createTheme, ThemeProvider } from '@mui/material/styles'; | |
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; | |
import Layout from './components/Layout'; | |
import Create from './pages/Create'; | |
import Notes from './pages/Notes'; | |
const theme = createTheme({ | |
palette: { | |
primary: red, | |
secondary: purple, | |
}, | |
typography: { | |
fontFamily: 'Quicksand', | |
fontWeightLight: 400, | |
fontWeightRegular: 500, | |
fontWeightMedium: 600, | |
fontWeightBold: 700, | |
}, | |
}); | |
function App() { | |
return ( | |
<ThemeProvider theme={theme}> | |
<Router> | |
<Layout> | |
<Switch> | |
<Route exact path='/'> | |
<Notes /> | |
</Route> | |
<Route path='/create'> | |
<Create /> | |
</Route> | |
</Switch> | |
</Layout> | |
</Router> | |
</ThemeProvider> | |
); | |
} | |
export default App; |
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 KeyboardArrowRightIcon from "@mui/icons-material/KeyboardArrowRight"; | |
import Button from "@mui/material/Button"; | |
import Container from "@mui/material/Container"; | |
import FormControl from "@mui/material/FormControl"; | |
import FormControlLabel from "@mui/material/FormControlLabel"; | |
import FormLabel from "@mui/material/FormLabel"; | |
import Radio from "@mui/material/Radio"; | |
import RadioGroup from "@mui/material/RadioGroup"; | |
import TextField from "@mui/material/TextField"; | |
import Typography from "@mui/material/Typography"; | |
import React, { useState } from "react"; | |
import { useHistory } from "react-router"; | |
export default function Create() { | |
const 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("http://localhost:8000/notes", { | |
method: "POST", | |
headers: { "Content-type": "application/json" }, | |
body: JSON.stringify({ title, details, category }), | |
}).then(() => history.push("/")); | |
} | |
}; | |
return ( | |
<Container> | |
<Typography | |
variant="h6" | |
color="textSecondary" | |
component="h2" | |
gutterBottom | |
> | |
Create a New Note | |
</Typography> | |
<form noValidate autoComplete="off" onSubmit={handleSubmit}> | |
<TextField | |
onChange={(e) => setTitle(e.target.value)} | |
sx={{ | |
marginTop: "20px", | |
marginBottom: "20px", | |
display: "block", | |
}} | |
label="Note Title" | |
variant="outlined" | |
color="secondary" | |
fullWidth | |
required | |
error={titleError} | |
></TextField> | |
<TextField | |
onChange={(e) => setDetails(e.target.value)} | |
sx={{ | |
marginTop: "20px", | |
marginBottom: "20px", | |
display: "block", | |
}} | |
label="Details" | |
variant="outlined" | |
color="secondary" | |
multiline | |
rows={4} | |
fullWidth | |
required | |
error={detailsError} | |
></TextField> | |
<FormControl | |
sx={{ | |
marginTop: "20px", | |
marginBottom: "20px", | |
display: "block", | |
}} | |
> | |
<FormLabel>Note Category</FormLabel> | |
<RadioGroup | |
value={category} | |
onChange={(e) => setCategory(e.target.value)} | |
> | |
<FormControlLabel value="money" control={<Radio />} label="Money" /> | |
<FormControlLabel value="todos" control={<Radio />} label="Todos" /> | |
<FormControlLabel | |
value="reminders" | |
control={<Radio />} | |
label="Reminders" | |
/> | |
<FormControlLabel value="work" control={<Radio />} label="Work" /> | |
</RadioGroup> | |
</FormControl> | |
<Button | |
type="submit" | |
color="secondary" | |
variant="contained" | |
endIcon={<KeyboardArrowRightIcon />} | |
> | |
Submit | |
</Button> | |
</form> | |
</Container> | |
); | |
} |
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 { AddCircleOutlineOutlined, SubjectOutlined } from '@mui/icons-material'; | |
import { ListItemIcon } from '@mui/material'; | |
import AppBar from '@mui/material/AppBar'; | |
import Avatar from '@mui/material/Avatar'; | |
import Drawer from '@mui/material/Drawer'; | |
import List from '@mui/material/List'; | |
import ListItem from '@mui/material/ListItem'; | |
import ListItemText from '@mui/material/ListItemText'; | |
import Toolbar from '@mui/material/Toolbar'; | |
import Typography from '@mui/material/Typography'; | |
import { format } from 'date-fns'; | |
import React from 'react'; | |
import { useHistory, useLocation } from 'react-router'; | |
import './Layout.css'; | |
export default function Layout({ children }) { | |
const history = useHistory(); | |
const location = useLocation(); | |
const menuItems = [ | |
{ | |
text: 'My Notes', | |
icon: <SubjectOutlined color='secondary' />, | |
path: '/', | |
}, | |
{ | |
text: 'Create Note', | |
icon: <AddCircleOutlineOutlined color='secondary' />, | |
path: '/create', | |
}, | |
]; | |
return ( | |
<div className='root'> | |
<AppBar sx={{ width: 'calc(100% - 240px)' }} elevation={0}> | |
<Toolbar> | |
<Typography className='date'> | |
Today is the {format(new Date(), 'do MMMM Y')} | |
</Typography> | |
<Typography>Mario</Typography> | |
<Avatar src='/IMG_7018.png' className='avatar' /> | |
</Toolbar> | |
</AppBar> | |
<Drawer sx={{ width: 240 }} variant='permanent' anchor='left'> | |
<div> | |
<Typography variant='h5' className='title'> | |
Ninja Notes | |
</Typography> | |
</div> | |
<List sx={{ width: 240 }}> | |
{menuItems.map((item) => ( | |
<ListItem | |
button | |
key={item.text} | |
onClick={() => history.push(item.path)} | |
sx={ | |
location.pathname === item.path | |
? { background: '#f4f4f4' } | |
: null | |
} | |
> | |
<ListItemIcon>{item.icon}</ListItemIcon> | |
<ListItemText primary={item.text} /> | |
</ListItem> | |
))} | |
</List> | |
</Drawer> | |
<div className='page'> | |
<div className='toolbar'></div> | |
<div>{children}</div> | |
</div> | |
</div> | |
); | |
} |
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 { DeleteOutlined } from '@mui/icons-material'; | |
import { Avatar, IconButton, Typography } from '@mui/material'; | |
import Card from '@mui/material/Card'; | |
import CardContent from '@mui/material/CardContent'; | |
import CardHeader from '@mui/material/CardHeader'; | |
import { blue, green, pink, yellow } from '@mui/material/colors'; | |
import React from 'react'; | |
import './NoteCard.css'; | |
export default function NoteCard({ note, handleDelete }) { | |
// console.log(note.category); | |
let avatarColor; | |
if (note.category === 'work') { | |
avatarColor = { bgcolor: yellow[700] }; | |
} else if (note.category === 'money') { | |
avatarColor = { bgcolor: green[500] }; | |
} else if (note.category === 'todos') { | |
avatarColor = { bgcolor: pink[500] }; | |
} else { | |
avatarColor = { bgcolor: blue[500] }; | |
} | |
// console.log('***', avatarClass); | |
return ( | |
<div> | |
<Card elevation={3}> | |
<CardHeader | |
avatar={ | |
<Avatar sx={avatarColor}>{note.category[0].toUpperCase()}</Avatar> | |
} | |
action={ | |
<IconButton onClick={() => handleDelete(note.id)}> | |
<DeleteOutlined /> | |
</IconButton> | |
} | |
title={note.title} | |
subheader={note.category} | |
></CardHeader> | |
<CardContent> | |
<Typography variant='body2' color='textSecondary'> | |
{note.details} | |
</Typography> | |
</CardContent> | |
</Card> | |
</div> | |
); | |
} |
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 KeyboardArrowRightIcon from "@mui/icons-material/KeyboardArrowRight"; | |
import Button from "@mui/material/Button"; | |
import Container from "@mui/material/Container"; | |
import FormControl from "@mui/material/FormControl"; | |
import FormControlLabel from "@mui/material/FormControlLabel"; | |
import FormLabel from "@mui/material/FormLabel"; | |
import Radio from "@mui/material/Radio"; | |
import RadioGroup from "@mui/material/RadioGroup"; | |
import TextField from "@mui/material/TextField"; | |
import Typography from "@mui/material/Typography"; | |
import React, { useState } from "react"; | |
import { useHistory } from "react-router"; | |
export default function Create() { | |
const 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("http://localhost:8000/notes", { | |
method: "POST", | |
headers: { "Content-type": "application/json" }, | |
body: JSON.stringify({ title, details, category }), | |
}).then(() => history.push("/")); | |
} | |
}; | |
return ( | |
<Container> | |
<Typography | |
variant="h6" | |
color="textSecondary" | |
component="h2" | |
gutterBottom | |
> | |
Create a New Note | |
</Typography> | |
<form noValidate autoComplete="off" onSubmit={handleSubmit}> | |
<TextField | |
onChange={(e) => setTitle(e.target.value)} | |
sx={{ | |
marginTop: "20px", | |
marginBottom: "20px", | |
display: "block", | |
}} | |
label="Note Title" | |
variant="outlined" | |
color="secondary" | |
fullWidth | |
required | |
error={titleError} | |
></TextField> | |
<TextField | |
onChange={(e) => setDetails(e.target.value)} | |
sx={{ | |
marginTop: "20px", | |
marginBottom: "20px", | |
display: "block", | |
}} | |
label="Details" | |
variant="outlined" | |
color="secondary" | |
multiline | |
rows={4} | |
fullWidth | |
required | |
error={detailsError} | |
></TextField> | |
<FormControl | |
sx={{ | |
marginTop: "20px", | |
marginBottom: "20px", | |
display: "block", | |
}} | |
> | |
<FormLabel>Note Category</FormLabel> | |
<RadioGroup | |
value={category} | |
onChange={(e) => setCategory(e.target.value)} | |
> | |
<FormControlLabel value="money" control={<Radio />} label="Money" /> | |
<FormControlLabel value="todos" control={<Radio />} label="Todos" /> | |
<FormControlLabel | |
value="reminders" | |
control={<Radio />} | |
label="Reminders" | |
/> | |
<FormControlLabel value="work" control={<Radio />} label="Work" /> | |
</RadioGroup> | |
</FormControl> | |
<Button | |
type="submit" | |
color="secondary" | |
variant="contained" | |
endIcon={<KeyboardArrowRightIcon />} | |
> | |
Submit | |
</Button> | |
</form> | |
</Container> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment