index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import qrCode from '../../../img/icons8-telegram-app.svg'
  2. import { makeStyles, Button, TextField, MenuItem } from '@material-ui/core'
  3. import Checkbox from '@mui/material/Checkbox';
  4. import FormControlLabel from '@mui/material/FormControlLabel';
  5. import React from 'react';
  6. import { countries } from './countries'
  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. picture: {
  19. marginBottom: 16,
  20. paddingLeft: 45
  21. },
  22. title: {
  23. textAlign: 'center',
  24. marginBottom: 16,
  25. },
  26. titleConfirm: {
  27. color: '#b4b4b4',
  28. fontWeight:400
  29. },
  30. buttonQR: {
  31. marginTop: 20,
  32. height: 50,
  33. color: '#5497f0'
  34. },
  35. buttonNext: {
  36. marginTop: 20,
  37. height: 50,
  38. color: '#f8f8f8',
  39. backgroundColor:'#1d74c5',
  40. },
  41. textField: {
  42. marginBottom:20
  43. },
  44. })
  45. interface IAuthorizationForm {
  46. handleIsQR: () => void,
  47. handleIsCheck:(e:React.ChangeEvent<HTMLInputElement>) => void
  48. handleSendCode: () => Promise<void>,
  49. number:string,
  50. setNumber: React.Dispatch<React.SetStateAction<string>>,
  51. country: string,
  52. setCountry: React.Dispatch<React.SetStateAction<string>>,
  53. isChecked: boolean,
  54. }
  55. interface ICountry {
  56. name: string,
  57. code: string
  58. }
  59. const Authorization = ({
  60. handleIsQR,
  61. handleIsCheck,
  62. handleSendCode,
  63. number,
  64. setNumber,
  65. country,
  66. setCountry,
  67. isChecked,
  68. }: IAuthorizationForm) => {
  69. const classes = useStyles()
  70. const isValidCredentials = () => {
  71. const numberLength = number.split(' ').join('').trim().length
  72. if (numberLength < 13 || numberLength > 13) return false
  73. if (!country || country === 'Country') return false
  74. if(number.slice(0,1) === '+') return true
  75. return true
  76. }
  77. const handleTextField = (e: React.ChangeEvent<HTMLInputElement>) => {
  78. const value = e.target.value
  79. const name = e.target.name
  80. switch (name) {
  81. case 'country':
  82. setCountry(value)
  83. const code = countries.find(el => el.name === value)?.code
  84. code&&setNumber(code)
  85. break;
  86. case 'number':
  87. setNumber(value)
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. return (
  94. <div className={classes.container} >
  95. <img className={classes.picture} alt="QRCode" src={qrCode} width='270' height='270' />
  96. <h2 className={classes.title}>Telegram</h2>
  97. <h4 className={classes.title + ' ' + classes.titleConfirm}>
  98. Please confirm your country code and enter your phone number.
  99. </h4>
  100. <TextField
  101. id="country"
  102. name='country'
  103. label="Country"
  104. fullWidth
  105. variant='outlined'
  106. defaultValue=''
  107. select
  108. className={classes.textField}
  109. onChange={handleTextField}
  110. >
  111. {countries.map(({name}: ICountry) => (
  112. <MenuItem value={name} key={name}>
  113. {name}
  114. </MenuItem>
  115. ))}
  116. </TextField>
  117. <TextField
  118. id="number"
  119. name='number'
  120. label="Your phone number"
  121. value={number}
  122. fullWidth
  123. variant='outlined'
  124. onChange={handleTextField}
  125. />
  126. <FormControlLabel
  127. label="Keep me signed in"
  128. control={<Checkbox
  129. checked={isChecked}
  130. onChange={handleIsCheck}
  131. inputProps={{ 'aria-label': 'controlled' }}
  132. />}
  133. />
  134. {isValidCredentials() &&
  135. <Button
  136. onClick={handleSendCode}
  137. className={classes.buttonNext}
  138. color='primary'
  139. variant="contained"
  140. fullWidth>
  141. NEXT
  142. </Button>}
  143. <Button
  144. onClick={handleIsQR}
  145. className={classes.buttonQR}
  146. variant="text"
  147. color="default"
  148. fullWidth>
  149. LOG IN BY QR CODE
  150. </Button>
  151. </div>
  152. );
  153. };
  154. export default Authorization;