|
3 | 3 | import Badge from "@material-ui/core/Badge"; |
4 | 4 | import IconButton from "@material-ui/core/IconButton"; |
5 | 5 | import NotificationsIcon from "@material-ui/icons/Notifications"; |
6 | | -import React from "react"; |
| 6 | +import styled from "styled-components"; |
| 7 | +import { Link, Menu, MenuItem as MUMenuItem } from "@material-ui/core"; |
| 8 | +import React, { useState } from "react"; |
7 | 9 |
|
8 | | -export const AlertsDropdown = ({ alerts }: { alerts: Array<Alert> }) => ( |
9 | | - <IconButton color="inherit"> |
10 | | - <Badge badgeContent={alerts.length} color="secondary"> |
11 | | - <NotificationsIcon /> |
12 | | - </Badge> |
13 | | - </IconButton> |
| 10 | +const DROPDOWN_NAME = "@@profile/alerts/dropdown"; |
| 11 | + |
| 12 | +/* |
| 13 | + @TODO: <MenuItem /> on Material UI v3.9.2 doesn't have props to handle links in a better way. |
| 14 | + remove added styles once there are something again to handle link children |
| 15 | +*/ |
| 16 | +const MenuItem = styled(MUMenuItem)` |
| 17 | + && { |
| 18 | + padding: 0; |
| 19 | + position: relative; |
| 20 | + height: 2.4rem; |
| 21 | + width: 140px; |
| 22 | + } |
| 23 | +
|
| 24 | + > a { |
| 25 | + position: absolute; |
| 26 | + top: 0px; |
| 27 | + right: 0; |
| 28 | + bottom: 0; |
| 29 | + left: 0; |
| 30 | + display: flex; |
| 31 | + align-items: center; |
| 32 | + padding: 0 1rem; |
| 33 | + } |
| 34 | +`; |
| 35 | + |
| 36 | +const Dropdown = ({ anchorEl, handleClose, alerts }) => ( |
| 37 | + <Menu |
| 38 | + id={DROPDOWN_NAME} |
| 39 | + anchorEl={anchorEl} |
| 40 | + open={Boolean(anchorEl)} |
| 41 | + onClose={handleClose} |
| 42 | + > |
| 43 | + {alerts.map(alert => ( |
| 44 | + <MenuItem key={alert.created_at} onClick={handleClose}> |
| 45 | + <Link href={alert.data.original_post_id}>{alert.data.topic_title}</Link> |
| 46 | + </MenuItem> |
| 47 | + ))} |
| 48 | + </Menu> |
14 | 49 | ); |
15 | 50 |
|
16 | | -AlertsDropdown.defaultProps = { |
17 | | - alerts: [], |
| 51 | +export const AlertsDropdown = ( |
| 52 | + { alerts }: { alerts: Array<Alert> } = { alerts: [] } |
| 53 | +) => { |
| 54 | + const [anchorEl, setAnchorEl] = useState(null); |
| 55 | + const handleClose = () => setAnchorEl(null); |
| 56 | + const toggle = e => setAnchorEl(e.currentTarget); |
| 57 | + |
| 58 | + return ( |
| 59 | + <React.Fragment> |
| 60 | + <IconButton |
| 61 | + color="inherit" |
| 62 | + aria-owns={anchorEl ? DROPDOWN_NAME : undefined} |
| 63 | + aria-haspopup="true" |
| 64 | + onClick={toggle} |
| 65 | + > |
| 66 | + <Badge badgeContent={alerts.length} color="secondary"> |
| 67 | + <NotificationsIcon /> |
| 68 | + </Badge> |
| 69 | + </IconButton> |
| 70 | + <Dropdown anchorEl={anchorEl} handleClose={handleClose} alerts={alerts} /> |
| 71 | + </React.Fragment> |
| 72 | + ); |
18 | 73 | }; |
0 commit comments