|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { connect } from "react-redux"; |
| 3 | +import { ListItemAvatar, ListItemText } from "@material-ui/core"; |
| 4 | +import { makeStyles } from "@material-ui/core/styles"; |
| 5 | +import IconButton from "@material-ui/core/IconButton"; |
| 6 | +import LocalOfferIcon from "@material-ui/icons/LocalOffer"; |
| 7 | +import Menu from "@material-ui/core/Menu"; |
| 8 | +import MenuItem from "@material-ui/core/MenuItem"; |
| 9 | +import moment from "moment"; |
| 10 | +import Typography from "@material-ui/core/Typography"; |
| 11 | +import ClearIcon from "@material-ui/icons/Clear"; |
| 12 | +import CheckIcon from "@material-ui/icons/Check"; |
| 13 | +import ErrorIcon from "@material-ui/icons/Error"; |
| 14 | +import CancelIcon from "@material-ui/icons/Cancel"; |
| 15 | +import MoreHorizIcon from "@material-ui/icons/MoreHoriz"; |
| 16 | +import CheckBoxIcon from "@material-ui/icons/CheckBox"; |
| 17 | +import MarkunreadIcon from "@material-ui/icons/Markunread"; |
| 18 | +import Button from "@material-ui/core/Button"; |
| 19 | +import { |
| 20 | + updateNotificationAsync, |
| 21 | + removeNotificationAsync, |
| 22 | +} from "../../../../redux/actions/notificationActions"; |
| 23 | + |
| 24 | +const useStyles = makeStyles(() => ({ |
| 25 | + acceptedIcon: { |
| 26 | + color: "green", |
| 27 | + }, |
| 28 | + rejectedIcon: { |
| 29 | + color: "red", |
| 30 | + }, |
| 31 | + moreItems: { |
| 32 | + textTransform: "none", |
| 33 | + }, |
| 34 | +})); |
| 35 | + |
| 36 | +const SeeMoreMenu = ({ |
| 37 | + dispatch, |
| 38 | + handleDeleteClick, |
| 39 | + onUpdateStatus, |
| 40 | + _id, |
| 41 | + isRead, |
| 42 | +}) => { |
| 43 | + const [anchorEl, setAnchorEl] = useState(null); |
| 44 | + const classes = useStyles(); |
| 45 | + const open = Boolean(anchorEl); |
| 46 | + |
| 47 | + const handleClick = (event) => { |
| 48 | + setAnchorEl(event.currentTarget); |
| 49 | + }; |
| 50 | + |
| 51 | + const handleClose = () => { |
| 52 | + setAnchorEl(null); |
| 53 | + }; |
| 54 | + |
| 55 | + const handleReadClick = () => { |
| 56 | + dispatch(updateNotificationAsync(_id, !isRead)).then(() => { |
| 57 | + onUpdateStatus(_id, !isRead); |
| 58 | + }); |
| 59 | + }; |
| 60 | + |
| 61 | + return ( |
| 62 | + <div> |
| 63 | + <IconButton |
| 64 | + aria-label="more" |
| 65 | + aria-controls="more-menu" |
| 66 | + aria-haspopup="true" |
| 67 | + onClick={handleClick} |
| 68 | + > |
| 69 | + <MoreHorizIcon /> |
| 70 | + </IconButton> |
| 71 | + <Menu |
| 72 | + id="more-menu" |
| 73 | + anchorEl={anchorEl} |
| 74 | + anchorOrigin={{ |
| 75 | + vertical: "bottom", |
| 76 | + horizontal: "center", |
| 77 | + }} |
| 78 | + transformOrigin={{ |
| 79 | + vertical: "top", |
| 80 | + horizontal: "center", |
| 81 | + }} |
| 82 | + keepMounted |
| 83 | + open={open} |
| 84 | + onClose={handleClose} |
| 85 | + > |
| 86 | + <MenuItem onClick={handleClose}> |
| 87 | + <Button |
| 88 | + color="inherit" |
| 89 | + onClick={handleReadClick} |
| 90 | + className={classes.moreItems} |
| 91 | + startIcon={isRead ? <MarkunreadIcon /> : <CheckIcon />} |
| 92 | + > |
| 93 | + {isRead ? "Mark as unread" : "Mark as read"} |
| 94 | + </Button> |
| 95 | + </MenuItem> |
| 96 | + <MenuItem onClick={handleClose}> |
| 97 | + <Button |
| 98 | + color="inherit" |
| 99 | + onClick={handleDeleteClick} |
| 100 | + className={classes.moreItems} |
| 101 | + startIcon={<ClearIcon />} |
| 102 | + > |
| 103 | + Clear |
| 104 | + </Button> |
| 105 | + </MenuItem> |
| 106 | + </Menu> |
| 107 | + </div> |
| 108 | + ); |
| 109 | +}; |
| 110 | + |
| 111 | +const Notification = ({ |
| 112 | + dispatch, |
| 113 | + _id, |
| 114 | + type, |
| 115 | + date, |
| 116 | + isRead, |
| 117 | + content, |
| 118 | + key, |
| 119 | + onUpdateStatus, |
| 120 | +}) => { |
| 121 | + const classes = useStyles(); |
| 122 | + |
| 123 | + const [isExisting, setIsExisting] = useState(true); |
| 124 | + |
| 125 | + const handleDeleteClick = () => { |
| 126 | + dispatch(removeNotificationAsync(_id)).then(() => { |
| 127 | + setIsExisting(false); |
| 128 | + }); |
| 129 | + }; |
| 130 | + |
| 131 | + const getItemAvatar = (type) => { |
| 132 | + if (type === "newOffering") { |
| 133 | + return <LocalOfferIcon color="primary" />; |
| 134 | + } else if (type === "offeringAccepted") { |
| 135 | + return <CheckBoxIcon className={classes.acceptedIcon} />; |
| 136 | + } else if (type === "offeringRejected") { |
| 137 | + return <CancelIcon className={classes.rejectedIcon} />; |
| 138 | + } else { |
| 139 | + return <ErrorIcon />; |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + return isExisting ? ( |
| 144 | + <MenuItem selected={!isRead} divider={true} key={key}> |
| 145 | + <ListItemAvatar>{getItemAvatar(type)}</ListItemAvatar> |
| 146 | + |
| 147 | + <ListItemText |
| 148 | + primary={content} |
| 149 | + primaryTypographyProps={{ |
| 150 | + style: { whiteSpace: "normal", wordWrap: "break-word" }, |
| 151 | + }} |
| 152 | + secondary={moment(date).format("MMMM Do YYYY, h:mm a")} |
| 153 | + /> |
| 154 | + |
| 155 | + <SeeMoreMenu |
| 156 | + dispatch={dispatch} |
| 157 | + handleDeleteClick={handleDeleteClick} |
| 158 | + onUpdateStatus={onUpdateStatus} |
| 159 | + _id={_id} |
| 160 | + isRead={isRead} |
| 161 | + className={classes.seeMoreMenu} |
| 162 | + /> |
| 163 | + </MenuItem> |
| 164 | + ) : null; |
| 165 | +}; |
| 166 | + |
| 167 | +export default connect()(Notification); |
0 commit comments