|
@@ -1,140 +1,163 @@
|
|
|
import React, { useState } from 'react';
|
|
|
-import { useHistory, Link } from 'react-router-dom';
|
|
|
-import { connect } from 'react-redux';
|
|
|
+import { useHistory } from 'react-router-dom';
|
|
|
+import { connect, useDispatch } from 'react-redux';
|
|
|
|
|
|
-import { AppBar, Box, Toolbar, Typography, Menu, Container, Avatar, Tooltip, MenuItem, IconButton } from '@mui/material'
|
|
|
+import { AppBar, Box, Toolbar, Typography, Menu, Container, Avatar, Tooltip, MenuItem, IconButton, Stack } from '@mui/material'
|
|
|
import { Instagram, AddAPhotoRounded } from '@mui/icons-material';
|
|
|
+import { actionFullLogout } from '../redux/action';
|
|
|
|
|
|
+import { url } from '../../App';
|
|
|
|
|
|
|
|
|
-// отрисовываем логин
|
|
|
-const UserName = ({ login }) => <span>{login}</span>
|
|
|
-const CUserName = connect(state => ({ login: state.auth.payload?.sub?.login }))(UserName)
|
|
|
-
|
|
|
-// отрисовывваем логин вариант 2
|
|
|
-const UserName2 = ({ user }) =>
|
|
|
- <MenuItem key={1} >
|
|
|
- <Typography textAlign="center" onClick={() => console.log('click login')}>
|
|
|
- <span>{user.login}</span>
|
|
|
- </Typography>
|
|
|
- </MenuItem>
|
|
|
+function UserMenu({ login }) {
|
|
|
+ let history = useHistory()
|
|
|
+ const [anchorElUser, setAnchorElUser] = useState(null)
|
|
|
+ const dispatch = useDispatch()
|
|
|
|
|
|
-const CUserName2 = connect(state => ({ user: state.auth.payload?.sub }))(UserName2)
|
|
|
|
|
|
+ const handleOpenUserMenu = (event) => {
|
|
|
+ setAnchorElUser(event.currentTarget)
|
|
|
+ }
|
|
|
|
|
|
+ const handleCloseUserMenu = () => {
|
|
|
+ setAnchorElUser(null)
|
|
|
+ }
|
|
|
|
|
|
-// ====================
|
|
|
-function ResponsiveAppBar() {
|
|
|
- let history = useHistory()
|
|
|
+ function toMyAccount() {
|
|
|
+ history.push(`/user/${login?._id}`)
|
|
|
+ }
|
|
|
|
|
|
- function goToMyAccount() {
|
|
|
- history.push('/user/63f8d0c273ca650acb9353f3');
|
|
|
+ function toCreatePost() {
|
|
|
+ history.push('/createpost')
|
|
|
}
|
|
|
|
|
|
- const [anchorElUser, setAnchorElUser] = useState(null);
|
|
|
+ return (
|
|
|
+ <Stack
|
|
|
+ direction='row'
|
|
|
+ >
|
|
|
+ <IconButton
|
|
|
+ sx={{
|
|
|
+ color: '#000'
|
|
|
+ }}
|
|
|
+ onClick={toCreatePost}
|
|
|
+ >
|
|
|
+ <AddAPhotoRounded />
|
|
|
+ </IconButton>
|
|
|
+
|
|
|
+ <Box sx={{ flexGrow: 0 }}>
|
|
|
+ <Tooltip>
|
|
|
+ <IconButton
|
|
|
+ onClick={handleOpenUserMenu}
|
|
|
+ >
|
|
|
+ <Avatar
|
|
|
+ alt={login?.login}
|
|
|
+ src={url.slice(0, -7) + login?.avatar?.url}
|
|
|
+ />
|
|
|
+ </IconButton>
|
|
|
+ </Tooltip>
|
|
|
+
|
|
|
+ <Menu
|
|
|
+ sx={{
|
|
|
+ mt: '50px'
|
|
|
+ }}
|
|
|
+ id="menu-appbar"
|
|
|
+ anchorEl={anchorElUser}
|
|
|
+ anchorOrigin={{
|
|
|
+ vertical: 'top',
|
|
|
+ horizontal: 'right',
|
|
|
+ }}
|
|
|
+ keepMounted
|
|
|
+ transformOrigin={{
|
|
|
+ vertical: 'top',
|
|
|
+ horizontal: 'right',
|
|
|
+ }}
|
|
|
+ open={Boolean(anchorElUser)}
|
|
|
+ onClose={handleCloseUserMenu}
|
|
|
+ >
|
|
|
+ <MenuItem
|
|
|
+ key={1}
|
|
|
+ onClick={toMyAccount}
|
|
|
+ >
|
|
|
+ <Typography
|
|
|
+ textAlign="center">
|
|
|
+ <span>
|
|
|
+ {login?.login}
|
|
|
+ </span>
|
|
|
+ </Typography>
|
|
|
+ </MenuItem>
|
|
|
+
|
|
|
+ <MenuItem
|
|
|
+ key={2}
|
|
|
+ onClick={() => { dispatch(actionFullLogout()) }}
|
|
|
+ >
|
|
|
+ <Typography
|
|
|
+ textAlign="center"
|
|
|
+ >
|
|
|
+ <span>
|
|
|
+ Выйти
|
|
|
+ </span>
|
|
|
+ </Typography>
|
|
|
+ </MenuItem>
|
|
|
+ </Menu>
|
|
|
+ </Box>
|
|
|
+ </Stack>
|
|
|
+ )
|
|
|
+}
|
|
|
|
|
|
- const handleOpenUserMenu = (event) => {
|
|
|
- setAnchorElUser(event.currentTarget);
|
|
|
- };
|
|
|
+function Header({ login }) {
|
|
|
+ let history = useHistory()
|
|
|
|
|
|
- const handleCloseUserMenu = () => {
|
|
|
- setAnchorElUser(null);
|
|
|
- };
|
|
|
+ function toMain() {
|
|
|
+ history.push('/');
|
|
|
+ }
|
|
|
|
|
|
return (
|
|
|
- <AppBar position='sticky' color='inherit'>
|
|
|
- <Container maxWidth="xl">
|
|
|
- <Toolbar disableGutters>
|
|
|
- <IconButton edge='start'>
|
|
|
- <Instagram sx={{
|
|
|
- // display: { xs: 'flex' },
|
|
|
- color: 'black'
|
|
|
- }} />
|
|
|
- </IconButton>
|
|
|
-
|
|
|
- <Typography
|
|
|
- variant="h6"
|
|
|
- noWrap
|
|
|
- component="a"
|
|
|
- href="/"
|
|
|
+ <AppBar
|
|
|
+ position='sticky'
|
|
|
+ color='inherit'
|
|
|
+ >
|
|
|
+ <Container
|
|
|
+ maxWidth="xl"
|
|
|
+ >
|
|
|
+ <Toolbar
|
|
|
+ disableGutters
|
|
|
+ >
|
|
|
+ <Stack
|
|
|
+ direction='row'
|
|
|
sx={{
|
|
|
- display: { xs: 'none', md: 'flex' },
|
|
|
- fontFamily: 'monospace',
|
|
|
- fontWeight: 700,
|
|
|
- letterSpacing: '.3rem',
|
|
|
- color: 'inherit',
|
|
|
- textDecoration: 'none',
|
|
|
- flexGrow: '1',
|
|
|
- ml: 1
|
|
|
+ display: 'flex',
|
|
|
+ flexGrow: '1'
|
|
|
}}
|
|
|
>
|
|
|
- Hipstagram
|
|
|
- </Typography>
|
|
|
-
|
|
|
- <IconButton>
|
|
|
- <Link to='/createpost'>
|
|
|
- <AddAPhotoRounded sx={{
|
|
|
- display: { xs: 'flex' },
|
|
|
- mr: 1,
|
|
|
- // color: 'black'
|
|
|
- }} />
|
|
|
- </Link>
|
|
|
- </IconButton>
|
|
|
-
|
|
|
- <Box sx={{ flexGrow: 0 }}>
|
|
|
- <Tooltip >
|
|
|
- <IconButton onClick={handleOpenUserMenu} sx={{ p: 0 }}>
|
|
|
- <Avatar alt="Remy Sharp" src="/static/images/avatar/2.jpg" />
|
|
|
- </IconButton>
|
|
|
- </Tooltip>
|
|
|
- <Menu
|
|
|
- sx={{ mt: '45px' }}
|
|
|
- id="menu-appbar"
|
|
|
- anchorEl={anchorElUser}
|
|
|
- anchorOrigin={{
|
|
|
- vertical: 'top',
|
|
|
- horizontal: 'right',
|
|
|
+ <IconButton
|
|
|
+ edge='start'
|
|
|
+ sx={{
|
|
|
+ color: '#000'
|
|
|
}}
|
|
|
- keepMounted
|
|
|
- transformOrigin={{
|
|
|
- vertical: 'top',
|
|
|
- horizontal: 'right',
|
|
|
- }}
|
|
|
- open={Boolean(anchorElUser)}
|
|
|
- onClose={handleCloseUserMenu}
|
|
|
+ onClick={toMain}
|
|
|
>
|
|
|
- {/* <CUserName2 /> */}
|
|
|
-
|
|
|
- <MenuItem key={1} onClick={goToMyAccount}>
|
|
|
- <Typography textAlign="center">
|
|
|
- <CUserName />
|
|
|
- </Typography>
|
|
|
- </MenuItem>
|
|
|
-
|
|
|
- <MenuItem key={2} onClick={() => console.log('click logout')}>
|
|
|
- <Typography textAlign="center">
|
|
|
- <span>Выйти</span>
|
|
|
- </Typography>
|
|
|
- </MenuItem>
|
|
|
- </Menu>
|
|
|
- </Box>
|
|
|
+ <Instagram />
|
|
|
+
|
|
|
+ <Typography
|
|
|
+ variant="h6"
|
|
|
+ sx={{
|
|
|
+ fontFamily: 'monospace',
|
|
|
+ fontWeight: 700,
|
|
|
+ letterSpacing: '.3rem',
|
|
|
+ ml: 1
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ Hipstagram
|
|
|
+ </Typography>
|
|
|
+ </IconButton>
|
|
|
+ </Stack>
|
|
|
+
|
|
|
+ {localStorage.authToken && <UserMenu login={login} />}
|
|
|
+
|
|
|
</Toolbar>
|
|
|
</Container>
|
|
|
</AppBar>
|
|
|
)
|
|
|
}
|
|
|
|
|
|
-export default ResponsiveAppBar;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-// <Container>
|
|
|
-// <Link to="/createpost" style={{ textDecoration: 'none' }}>
|
|
|
-// <Button variant="contained" color='primary' startIcon={<AddAPhotoRoundedIcon />} onClick={() => (console.log('click add'))}>
|
|
|
-// Добавить пост
|
|
|
-// </Button>
|
|
|
-// </Link>
|
|
|
-// </Container>
|
|
|
+export const CHeader = connect(state => ({ login: state?.promise?.AboutMe?.payload }))(Header)
|