Skip to content

Commit aa91cae

Browse files
committed
feat(notifications): add ability to toggle a dropdown with alerts
1 parent a7c5d68 commit aa91cae

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

flow-typed/myLibDef.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ declare type Link = {
1919

2020
declare type Notification = {
2121
created_at: string,
22-
data: {
22+
data: $Shape<{
2323
display_username: string,
2424
group_id: number,
25+
original_post_id: number,
26+
original_post_type: number,
27+
original_username: string,
28+
revision_number: number,
2529
topic_title: string,
26-
},
30+
}>,
2731
fancy_title: string,
2832
id: number,
2933
notification_type: number,

src/profile/AlertsDropdown.js

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,71 @@
33
import Badge from "@material-ui/core/Badge";
44
import IconButton from "@material-ui/core/IconButton";
55
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";
79

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>
1449
);
1550

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+
);
1873
};

0 commit comments

Comments
 (0)