doc: update Navbar component and Navbar function
This commit is contained in:
1 parent
a1c2d9a508
commit
4ec628321b
1 file changed
+54
-12
@@ -1,29 +1,49 @@
|
|||||||
import { useState, useEffect, useContext } from 'react';
|
import { useState, useEffect, useContext } from 'react';
|
||||||
import { AppBar, IconButton, Toolbar, Drawer, Button, Avatar, useMediaQuery } from "@mui/material";
|
import { AppBar, IconButton, Toolbar, Drawer, Button, Avatar, useMediaQuery } from '@mui/material';
|
||||||
import { Menu, AccountCircle, Brightness4, Brightness7 } from "@mui/icons-material";
|
import { Menu, AccountCircle, Brightness4, Brightness7 } from '@mui/icons-material';
|
||||||
import { useTheme } from "@mui/material/styles";
|
import { useTheme } from '@mui/material/styles';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
import { useDispatch, useSelector } from 'react-redux';
|
import { useDispatch, useSelector } from 'react-redux';
|
||||||
|
|
||||||
import useStyles from './styles';
|
import useStyles from './styles';
|
||||||
import { Search, Sidebar } from '../index';
|
import { Search, Sidebar } from '../index';
|
||||||
import { ColorModeContext } from "../../utils/ToggleColorMode";
|
import { setUser } from '../../features/auth';
|
||||||
|
import { fetchToken, createSessionId, moviesApi } from '../../utils/index';
|
||||||
|
import { ColorModeContext } from '../../utils/ToggleColorMode';
|
||||||
|
|
||||||
function Navbar() {
|
function Navbar() {
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
const isMobile = useMediaQuery('(max-width:600px)');
|
const isMobile = useMediaQuery('(max-width:600px)');
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
const dispatch = useDispatch();
|
||||||
const [ mobileOpen, setMobileOpen ] = useState(false);
|
const { isAuthenticated, user } = useSelector((state) => state.user);
|
||||||
|
const [mobileOpen, setMobileOpen] = useState(false);
|
||||||
|
|
||||||
const colorMode = useContext(ColorModeContext);
|
const colorMode = useContext(ColorModeContext);
|
||||||
|
|
||||||
const token = localStorage.getItem('request_token');
|
const token = localStorage.getItem('request_token');
|
||||||
const sessionIdFromLocalStorage = localStorage.getItem('session_id');
|
const sessionIdFromLocalStorage = localStorage.getItem('session_id');
|
||||||
|
|
||||||
return(
|
useEffect(() => {
|
||||||
|
const logInUser = async () => {
|
||||||
|
if (token) {
|
||||||
|
if (sessionIdFromLocalStorage) {
|
||||||
|
const { data: userData } = await moviesApi.get(`/account?session_id=${sessionIdFromLocalStorage}`);
|
||||||
|
dispatch(setUser(userData));
|
||||||
|
} else {
|
||||||
|
const sessionId = await createSessionId();
|
||||||
|
const { data: userData } = await moviesApi.get(`/account?session_id=${sessionId}`);
|
||||||
|
dispatch(setUser(userData));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
logInUser();
|
||||||
|
}, [token]);
|
||||||
|
|
||||||
|
return (
|
||||||
<>
|
<>
|
||||||
<AppBar position={'fixed'}>
|
<AppBar position="fixed">
|
||||||
<Toolbar className={classes.toolbar}>
|
<Toolbar className={classes.toolbar}>
|
||||||
{isMobile && (
|
{isMobile && (
|
||||||
<IconButton
|
<IconButton
|
||||||
@@ -32,18 +52,40 @@ function Navbar() {
|
|||||||
style={{ outline: 'none' }}
|
style={{ outline: 'none' }}
|
||||||
onClick={() => setMobileOpen((prevMobileOpen) => !prevMobileOpen)}
|
onClick={() => setMobileOpen((prevMobileOpen) => !prevMobileOpen)}
|
||||||
className={classes.menuButton}
|
className={classes.menuButton}
|
||||||
>
|
>
|
||||||
<Menu />
|
<Menu />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
)}
|
)}
|
||||||
<IconButton
|
<IconButton
|
||||||
color={'inherit'}
|
color="inherit"
|
||||||
sx={{ ml: 1}}
|
sx={{ ml: 1 }}
|
||||||
onClick={colorMode.toggleColorMode}
|
onClick={colorMode.toggleColorMode}
|
||||||
>
|
>
|
||||||
{theme.palette.mode === 'dark' ? <Brightness7 /> : <Brightness4 />}
|
{theme.palette.mode === 'dark' ? <Brightness7 /> : <Brightness4 />}
|
||||||
</IconButton>
|
</IconButton>
|
||||||
{!isMobile && <Search />}
|
{!isMobile && <Search />}
|
||||||
|
<div>
|
||||||
|
{!isAuthenticated ? (
|
||||||
|
<Button color="inherit" onClick={fetchToken}>
|
||||||
|
Login <AccountCircle />
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
color="inherit"
|
||||||
|
component={Link}
|
||||||
|
to={`/profile/${user.id}`}
|
||||||
|
className={classes.linkButton}
|
||||||
|
>
|
||||||
|
{!isMobile && <>My Movies </>}
|
||||||
|
<Avatar
|
||||||
|
style={{ width: 30, height: 30 }}
|
||||||
|
alt="Profile"
|
||||||
|
src={`https://www.themoviedb.org/t/p/w64_and_h64_face${user?.avatar?.tmdb?.avatar?.avatar_path}`}
|
||||||
|
/>
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{isMobile && <Search />}
|
||||||
</Toolbar>
|
</Toolbar>
|
||||||
</AppBar>
|
</AppBar>
|
||||||
<div>
|
<div>
|
||||||
@@ -67,7 +109,7 @@ function Navbar() {
|
|||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Navbar;
|
export default Navbar;
|
||||||
Reference in new issue
Block a user