index.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { makeStyles } from '@material-ui/core'
  2. import { useState } from 'react';
  3. import { useDispatch } from 'react-redux';
  4. import { styled } from '@mui/material/styles';
  5. import Button from '@mui/material/Button';
  6. import Menu from '@mui/material/Menu';
  7. import MenuItem from '@mui/material/MenuItem';
  8. import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
  9. import ListItemButton from '@mui/material/ListItemButton';
  10. import Avatar from '@mui/material/Avatar';
  11. import ListItemText from '@mui/material/ListItemText';
  12. import ListItemIcon from '@mui/material/ListItemIcon';
  13. import ContentCopyIcon from '@mui/icons-material/ContentCopy';
  14. import { CopyToClipboard } from 'react-copy-to-clipboard';
  15. import {removeContact } from '../../../../../api-data';
  16. import { actionIsOpen } from '../../../../../redux/control/action';
  17. import { TContact } from '../../../../../typescript/redux/contacts/types';
  18. import { TIsOpen } from '../../../../../typescript/redux/control/types';
  19. import { firstLetter,slicedWord,timeStampEU,copied,prodBaseURL } from '../../../../../helpers';
  20. const StyledMenu = styled((props:any) => (
  21. <Menu
  22. elevation={0}
  23. anchorOrigin={{
  24. vertical: 'top',
  25. horizontal: 'right',
  26. }}
  27. transformOrigin={{
  28. vertical: 'bottom',
  29. horizontal: 'right',
  30. }}
  31. {...props}
  32. />
  33. ))(({ theme }:any) => ({
  34. '& .MuiPaper-root': {
  35. borderRadius: 10,
  36. marginTop: theme.spacing(0),
  37. minWidth: 220,
  38. color:
  39. theme.palette.mode === 'light' ? 'rgb(55, 65, 81)' : theme.palette.grey[500],
  40. boxShadow:
  41. 'rgb(255, 255, 255) 0px 0px 0px 0px, rgba(0, 0, 0, 0.05) 0px 0px 0px 1px, rgba(0, 0, 0, 0.1) 0px 10px 15px -3px, rgba(0, 0, 0, 0.05) 0px 4px 6px -2px',
  42. '& .MuiMenu-list': {
  43. padding: '8px 8px',
  44. },
  45. '& .MuiMenuItem-root': {
  46. marginBottom: theme.spacing(1),
  47. '& .MuiSvgIcon-root': {
  48. fontSize: 21,
  49. color: theme.palette.text.secondary,
  50. marginRight: theme.spacing(4),
  51. }
  52. },
  53. },
  54. }));
  55. const useStyles = makeStyles({
  56. listItem_iconAvatar: {
  57. marginRight:10
  58. },
  59. modalDelete: {
  60. background: '#ffffff',
  61. position: 'absolute',
  62. content:'',
  63. width: '20%',
  64. height:'auto',
  65. left: '40%',
  66. bottom: '48.5%',
  67. borderRadius: 10,
  68. padding: 10,
  69. display: 'flex',
  70. flexDirection:'column'
  71. },
  72. overlay: {
  73. position: 'fixed',
  74. top: 0,
  75. left: 0,
  76. width: '100vw',
  77. height: '100vh',
  78. zIndex: 100,
  79. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  80. overflowY: 'hidden',
  81. },
  82. titleWrapper: {
  83. display: 'flex',
  84. justifyContent: 'flex-start',
  85. alignContent: 'center',
  86. alignItems:'center'
  87. },
  88. })
  89. interface IContactItem {
  90. contact: TContact,
  91. handleListItemClick: (companionId: string) => void,
  92. isOpen: TIsOpen,
  93. }
  94. const ContactItem = ({contact,handleListItemClick,isOpen}:IContactItem) => {
  95. const classes = useStyles()
  96. const dispatch = useDispatch()
  97. const [anchorEl, setAnchorEl] = useState<any>(null);
  98. const [selected, setSelected] = useState<boolean>(false);
  99. const [modal,setModal] = useState<boolean>(false)
  100. const open = Boolean(anchorEl);
  101. const { name, lastName, avatarUrl, color, companionId,createdAt, number,_id } = contact
  102. const handleClose = (type: string | undefined): void => {
  103. if (type === 'copy') copied('Number')
  104. if (type === 'delete') setModal(true)
  105. setAnchorEl(null)
  106. setSelected(false)
  107. }
  108. const handleDeleteModal = (e: any) => {
  109. const id = e.target.id
  110. if (id === 'overlay' || id === 'cancel') return setModal(false)
  111. if (id === 'delete') {
  112. isOpen === 'edit'&&dispatch(actionIsOpen('credentials'))
  113. removeContact(_id)
  114. setModal(false)
  115. }
  116. }
  117. const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
  118. e.preventDefault()
  119. setAnchorEl(e.currentTarget)
  120. setSelected(true)
  121. }
  122. return (
  123. <div>
  124. <ListItemButton
  125. selected={selected}
  126. onClick={() => handleListItemClick(companionId)}
  127. onContextMenu={(e) => handleContextMenu(e)}
  128. >
  129. <ListItemIcon className={classes.listItem_iconAvatar}>
  130. <Avatar alt={name} src={avatarUrl?`${prodBaseURL}/${avatarUrl}`:undefined}
  131. sx={{ background: color, width: 54, height: 54 }}>
  132. {!avatarUrl&&`${firstLetter(name)}${firstLetter(lastName)}`}
  133. </Avatar>
  134. </ListItemIcon>
  135. <ListItemText primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  136. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  137. secondary={`Registered since ${timeStampEU(createdAt)}`} />
  138. </ListItemButton>
  139. <StyledMenu
  140. id="demo-positioned-menu"
  141. aria-labelledby="demo-positioned-button"
  142. anchorEl={anchorEl}
  143. open={open}
  144. onClose={handleClose}
  145. >
  146. <CopyToClipboard onCopy={() => handleClose('copy')} text={number}>
  147. <MenuItem>
  148. <ContentCopyIcon />
  149. Copy number
  150. </MenuItem>
  151. </CopyToClipboard>
  152. <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
  153. <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
  154. Delete contact
  155. </MenuItem>
  156. </StyledMenu>
  157. {modal&&<div onClick={handleDeleteModal} className={classes.overlay} id='overlay'>
  158. <div className={classes.modalDelete}>
  159. <div className={classes.titleWrapper}>
  160. <Avatar alt={name} src={avatarUrl?`${prodBaseURL}/${avatarUrl}`:undefined}
  161. sx={{ background: color, width: 38, height: 38,marginRight:2}}>
  162. {`${firstLetter(name)}${firstLetter(lastName)}`}
  163. </Avatar>
  164. <h3 style={{color: '#2c2c2c'}}>Delete contact</h3>
  165. </div>
  166. <p style={{ color: '#050505' }}>{`Are you sure you want to delete contact
  167. ${`${firstLetter(name)}${slicedWord(name, 15, 1)}
  168. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}?`}</p>
  169. <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
  170. DELETE CONTACT
  171. </Button>
  172. <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
  173. CANCEL
  174. </Button>
  175. </div>
  176. </div>}
  177. </div>
  178. );
  179. }
  180. export default ContactItem