index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { makeStyles, TextField } from '@material-ui/core'
  2. import ListItemAvatar from '@mui/material/ListItemAvatar';
  3. import Avatar from '@mui/material/Avatar';
  4. import AddAPhotoIcon from '@mui/icons-material/AddAPhoto';
  5. import PhotoCameraFrontIcon from '@mui/icons-material/PhotoCameraFront';
  6. import CameraIcon from '@mui/icons-material/Camera';
  7. import { useEffect } from 'react';
  8. import { useDropzone } from 'react-dropzone';
  9. import Webcam from "react-webcam";
  10. import { format,firstLetter,prodAwsS3 } from '../../../../../helpers'
  11. import { IAuthorizationState } from '../../../../../typescript/redux/authorization/interfaces'
  12. const useStyles = makeStyles({
  13. container: {
  14. display: 'flex',
  15. alignItems: 'center',
  16. alignContent:'center',
  17. flexDirection: 'column',
  18. width: '100%',
  19. padding: 20,
  20. paddingTop:0,
  21. position: 'relative',
  22. backgroundColor: '#ffffff',
  23. },
  24. imgWrapper: {
  25. marginBottom: 30,
  26. width: '100%',
  27. display: 'flex',
  28. flexWrap: 'nowrap',
  29. alignItems: 'center',
  30. alignContent: 'center',
  31. flexDirection: 'row',
  32. justifyContent:'center'
  33. },
  34. overlay: {
  35. position: 'fixed',
  36. top: 0,
  37. left: 0,
  38. width: '100vw',
  39. height: '100vh',
  40. zIndex: 100,
  41. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  42. overflowY: 'hidden',
  43. display: 'flex',
  44. justifyContent: 'center',
  45. alignContent: 'center',
  46. alignItems: 'center',
  47. flexDirection:'column'
  48. },
  49. capturedPicture: {
  50. borderRadius: 10,
  51. border:'solid 2px rgb(62, 149, 231)'
  52. },
  53. capturePhoto: {
  54. color: '#ffffff',
  55. cursor: 'pointer',
  56. '&:hover': {
  57. color: '#48ff00',
  58. animation: `$rotating 2s linear infinite`
  59. },
  60. },
  61. '@keyframes rotating': {
  62. 'from': { transform: 'rotate(0deg)'},
  63. 'to': { transform: 'rotate(360deg)'},
  64. },
  65. })
  66. interface IEditList {
  67. user: IAuthorizationState,
  68. name: string,
  69. setName: any,
  70. lastName: string,
  71. setLastName: any,
  72. openBtn: boolean,
  73. setOpenBtn: any,
  74. file:any,
  75. setFile: (file: any) => void,
  76. camera: boolean,
  77. setCamera: any,
  78. selfie:any,
  79. setSelfie: (file: any) => void,
  80. }
  81. const EditList = (props: IEditList) => {
  82. const classes = useStyles()
  83. const { user, name, setName, lastName, setLastName,
  84. openBtn, setOpenBtn,file,setFile,camera,setCamera,selfie,setSelfie } = props
  85. const { avatarUrl, color } = user
  86. const { getRootProps, getInputProps, acceptedFiles } = useDropzone({
  87. noDrag: true,
  88. accept:'image/*'
  89. });
  90. const videoConstraints = {
  91. width: 1280,
  92. height: 720,
  93. facingMode: "user"
  94. };
  95. const handleTextField = (e: React.ChangeEvent<HTMLInputElement>) => {
  96. !openBtn&&setOpenBtn(true)
  97. const value = format(e.target.value)
  98. const name = e.target.name
  99. switch (name) {
  100. case 'name':
  101. setName(value)
  102. break;
  103. case 'lastName':
  104. setLastName(value)
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. const handleOpenCamera = () => {
  111. setCamera(true)
  112. file&&setFile(null)
  113. }
  114. const handleCloseCamera = (e: any) => {
  115. const id = e.target.id
  116. if (id === 'overlay') setCamera(false)
  117. }
  118. useEffect(() => {
  119. if (acceptedFiles.slice(-1)[0]) {
  120. setFile(acceptedFiles.slice(-1)[0])
  121. setSelfie(false)
  122. setOpenBtn(true)
  123. }
  124. }, [setFile,setSelfie,setOpenBtn,acceptedFiles])
  125. return (
  126. <div className={classes.container}>
  127. <div className={classes.imgWrapper}>
  128. <div {...getRootProps()}>
  129. <AddAPhotoIcon fontSize='large'
  130. sx={{color: file ? 'rgb(62, 149, 231)' : '#6b6b6b',
  131. '&:hover': { color: 'rgb(41, 139, 231)' },cursor:'pointer'}} />
  132. <input {...getInputProps()}/>
  133. </div>
  134. <ListItemAvatar style={{margin:'0px 20px'}}>
  135. <Avatar alt={name} src={avatarUrl?`${prodAwsS3}/${avatarUrl}`:undefined}
  136. sx={{ background: color, width: 120, height: 120,fontSize:30}}>
  137. {`${firstLetter(name)}${firstLetter(lastName)}`}
  138. </Avatar>
  139. </ListItemAvatar>
  140. <PhotoCameraFrontIcon onClick={handleOpenCamera} fontSize='large'
  141. sx={{color: selfie ? 'rgb(62, 149, 231)' : '#6b6b6b',
  142. '&:hover': { color: 'rgb(41, 139, 231)' },cursor:'pointer'}}/>
  143. </div>
  144. {camera &&
  145. <div onClick={handleCloseCamera} id='overlay'
  146. className={classes.overlay}>
  147. <Webcam
  148. audio={false}
  149. screenshotFormat="image/jpeg"
  150. width='40%'
  151. videoConstraints={videoConstraints}
  152. style={{marginBottom:30}}
  153. >
  154. {({ getScreenshot }) => <>
  155. <CameraIcon onClick={() => {
  156. setSelfie(getScreenshot())
  157. setOpenBtn(true)
  158. }}
  159. className={classes.capturePhoto} fontSize='large' style={{marginBottom:30}} />
  160. <img className={classes.capturedPicture} width='300' height='174'
  161. style={{visibility:selfie?'visible':'hidden'}} src={selfie} alt='chosen pic'></img>
  162. </>}
  163. </Webcam>
  164. </div>}
  165. <TextField
  166. id="name"
  167. name='name'
  168. label="Name"
  169. value={name}
  170. fullWidth
  171. variant='outlined'
  172. onChange={handleTextField}
  173. style={{marginBottom:30}}
  174. />
  175. <TextField
  176. id="lastName"
  177. name='lastName'
  178. label="LastName"
  179. value={lastName}
  180. fullWidth
  181. variant='outlined'
  182. onChange={handleTextField}
  183. />
  184. </div>
  185. )
  186. };
  187. export default EditList;