MyAccountPage.jsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import Breadcrumb from "../components/Breadcrumbs";
  2. import {
  3. Box,
  4. Button,
  5. Container,
  6. TextField,
  7. Typography,
  8. useMediaQuery
  9. } from "@mui/material";
  10. import UnstyledButtonCustom from "../components/MainButton";
  11. import ArrowForwardIosIcon from '@mui/icons-material/ArrowForwardIos';
  12. import {useState} from "react";
  13. import Link from "react-router-dom/es/Link";
  14. import {connect} from 'react-redux';
  15. import {actionFullLogin, actionFullRegister} from "../actions/ActionLogin";
  16. import Redirect from "react-router-dom/es/Redirect";
  17. import {actionClearPromise} from "../reducers/PromiseReducer";
  18. const MyAccountPage = ({auth, promise, user, onLogin, onRegister, onClear}) => {
  19. const matches = useMediaQuery('(max-width:899px)')
  20. const [status, setStatus] = useState('login')
  21. if (auth?.payload && Object.keys(user).length !== 0){
  22. onClear('login'); onClear('register');
  23. }
  24. const Form = ({title, target, onClickEvent, children}) => {
  25. const matches2 = useMediaQuery('(max-width:450px)')
  26. const [valueLogin, setValueLogin] = useState('')
  27. const [valuePassword, setValuePassword] = useState('')
  28. const [flagLogin, setFlagLog] = useState(false)
  29. const [flagPassword, setFlagPass] = useState(false)
  30. return (
  31. <Box
  32. sx={{
  33. backgroundColor: "#fff",
  34. display: "flex",
  35. flexDirection: "column",
  36. justifyContent: "center",
  37. alignItems: "center",
  38. padding: matches2 ? "30px 20px" : "40px 60px"
  39. }}
  40. >
  41. <Typography
  42. variant='h4'
  43. letterSpacing='10px'
  44. marginBottom='30px'
  45. >
  46. {title}
  47. </Typography>
  48. <TextField
  49. id="login"
  50. label="Login"
  51. variant="standard"
  52. type="text"
  53. required
  54. fullWidth
  55. value={valueLogin}
  56. onChange={e => {setValueLogin(e.target.value); setFlagLog(true)}}
  57. error={flagLogin && (valueLogin.length < 3 || valueLogin.length > 20)}
  58. sx={{marginBottom: '20px'}}
  59. />
  60. <TextField
  61. id="password"
  62. label="Password"
  63. variant="standard"
  64. type="password"
  65. required
  66. fullWidth
  67. value={valuePassword}
  68. onChange={e => {setValuePassword(e.target.value); setFlagPass(true)}}
  69. error={flagPassword && (valuePassword.length < 3 || valuePassword.length > 20)}
  70. sx={{marginBottom: '30px'}}
  71. />
  72. {children}
  73. <UnstyledButtonCustom
  74. onClick={() => {
  75. if(valuePassword.length >= 3 && valuePassword.length <= 20 &&
  76. valueLogin.length >= 3 && valueLogin.length <= 20) {
  77. onClickEvent(valueLogin, valuePassword)
  78. }
  79. }}
  80. text={title}
  81. />
  82. <Box
  83. width='100%'
  84. display='flex'
  85. justifyContent='flex-end'
  86. marginTop='20px'
  87. >
  88. <Button
  89. onClick={() => setStatus(target)}
  90. >
  91. {target}
  92. <ArrowForwardIosIcon/>
  93. </Button>
  94. </Box>
  95. </Box>
  96. )
  97. }
  98. return (
  99. <>
  100. <Breadcrumb links={['my account']}/>
  101. <main style={{backgroundColor: "#f3f3f3", padding: matches ? "20px 0" : "50px 0"}}>
  102. <Container maxWidth="sm">
  103. {(auth?.payload && Object.keys(user).length !== 0) ? <Redirect to={'/profile'}/>:
  104. status === 'login' ?
  105. <Form title={'LOGIN'} target={'register'} onClickEvent={onLogin} children={
  106. <>
  107. {promise?.login?.status === "RESOLVED" &&
  108. <Typography
  109. variant='body2'
  110. color='red'
  111. marginBottom='20px'
  112. >
  113. This user does not exist
  114. </Typography>
  115. }
  116. </>
  117. }/> :
  118. <Form title={'REGISTER'} target={'login'} onClickEvent={onRegister} children={
  119. <>
  120. <Typography
  121. variant='body2'
  122. color='#616161'
  123. textAlign='justify'
  124. marginBottom='40px'
  125. >
  126. Your personal data will be used to support your experience throughout this website, to manage access to your account, and for other purposes described in our
  127. <Link style={{textDecoration: 'none'}} to='/privacy-policy'> privacy policy</Link>
  128. .
  129. </Typography>
  130. {promise?.register?.status === "RESOLVED" &&
  131. <Typography
  132. variant='body2'
  133. color='red'
  134. marginBottom='20px'
  135. >
  136. Such user already exists
  137. </Typography>
  138. }
  139. </>
  140. }/>
  141. }
  142. </Container>
  143. </main>
  144. </>
  145. )
  146. }
  147. const CLoginForm = connect(state => ({auth: state.auth, promise: state.promise, user: state.user}), {onLogin: actionFullLogin, onRegister: actionFullRegister, onClear: actionClearPromise})(MyAccountPage)
  148. export default CLoginForm