Add action to avatar menu dropdown

This commit is contained in:
Leonardo Murça 2022-06-15 15:03:12 -03:00
parent ab6252e67a
commit ff8c8073db
3 changed files with 53 additions and 42 deletions

View file

@ -1,4 +1,4 @@
import { Navigate, Route, Routes, useLocation } from 'react-router-dom'; import { Navigate, Route, Routes, useNavigate } from 'react-router-dom';
import { import {
CalendarMonth, CalendarMonth,
CalendarMonthOutlined, CalendarMonthOutlined,
@ -15,12 +15,28 @@ import { Container } from '@mui/system';
import useLayoutType from './hooks/useLayoutType'; import useLayoutType from './hooks/useLayoutType';
import Toolbar from './components/Toolbar'; import Toolbar from './components/Toolbar';
import { useUser } from './context/user'; import { useUser } from './context/user';
import { useAuthState } from './context/auth';
function AuthenticatedApp() { function AuthenticatedApp() {
const navigate = useNavigate();
const { state } = useUser(); const { state } = useUser();
const { logout } = useAuthState();
const layoutType = useLayoutType(); const layoutType = useLayoutType();
const avatarMenuOptions = [
{
text: 'Meu Perfil',
action: () => navigate('/profile', { replace: true }),
},
{
text: 'Sair',
action: logout,
},
];
return ( return (
state &&
state.user && (
<> <>
<Toolbar <Toolbar
title={ title={
@ -29,6 +45,7 @@ function AuthenticatedApp() {
</p> </p>
} }
layoutType={layoutType} layoutType={layoutType}
avatarMenuOptions={avatarMenuOptions}
/> />
<Container <Container
maxWidth="false" maxWidth="false"
@ -46,6 +63,7 @@ function AuthenticatedApp() {
</Routes> </Routes>
</Container> </Container>
</> </>
)
); );
} }

View file

@ -11,12 +11,10 @@ import {
} from '@mui/material'; } from '@mui/material';
import { useState } from 'react'; import { useState } from 'react';
function Toolbar({ title, layoutType }) { function Toolbar({ title, layoutType, avatarMenuOptions }) {
const [anchorElUser, setAnchorElUser] = useState(null); const [anchorElUser, setAnchorElUser] = useState(null);
const [anchorElNotifications, setAnchorElNotifications] = useState(null); const [anchorElNotifications, setAnchorElNotifications] = useState(null);
console.log(layoutType);
switch (layoutType) { switch (layoutType) {
case 'desktop': case 'desktop':
return ( return (
@ -87,12 +85,11 @@ function Toolbar({ title, layoutType }) {
open={Boolean(anchorElUser)} open={Boolean(anchorElUser)}
onClose={() => setAnchorElUser(null)} onClose={() => setAnchorElUser(null)}
> >
<MenuItem onClick={() => setAnchorElUser(null)}> {avatarMenuOptions.map(option => (
<Typography textAlign="center">Meu perfil</Typography> <MenuItem key={option.text} onClick={option.action}>
</MenuItem> <Typography textAlign="center">{option.text}</Typography>
<MenuItem onClick={() => setAnchorElUser(null)}>
<Typography textAlign="center">Sair</Typography>
</MenuItem> </MenuItem>
))}
</Menu> </Menu>
</Box> </Box>
</Box> </Box>
@ -168,12 +165,11 @@ function Toolbar({ title, layoutType }) {
open={Boolean(anchorElUser)} open={Boolean(anchorElUser)}
onClose={() => setAnchorElUser(null)} onClose={() => setAnchorElUser(null)}
> >
<MenuItem onClick={() => setAnchorElUser(null)}> {avatarMenuOptions.map(option => (
<Typography textAlign="center">Meu perfil</Typography> <MenuItem key={option.text} onClick={option.action}>
</MenuItem> <Typography textAlign="center">{option.text}</Typography>
<MenuItem onClick={() => setAnchorElUser(null)}>
<Typography textAlign="center">Sair</Typography>
</MenuItem> </MenuItem>
))}
</Menu> </Menu>
</Box> </Box>
</Box> </Box>

View file

@ -1,15 +1,12 @@
import { useAuthState } from '../../context/auth';
import { useUser } from '../../context/user'; import { useUser } from '../../context/user';
function Home() { function Home() {
const { isPending, classrooms } = useUser(); const { isPending, classrooms } = useUser();
const { logout } = useAuthState();
return ( return (
<div> <div>
<h2>Página inicial</h2> <h2>Página inicial</h2>
<button onClick={classrooms}>Get classrooms</button> <button onClick={classrooms}>Get classrooms</button>
<button onClick={logout}>Logout</button>
{isPending && <h1>Loading...</h1>} {isPending && <h1>Loading...</h1>}
</div> </div>
); );