Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

269 wiersze
9.6 KiB

  1. import { useEffect, useState } from 'react';
  2. import { Link as RouterLink } from 'react-router-dom';
  3. // material-ui
  4. import {
  5. Box,
  6. Button,
  7. Divider,
  8. FormControl,
  9. FormHelperText,
  10. Grid,
  11. Link,
  12. IconButton,
  13. InputAdornment,
  14. InputLabel,
  15. OutlinedInput,
  16. Stack,
  17. Typography
  18. } from '@mui/material';
  19. // third party
  20. import * as Yup from 'yup';
  21. import { Formik } from 'formik';
  22. // project import
  23. import FirebaseSocial from './FirebaseSocial';
  24. import AnimateButton from 'components/@extended/AnimateButton';
  25. import { strengthColorChi, strengthIndicator } from 'utils/password-strength';
  26. // assets
  27. import { EyeOutlined, EyeInvisibleOutlined } from '@ant-design/icons';
  28. // ============================|| FIREBASE - REGISTER ||============================ //
  29. const AuthRegister = () => {
  30. const [level, setLevel] = useState();
  31. const [showPassword, setShowPassword] = useState(false);
  32. const handleClickShowPassword = () => {
  33. setShowPassword(!showPassword);
  34. };
  35. const handleMouseDownPassword = (event) => {
  36. event.preventDefault();
  37. };
  38. const changePassword = (value) => {
  39. const temp = strengthIndicator(value);
  40. setLevel(strengthColorChi(temp));
  41. };
  42. useEffect(() => {
  43. changePassword('');
  44. }, []);
  45. function getMaxErrStr(num, fieldname){
  46. return intl.formatMessage({ id: 'noMoreThenNWords' },{num:num, fieldname:fieldname?intl.formatMessage({ id: fieldname})+": ":""});
  47. }
  48. return (
  49. <>
  50. <Formik
  51. initialValues={{
  52. firstname: '',
  53. lastname: '',
  54. email: '',
  55. company: '',
  56. password: '',
  57. submit: null
  58. }}
  59. validationSchema={Yup.object().shape({
  60. firstname: Yup.string().max(30, getMaxErrStr(30)).required('First Name is required'),
  61. lastname: Yup.string().max(30, getMaxErrStr(30)).required('Last Name is required'),
  62. email: Yup.string().email('Must be a valid email').max(128, getMaxErrStr(128)).required('Email is required'),
  63. password: Yup.string().max(60, getMaxErrStr(60)).required('Password is required')
  64. })}
  65. onSubmit={async (values, { setErrors, setStatus, setSubmitting }) => {
  66. try {
  67. setStatus({ success: false });
  68. setSubmitting(false);
  69. } catch (err) {
  70. console.error(err);
  71. setStatus({ success: false });
  72. setErrors({ submit: err.message });
  73. setSubmitting(false);
  74. }
  75. }}
  76. >
  77. {({ errors, handleBlur, handleChange, handleSubmit, isSubmitting, touched, values }) => (
  78. <form noValidate onSubmit={handleSubmit}>
  79. <Grid container spacing={3}>
  80. <Grid item xs={12} md={6}>
  81. <Stack spacing={1}>
  82. <InputLabel htmlFor="firstname-signup">First Name*</InputLabel>
  83. <OutlinedInput
  84. id="firstname-login"
  85. type="firstname"
  86. value={values.firstname}
  87. name="firstname"
  88. onBlur={handleBlur}
  89. onChange={handleChange}
  90. placeholder="John"
  91. fullWidth
  92. error={Boolean(touched.firstname && errors.firstname)}
  93. />
  94. {touched.firstname && errors.firstname && (
  95. <FormHelperText error id="helper-text-firstname-signup">
  96. {errors.firstname}
  97. </FormHelperText>
  98. )}
  99. </Stack>
  100. </Grid>
  101. <Grid item xs={12} md={6}>
  102. <Stack spacing={1}>
  103. <InputLabel htmlFor="lastname-signup">Last Name*</InputLabel>
  104. <OutlinedInput
  105. fullWidth
  106. error={Boolean(touched.lastname && errors.lastname)}
  107. id="lastname-signup"
  108. type="lastname"
  109. value={values.lastname}
  110. name="lastname"
  111. onBlur={handleBlur}
  112. onChange={handleChange}
  113. placeholder="Doe"
  114. inputProps={{}}
  115. />
  116. {touched.lastname && errors.lastname && (
  117. <FormHelperText error id="helper-text-lastname-signup">
  118. {errors.lastname}
  119. </FormHelperText>
  120. )}
  121. </Stack>
  122. </Grid>
  123. <Grid item xs={12}>
  124. <Stack spacing={1}>
  125. <InputLabel htmlFor="company-signup">Company</InputLabel>
  126. <OutlinedInput
  127. fullWidth
  128. error={Boolean(touched.company && errors.company)}
  129. id="company-signup"
  130. value={values.company}
  131. name="company"
  132. onBlur={handleBlur}
  133. onChange={handleChange}
  134. placeholder="Demo Inc."
  135. inputProps={{}}
  136. />
  137. {touched.company && errors.company && (
  138. <FormHelperText error id="helper-text-company-signup">
  139. {errors.company}
  140. </FormHelperText>
  141. )}
  142. </Stack>
  143. </Grid>
  144. <Grid item xs={12}>
  145. <Stack spacing={1}>
  146. <InputLabel htmlFor="email-signup">Email Address*</InputLabel>
  147. <OutlinedInput
  148. fullWidth
  149. error={Boolean(touched.email && errors.email)}
  150. id="email-login"
  151. type="email"
  152. value={values.email}
  153. name="email"
  154. onBlur={handleBlur}
  155. onChange={handleChange}
  156. placeholder="[email protected]"
  157. inputProps={{}}
  158. />
  159. {touched.email && errors.email && (
  160. <FormHelperText error id="helper-text-email-signup">
  161. {errors.email}
  162. </FormHelperText>
  163. )}
  164. </Stack>
  165. </Grid>
  166. <Grid item xs={12}>
  167. <Stack spacing={1}>
  168. <InputLabel htmlFor="password-signup">Password</InputLabel>
  169. <OutlinedInput
  170. fullWidth
  171. error={Boolean(touched.password && errors.password)}
  172. id="password-signup"
  173. type={showPassword ? 'text' : 'password'}
  174. value={values.password}
  175. name="password"
  176. onBlur={handleBlur}
  177. onChange={(e) => {
  178. handleChange(e);
  179. changePassword(e.target.value);
  180. }}
  181. endAdornment={
  182. <InputAdornment position="end">
  183. <IconButton
  184. aria-label="toggle password visibility"
  185. onClick={handleClickShowPassword}
  186. onMouseDown={handleMouseDownPassword}
  187. edge="end"
  188. size="large"
  189. >
  190. {showPassword ? <EyeOutlined /> : <EyeInvisibleOutlined />}
  191. </IconButton>
  192. </InputAdornment>
  193. }
  194. placeholder="******"
  195. inputProps={{}}
  196. />
  197. {touched.password && errors.password && (
  198. <FormHelperText error id="helper-text-password-signup">
  199. {errors.password}
  200. </FormHelperText>
  201. )}
  202. </Stack>
  203. <FormControl fullWidth sx={{ mt: 2 }}>
  204. <Grid container spacing={2} alignItems="center">
  205. <Grid item>
  206. <Box sx={{ bgcolor: level?.color, width: 85, height: 8, borderRadius: '7px' }} />
  207. </Grid>
  208. <Grid item>
  209. <Typography variant="subtitle1" fontSize="0.75rem">
  210. {level?.label}
  211. </Typography>
  212. </Grid>
  213. </Grid>
  214. </FormControl>
  215. </Grid>
  216. <Grid item xs={12}>
  217. <Typography variant="body2">
  218. By Signing up, you agree to our &nbsp;
  219. <Link variant="subtitle2" component={RouterLink} to="#">
  220. Terms of Service
  221. </Link>
  222. &nbsp; and &nbsp;
  223. <Link variant="subtitle2" component={RouterLink} to="#">
  224. Privacy Policy
  225. </Link>
  226. </Typography>
  227. </Grid>
  228. {errors.submit && (
  229. <Grid item xs={12}>
  230. <FormHelperText error>{errors.submit}</FormHelperText>
  231. </Grid>
  232. )}
  233. <Grid item xs={12}>
  234. <AnimateButton>
  235. <Button disableElevation disabled={isSubmitting} fullWidth size="large" type="submit" variant="contained" color="primary">
  236. Create Account
  237. </Button>
  238. </AnimateButton>
  239. </Grid>
  240. <Grid item xs={12}>
  241. <Divider>
  242. <Typography variant="caption">Sign up with</Typography>
  243. </Divider>
  244. </Grid>
  245. <Grid item xs={12}>
  246. <FirebaseSocial />
  247. </Grid>
  248. </Grid>
  249. </form>
  250. )}
  251. </Formik>
  252. </>
  253. );
  254. };
  255. export default AuthRegister;