ProfilePage.jsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import Header from "../components/Header";
  2. import Footer from "../components/Footer";
  3. import Breadcrumb from "../components/Breadcrumbs";
  4. import {connect} from "react-redux";
  5. import {Box, Button, Container, Grid, TextField, Typography, useMediaQuery} from "@mui/material";
  6. import Redirect from "react-router-dom/es/Redirect";
  7. import Tabs from "@mui/material/Tabs";
  8. import Tab from "@mui/material/Tab";
  9. import {useRef, useState} from "react";
  10. import PropTypes from "prop-types";
  11. import ManageAccountsIcon from '@mui/icons-material/ManageAccounts';
  12. import CancelIcon from '@mui/icons-material/Cancel';
  13. import SendAndArchiveIcon from '@mui/icons-material/SendAndArchive';
  14. import {actionAuthLogout} from "../reducers/AuthReducer";
  15. import {actionUserRemove} from "../reducers/UserReducer";
  16. function TabPanel(props) {
  17. const { children, value, index, ...other } = props;
  18. return (
  19. <div
  20. role="tabpanel"
  21. hidden={value !== index}
  22. id={`vertical-tabpanel-${index}`}
  23. aria-labelledby={`vertical-tab-${index}`}
  24. style={{width: '100%'}}
  25. {...other}
  26. >
  27. {value === index && (
  28. <Box sx={{ p: 3}}>
  29. <Typography>{children}</Typography>
  30. </Box>
  31. )}
  32. </div>
  33. );
  34. }
  35. TabPanel.propTypes = {
  36. children: PropTypes.node,
  37. index: PropTypes.number.isRequired,
  38. value: PropTypes.number.isRequired,
  39. };
  40. function a11yProps(index) {
  41. return {
  42. id: `vertical-tab-${index}`,
  43. 'aria-controls': `vertical-tabpanel-${index}`,
  44. };
  45. }
  46. const ItemTabsAccountDefault = ({title, content}) => {
  47. const matches = useMediaQuery('(max-width:899px)')
  48. return(
  49. <Grid item xs={6} sm={4} marginBottom='20px'>
  50. <Typography
  51. color='#616161'
  52. fontWeight='300'
  53. marginBottom='5px'
  54. fontSize={matches ? '13px' : '16px'}
  55. >
  56. {title}
  57. </Typography>
  58. <Typography
  59. color='#000'
  60. fontWeight='400'
  61. fontSize={matches ? '16px' : '22px'}
  62. >
  63. {content}
  64. </Typography>
  65. </Grid>
  66. )
  67. }
  68. const FormUpload = ({user, time, setStatus}) => {
  69. return (
  70. <form action="/upload" method="post" encType="multipart/form-data" id='formProfile'>
  71. <Grid container spacing={2} justifyContent={'space-between'} alignItems={'center'} textAlign={'center'}>
  72. <TextField sx={{color: '#000'}} label={'Login'} variant="outlined" value={user?.login}/>
  73. <TextField sx={{color: '#000'}} label={'Nick'} variant="outlined" value={user?.nick} />
  74. <Button variant="contained" component="Avatar">Choose a new avatar<input type="file" hidden/></Button>
  75. <ItemTabsAccountDefault title={'Status account'} content={user?.acl[1]}/>
  76. <ItemTabsAccountDefault title={'Account creation date'} content={time}/>
  77. <Grid item xs={12} md={4} display='flex' justifyContent={'center'} alignItems={'center'}>
  78. <Button
  79. style={{ color: '#1976d2'}}
  80. fullWidth
  81. type='submit'
  82. marginRight='20px'
  83. >
  84. <SendAndArchiveIcon style={{marginRight: '5px'}}/>
  85. Save
  86. </Button>
  87. <Button
  88. style={{ color: '#1976d2'}}
  89. fullWidth
  90. onClick={() => setStatus(false)}
  91. marginRight='20px'
  92. >
  93. <CancelIcon style={{marginRight: '5px'}}/>
  94. Cancel
  95. </Button>
  96. </Grid>
  97. </Grid>
  98. </form>
  99. )
  100. }
  101. const AccountDetails = ({user, time}) => {
  102. const [status, setStatus] = useState(false)
  103. return (
  104. !status ?
  105. <Grid container spacing={2} justifyContent={'space-between'} alignItems={'center'} textAlign={'center'}>
  106. <ItemTabsAccountDefault title={'Login'} content={user?.login}/>
  107. <ItemTabsAccountDefault title={'Nick'} content={user?.nick}/>
  108. <ItemTabsAccountDefault title={'Status account'} content={user?.acl[1]}/>
  109. <ItemTabsAccountDefault title={'Account creation date'} content={time}/>
  110. <ItemTabsAccountDefault title={'Avatar'}
  111. content={
  112. (user?.avatar && <img src={user?.avatar} alt={'User avatar'}/>) || 'Not installed'
  113. }
  114. />
  115. <Grid item xs={12} md={4}>
  116. <Typography
  117. sx={{cursor: 'pointer'}}
  118. color={'#1976d2'}
  119. display='flex'
  120. justifyContent='center'
  121. alignItems='center'
  122. variant='h6'
  123. onClick={() => setStatus(true)}
  124. >
  125. <ManageAccountsIcon style={{marginRight: '10px'}}/>
  126. Edit data
  127. </Typography>
  128. </Grid>
  129. </Grid> :
  130. <FormUpload user={user} time={time} setStatus={value => setStatus(value)}/>
  131. )
  132. }
  133. const ProfilePage = ({user = {}, authLogOut, userLogOut}) => {
  134. const matches = useMediaQuery('(max-width:899px)')
  135. const matches2 = useMediaQuery('(max-width:768px)')
  136. const [value, setValue] = useState(0)
  137. const handleChange = (event, newValue) => {
  138. setValue(newValue);
  139. };
  140. let formattedTime = 0;
  141. if (Object.keys(user).length !== 0) {
  142. let date = new Date(+user.createdAt);
  143. let year = date.getFullYear();
  144. let month = "0" + date.getMonth();
  145. let day = "0" + date.getDate();
  146. let hours = "0" + date.getHours();
  147. let minutes = "0" + date.getMinutes();
  148. let seconds = "0" + date.getSeconds();
  149. formattedTime = day.substr(-2) + '.' + month.substr(-2) + '.' + year +
  150. ' ' + hours.substr(-2) + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
  151. }
  152. return (
  153. Object.keys(user).length === 0 ? <Redirect to={'/my-account'}/> :
  154. <>
  155. <Header/>
  156. <Breadcrumb links={['Profile']}/>
  157. <main style={{backgroundColor: "#f3f3f3", padding: matches ? "20px 0" : "50px 0"}}>
  158. <Container maxWidth="lg">
  159. <Box>
  160. <Typography
  161. variant='h5'
  162. textAlign='center'
  163. fontFamily='sarif'
  164. marginBottom={matches ? '20px':'40px'}
  165. >
  166. LOGGED IN AS <strong>{user.login.toUpperCase()}</strong>
  167. </Typography>
  168. </Box>
  169. <Box
  170. sx={{ flexGrow: 1, bgcolor: '#fff', display: 'flex', height: '100%', alignItems: 'center'}}
  171. flexDirection={matches2 ? 'column': "row"}
  172. >
  173. <Tabs
  174. orientation={matches2 ? 'horizontal': "vertical"}
  175. variant="scrollable"
  176. value={value}
  177. onChange={handleChange}
  178. aria-label="Profile settings"
  179. sx={{ borderRight: 1, borderColor: 'divider', padding: '50px 0', height: '100%'}}
  180. >
  181. <Tab sx={{padding: '0 50px', textAlign: 'center'}} label={'ACCOUNT DETAILS'} {...a11yProps(0)} />
  182. <Tab sx={{padding: '0 50px', textAlign: 'center'}} label={'my orders'} {...a11yProps(1)} />
  183. <Tab sx={{padding: '0 50px', textAlign: 'center'}} label={'wish list'} {...a11yProps(2)} />
  184. <Button onClick={() => {authLogOut(); userLogOut()}}>Logout</Button>
  185. </Tabs>
  186. <TabPanel value={value} index={0}>
  187. <AccountDetails user={user} time={formattedTime}/>
  188. </TabPanel>
  189. <TabPanel value={value} index={1}>
  190. my orders
  191. </TabPanel>
  192. <TabPanel value={value} index={2}>
  193. wish list
  194. </TabPanel>
  195. </Box>
  196. </Container>
  197. </main>
  198. <Footer/>
  199. </>
  200. )
  201. }
  202. const CProfilePage = connect(state => ({user: state.user}), {authLogOut: actionAuthLogout, userLogOut: actionUserRemove})(ProfilePage)
  203. export default CProfilePage