index.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { makeStyles, Button, TextField, Typography } from '@material-ui/core'
  2. import { useState } from 'react';
  3. import { useDispatch } from 'react-redux';
  4. import UploadAvatar from './UploadAvatar'
  5. import { asyncCreateUser } from '../../../redux/authorization/operations'
  6. import { format } from '../../../helpers'
  7. const useStyles = makeStyles({
  8. container: {
  9. display: 'flex',
  10. alignItems: 'center',
  11. alignContent:'center',
  12. flexDirection: 'column',
  13. width: 350,
  14. margin: '0 auto',
  15. paddingTop: 64,
  16. paddingBottom: 24,
  17. },
  18. title: {
  19. marginBottom: 20,
  20. textAlign: 'center'
  21. },
  22. buttonNext: {
  23. marginTop: 20,
  24. height: 50,
  25. color: '#f8f8f8',
  26. backgroundColor:'#1d74c5',
  27. },
  28. textField: {
  29. marginBottom:20
  30. }
  31. })
  32. const Registration = () => {
  33. const classes = useStyles()
  34. const [name, setName] = useState<string>('')
  35. const [lastName, setLastName] = useState<string>('')
  36. const [upload, setUpload] = useState<any>(null)
  37. const dispatch = useDispatch()
  38. const isValidCredentials = () => {
  39. const validName = name.length
  40. const validLastName = lastName.length
  41. if (validName < 3 || validName > 30) return false
  42. if (validLastName < 3 || validLastName > 30) return false
  43. return true
  44. }
  45. const handleTextField = (e: React.ChangeEvent<HTMLInputElement>) => {
  46. const value = format(e.target.value)
  47. const name = e.target.name
  48. switch (name) {
  49. case 'name':
  50. setName(value)
  51. break;
  52. case 'lastName':
  53. setLastName(value)
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. const handleUpdateUser = async () => dispatch(asyncCreateUser(name, lastName, upload))
  60. return (
  61. <div className={classes.container} >
  62. <Typography
  63. className={classes.title}
  64. variant="h5"
  65. color="initial">
  66. Please upload avatar and fill credentials*
  67. </Typography>
  68. <UploadAvatar
  69. setUpload={setUpload}
  70. />
  71. <TextField
  72. id="name"
  73. name='name'
  74. label="Name"
  75. value={name}
  76. fullWidth
  77. variant='outlined'
  78. onChange={handleTextField}
  79. className={classes.textField}
  80. required
  81. />
  82. <TextField
  83. id="lastName"
  84. name='lastName'
  85. label="LastName"
  86. value={lastName}
  87. fullWidth
  88. variant='outlined'
  89. onChange={handleTextField}
  90. className={classes.textField}
  91. required
  92. />
  93. {isValidCredentials() &&
  94. <Button
  95. onClick={handleUpdateUser}
  96. className={classes.buttonNext}
  97. color='primary'
  98. variant="contained"
  99. fullWidth
  100. > NEXT
  101. </Button>}
  102. </div>
  103. );
  104. };
  105. export default Registration;