Commit 968d25ec authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

remove sidebar;

create context to check login and keep user info;
parent e9153812
import * as React from "react"; import * as React from "react";
import { useContext } from "react";
import { styled, alpha } from "@mui/material/styles"; import { styled, alpha } from "@mui/material/styles";
import AppBar from "@mui/material/AppBar"; import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box"; import Box from "@mui/material/Box";
...@@ -13,19 +14,16 @@ import MenuIcon from "@mui/icons-material/Menu"; ...@@ -13,19 +14,16 @@ import MenuIcon from "@mui/icons-material/Menu";
import SearchIcon from "@mui/icons-material/Search"; import SearchIcon from "@mui/icons-material/Search";
import AccountCircle from "@mui/icons-material/AccountCircle"; import AccountCircle from "@mui/icons-material/AccountCircle";
import MailIcon from "@mui/icons-material/Mail"; import MailIcon from "@mui/icons-material/Mail";
import NotificationsIcon from "@mui/icons-material/Notifications";
import MoreIcon from "@mui/icons-material/MoreVert";
import { Divider } from "@mui/material"; import { Divider } from "@mui/material";
import List from "@mui/material/List"; import List from "@mui/material/List";
import { ListItem } from "@mui/material"; import { ListItem } from "@mui/material";
import { ListItemButton } from "@mui/material"; import { ListItemButton } from "@mui/material";
import { ListItemIcon } from "@mui/material"; import { ListItemIcon } from "@mui/material";
import PropTypes from "prop-types";
import CssBaseline from "@mui/material/CssBaseline";
import Drawer from "@mui/material/Drawer";
import InboxIcon from "@mui/icons-material/MoveToInbox"; import InboxIcon from "@mui/icons-material/MoveToInbox";
import ListItemText from "@mui/material/ListItemText"; import ListItemText from "@mui/material/ListItemText";
import ShoppingCart from "@mui/icons-material/ShoppingCart"; import ShoppingCart from "@mui/icons-material/ShoppingCart";
import { UserContext } from "@/pages/_app";
import Link from "next/link";
const Search = styled("div")(({ theme }) => ({ const Search = styled("div")(({ theme }) => ({
position: "relative", position: "relative",
...@@ -73,7 +71,7 @@ const drawer = ( ...@@ -73,7 +71,7 @@ const drawer = (
<Divider /> <Divider />
<List> <List>
{["Inbox", "Starred", "Send email", "Drafts"].map((text, index) => ( {["Inbox", "Starred", "Send email", "Drafts"].map((text, index) => (
<ListItem key={text} disablePadding> <ListItem key={index} disablePadding>
<ListItemButton> <ListItemButton>
<ListItemIcon> <ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />} {index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
...@@ -88,7 +86,7 @@ const drawer = ( ...@@ -88,7 +86,7 @@ const drawer = (
); );
function Navbar(props) { function Navbar(props) {
const drawerWidth = props.drawerWidth ?? 200; const user = useContext(UserContext);
const { window } = props; const { window } = props;
const [anchorEl, setAnchorEl] = React.useState(null); const [anchorEl, setAnchorEl] = React.useState(null);
const [mobileMoreAnchorEl, setMobileMoreAnchorEl] = React.useState(null); const [mobileMoreAnchorEl, setMobileMoreAnchorEl] = React.useState(null);
...@@ -142,37 +140,50 @@ function Navbar(props) { ...@@ -142,37 +140,50 @@ function Navbar(props) {
</Menu> </Menu>
); );
const MenuList = [ const MenuList = user?.value?.name
{ ? [
id: 0, {
label: "ตระกร้า", id: 0,
link: '/cart', label: "ตระกร้า",
element: ( link: "/cart",
<IconButton size="large" aria-label="show 4 new mails" color="inherit"> element: (
<Badge badgeContent={4} color="error"> <IconButton
<ShoppingCart /> size="large"
</Badge> aria-label="show 4 new mails"
</IconButton> color="inherit"
), >
}, <Badge badgeContent={4} color="error">
{ <ShoppingCart />
id: 1, </Badge>
label: "โปรไฟล์", </IconButton>
link: "/profile", ),
element: ( },
<IconButton {
size="large" id: 1,
edge="end" label: "โปรไฟล์",
aria-label="account of current user" link: "/profile",
aria-controls={menuId} element: (
aria-haspopup="true" <IconButton
color="inherit" size="large"
> edge="end"
<AccountCircle /> aria-label="account of current user"
</IconButton> aria-controls={menuId}
), aria-haspopup="true"
}, color="inherit"
]; >
<AccountCircle />
</IconButton>
),
},
]
: [
{
id: 0,
lable: "Login",
element: "Login",
link: "/login",
},
];
const mobileMenuId = "primary-search-account-menu-mobile"; const mobileMenuId = "primary-search-account-menu-mobile";
const renderMobileMenu = ( const renderMobileMenu = (
...@@ -191,36 +202,23 @@ function Navbar(props) { ...@@ -191,36 +202,23 @@ function Navbar(props) {
open={isMobileMenuOpen} open={isMobileMenuOpen}
onClose={handleMobileMenuClose} onClose={handleMobileMenuClose}
> >
{MenuList.map((menu) => ( {MenuList.map((menu, idx) => (
<> <span key={idx}>
<MenuItem> <Link className="text-black no-underline" href={menu.link}>
{menu.element} <MenuItem>
<p>{menu.label}</p> {menu.element}
</MenuItem> <p>{menu.label}</p>
</> </MenuItem>
</Link>
</span>
))} ))}
</Menu> </Menu>
); );
return ( return (
<> <>
<AppBar <AppBar position="fixed">
position="fixed"
sx={{
width: { md: `calc(100% - ${drawerWidth}px)` },
ml: { md: `${drawerWidth}px` },
}}
>
<Toolbar> <Toolbar>
<IconButton
size="large"
edge="start"
color="inherit"
aria-label="open drawer"
sx={{ mr: 2, display: { md: "none" } }}
>
<MenuIcon onClick={handleDrawerToggle} />
</IconButton>
<Typography <Typography
variant="h6" variant="h6"
noWrap noWrap
...@@ -240,7 +238,11 @@ function Navbar(props) { ...@@ -240,7 +238,11 @@ function Navbar(props) {
</Search> </Search>
<Box sx={{ flexGrow: 1 }} /> <Box sx={{ flexGrow: 1 }} />
<Box sx={{ display: { xs: "none", md: "flex" } }}> <Box sx={{ display: { xs: "none", md: "flex" } }}>
{MenuList.map((menu) => menu.element)} {MenuList.map((menu, idx) => (
<Link className="text-white" key={idx} href={menu.link}>
<span>{menu.element}</span>
</Link>
))}
</Box> </Box>
<Box sx={{ display: { xs: "flex", md: "none" } }}> <Box sx={{ display: { xs: "flex", md: "none" } }}>
<IconButton <IconButton
...@@ -251,56 +253,18 @@ function Navbar(props) { ...@@ -251,56 +253,18 @@ function Navbar(props) {
onClick={handleMobileMenuOpen} onClick={handleMobileMenuOpen}
color="inherit" color="inherit"
> >
<MoreIcon /> <MenuIcon />
</IconButton> </IconButton>
</Box> </Box>
</Toolbar> </Toolbar>
</AppBar> </AppBar>
{renderMobileMenu} {renderMobileMenu}
{renderMenu} {renderMenu}
<Box
component="nav"
sx={{ width: { sm: drawerWidth }, flexShrink: { sm: 0 } }}
aria-label="mailbox folders"
>
{/* The implementation can be swapped with js to avoid SEO duplication of links. */}
<Drawer
container={container}
variant="temporary"
open={mobileOpen}
onClose={handleDrawerToggle}
ModalProps={{
keepMounted: true, // Better open performance on mobile.
}}
sx={{
display: { xs: "block", sm: "block", md: "none" },
"& .MuiDrawer-paper": {
boxSizing: "border-box",
width: drawerWidth,
},
}}
>
{drawer}
</Drawer>
<Drawer
variant="permanent"
sx={{
display: { xs: "none", md: "block" },
"& .MuiDrawer-paper": {
boxSizing: "border-box",
width: drawerWidth,
},
}}
open
>
{drawer}
</Drawer>
</Box>
</> </>
); );
} }
Navbar.PropTypes = { // Navbar.PropTypes = {
window: PropTypes.func, // window: PropTypes.func,
}; // };
export default Navbar; export default Navbar;
import React from "react"; import React from "react";
import Navbar from "../Navbar"; import Navbar from "../Navbar";
import { Box } from "@mui/material"; import { Box } from "@mui/material";
import { Toolbar } from "@mui/material";
export default function UserLayout({ children }) { export default function UserLayout({ children }) {
const drawerWidth = 200;
return ( return (
<> <>
<Box sx={{ display: "flex" }}> <Box sx={{ display: "flex" }}>
<Navbar drawerWidth={drawerWidth} /> <Navbar />
{/* <Sidebar /> */} {/* <Sidebar /> */}
<Box <Box
component="main" component="main"
sx={{ sx={{
flexGrow: 1, flexGrow: 1,
p: 3, p: 3,
width: { sm: `calc(100% - ${drawerWidth}px)` },
}} }}
> >
<Toolbar />
{children} {children}
</Box> </Box>
</Box> </Box>
......
import Navbar from "@/components/Navbar";
import "@/styles/globals.css"; import "@/styles/globals.css";
import "@fontsource/roboto/300.css"; import "@fontsource/roboto/300.css";
import "@fontsource/roboto/400.css"; import "@fontsource/roboto/400.css";
import "@fontsource/roboto/500.css"; import "@fontsource/roboto/500.css";
import "@fontsource/roboto/700.css"; import "@fontsource/roboto/700.css";
import { createContext, useState, useEffect } from "react";
import UserLayout from "@/components/layout/UserLayout"; import UserLayout from "@/components/layout/UserLayout";
// axios.defaults.baseURL = "" // axios.defaults.baseURL = ""
export const UserContext = createContext(null);
export default function App({ Component, pageProps }) { export default function App({ Component, pageProps }) {
const [user, setUser] = useState({ name: "Nawasan" });
useEffect(() => {
setTimeout(() => {
setUser({ name: "" });
}, 3000);
});
return ( return (
<> <UserContext.Provider value={{ value: user, set: setUser }}>
<UserLayout> <UserLayout>
<Component {...pageProps} /> <Component {...pageProps} />
</UserLayout> </UserLayout>
</> </UserContext.Provider>
); );
} }
import { Html, Head, Main, NextScript } from 'next/document'
export default function Document() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
import React from "react";
export default function Error() {
return <div>Error</div>;
}
import userCookie from "@/components/lib/user"; import userCookie from "@/components/lib/user";
import { Box, TextField, Toolbar } from "@mui/material"; import { Box, TextField, Toolbar } from "@mui/material";
import React from "react"; import React from "react";
import {useContext} from 'react'
import { UserContext } from "./_app";
export default function Login() { export default function Login() {
let user = new userCookie(); const user = useContext(UserContext)
if (user.isLogin()) if (true)
return ( return (
<div> <div>
<h3>คุณเข้าสู่ระบบแล้ว</h3> <h3>{user.value.name}</h3>
</div> </div>
); );
...@@ -20,7 +23,6 @@ export default function Login() { ...@@ -20,7 +23,6 @@ export default function Login() {
"& .MuiTextField-root": { m: 1, width: "25ch" }, "& .MuiTextField-root": { m: 1, width: "25ch" },
}} }}
> >
<Toolbar />
<TextField label="ชื่อ" type="text" /> <TextField label="ชื่อ" type="text" />
</Box> </Box>
</main> </main>
......
...@@ -7,3 +7,8 @@ body { ...@@ -7,3 +7,8 @@ body {
padding: 0; padding: 0;
box-sizing: border-box; box-sizing: border-box;
} }
header a {
color: white;
text-decoration: none;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment