ProfilePage.jsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import Breadcrumb from "../components/Breadcrumbs";
  2. import {connect} from "react-redux";
  3. import {Avatar, Box, Button, Container, Grid, TextField, Typography, useMediaQuery} from "@mui/material";
  4. import Redirect from "react-router-dom/es/Redirect";
  5. import Tabs from "@mui/material/Tabs";
  6. import Tab from "@mui/material/Tab";
  7. import {useState} from "react";
  8. import PropTypes from "prop-types";
  9. import ManageAccountsIcon from '@mui/icons-material/ManageAccounts';
  10. import CancelIcon from '@mui/icons-material/Cancel';
  11. import SendAndArchiveIcon from '@mui/icons-material/SendAndArchive';
  12. import {actionAuthLogout} from "../reducers/AuthReducer";
  13. import {actionUserRemove} from "../reducers/UserReducer";
  14. import {backURL} from "../actions/PathDB";
  15. function TabPanel(props) {
  16. const { children, value, index, ...other } = props;
  17. return (
  18. <div
  19. role="tabpanel"
  20. hidden={value !== index}
  21. id={`vertical-tabpanel-${index}`}
  22. aria-labelledby={`vertical-tab-${index}`}
  23. style={{width: '100%'}}
  24. {...other}
  25. >
  26. {value === index && (
  27. <Box sx={{ p: 3}}>
  28. <Typography>{children}</Typography>
  29. </Box>
  30. )}
  31. </div>
  32. );
  33. }
  34. TabPanel.propTypes = {
  35. children: PropTypes.node,
  36. index: PropTypes.number.isRequired,
  37. value: PropTypes.number.isRequired,
  38. };
  39. function a11yProps(index) {
  40. return {
  41. id: `vertical-tab-${index}`,
  42. 'aria-controls': `vertical-tabpanel-${index}`,
  43. };
  44. }
  45. const ItemTabsAccountDefault = ({title, content}) => {
  46. const matches = useMediaQuery('(max-width:899px)')
  47. return(
  48. <Grid item xs={6} sm={4} marginBottom='20px'>
  49. <Typography
  50. color='#616161'
  51. fontWeight='300'
  52. marginBottom='5px'
  53. fontSize={matches ? '13px' : '16px'}
  54. >
  55. {title}
  56. </Typography>
  57. <Typography
  58. color='#000'
  59. fontWeight='400'
  60. fontSize={matches ? '16px' : '22px'}
  61. >
  62. {content}
  63. </Typography>
  64. </Grid>
  65. )
  66. }
  67. const FormUpload = ({user, time, setStatus}) => {
  68. return (
  69. <form action="/upload" method="post" encType="multipart/form-data" id='formProfile'>
  70. <Grid container spacing={2} justifyContent={'space-between'} alignItems={'center'} textAlign={'center'}>
  71. <TextField sx={{color: '#000'}} label={'Login'} variant="outlined" value={user?.login}/>
  72. <TextField sx={{color: '#000'}} label={'Nick'} variant="outlined" value={user?.nick} />
  73. <Button variant="contained" component="Avatar">Choose a new avatar<input type="file" hidden/></Button>
  74. <ItemTabsAccountDefault title={'Status account'} content={user?.acl[2] || user?.acl[1]}/>
  75. <ItemTabsAccountDefault title={'Account creation date'} content={time}/>
  76. <Grid item xs={12} md={4} display='flex' justifyContent={'center'} alignItems={'center'}>
  77. <Button
  78. style={{ color: '#1976d2'}}
  79. fullWidth
  80. type='submit'
  81. marginRight='20px'
  82. >
  83. <SendAndArchiveIcon style={{marginRight: '5px'}}/>
  84. Save
  85. </Button>
  86. <Button
  87. style={{ color: '#1976d2'}}
  88. fullWidth
  89. onClick={() => setStatus(false)}
  90. marginRight='20px'
  91. >
  92. <CancelIcon style={{marginRight: '5px'}}/>
  93. Cancel
  94. </Button>
  95. </Grid>
  96. </Grid>
  97. </form>
  98. )
  99. }
  100. const AccountDetails = ({user, time}) => {
  101. const [status, setStatus] = useState(false)
  102. return (
  103. !status ?
  104. <Grid container spacing={2} justifyContent={'space-between'} alignItems={'center'} textAlign={'center'}>
  105. <ItemTabsAccountDefault title={'Login'} content={user?.login}/>
  106. <ItemTabsAccountDefault title={'Nick'} content={user?.nick}/>
  107. <ItemTabsAccountDefault title={'Status account'} content={user?.acl[2] || user?.acl[1]}/>
  108. <ItemTabsAccountDefault title={'Account creation date'} content={time}/>
  109. <ItemTabsAccountDefault title={'Avatar'}
  110. content={
  111. // eslint-disable-next-line no-mixed-operators
  112. (user?.avatar?.url && <Avatar style={{margin: '0 auto'}} alt="User" src={backURL + '/' + user?.avatar?.url}/> || '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. <Breadcrumb links={['Profile']}/>
  156. <main style={{backgroundColor: "#f3f3f3", padding: matches ? "20px 0" : "50px 0"}}>
  157. <Container maxWidth="lg">
  158. <Box>
  159. <Typography
  160. variant='h5'
  161. textAlign='center'
  162. fontFamily='sarif'
  163. marginBottom={matches ? '20px':'40px'}
  164. >
  165. LOGGED IN AS <strong>{user.login.toUpperCase()}</strong>
  166. </Typography>
  167. </Box>
  168. <Box
  169. sx={{ flexGrow: 1, bgcolor: '#fff', display: 'flex', height: '100%', alignItems: 'center'}}
  170. flexDirection={matches2 ? 'column': "row"}
  171. >
  172. <Tabs
  173. orientation={matches2 ? 'horizontal': "vertical"}
  174. variant="scrollable"
  175. value={value}
  176. onChange={handleChange}
  177. aria-label="Profile settings"
  178. sx={{ borderRight: 1, borderColor: 'divider', padding: '50px 0', height: '100%'}}
  179. >
  180. <Tab sx={{padding: '0 50px', textAlign: 'center'}} label={'ACCOUNT DETAILS'} {...a11yProps(0)} />
  181. <Tab sx={{padding: '0 50px', textAlign: 'center'}} label={'my orders'} {...a11yProps(1)} />
  182. <Tab sx={{padding: '0 50px', textAlign: 'center'}} label={'wish list'} {...a11yProps(2)} />
  183. <Button onClick={() => {authLogOut(); userLogOut()}}>Logout</Button>
  184. </Tabs>
  185. <TabPanel value={value} index={0}>
  186. <AccountDetails user={user} time={formattedTime}/>
  187. </TabPanel>
  188. <TabPanel value={value} index={1}>
  189. my orders
  190. </TabPanel>
  191. <TabPanel value={value} index={2}>
  192. wish list
  193. </TabPanel>
  194. </Box>
  195. </Container>
  196. </main>
  197. </>
  198. )
  199. }
  200. const CProfilePage = connect(state => ({user: state.user}), {authLogOut: actionAuthLogout, userLogOut: actionUserRemove})(ProfilePage)
  201. export default CProfilePage