Skip to content

Commit bb71989

Browse files
authored
fix: Merge pull request #455 from UniversalDataTool/notification
implement notificaiton
2 parents c9ac47e + c7b0a45 commit bb71989

File tree

2 files changed

+134
-7
lines changed

2 files changed

+134
-7
lines changed

src/components/Toasts/index.js

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ import * as colors from "@material-ui/core/colors"
55
import Collapse from "@material-ui/core/Collapse"
66
import Fade from "@material-ui/core/Fade"
77
import classNames from "classnames"
8+
import {
9+
CheckCircle as SuccessIcon,
10+
Close as CloseIcon,
11+
Error as ErrorIcon,
12+
Info as InfoIcon,
13+
Warning as WarningIcon,
14+
} from "@material-ui/icons"
15+
import Typography from "@material-ui/core/Typography"
16+
import IconButton from "@material-ui/core/IconButton"
817

918
const useStyles = makeStyles({
1019
root: {
1120
position: "fixed",
1221
display: "flex",
1322
flexDirection: "column",
14-
bottom: 0,
15-
left: 0,
16-
right: 0,
23+
bottom: 16,
24+
right: 16,
1725
alignItems: "flex-start",
18-
pointerEvents: "none",
1926
},
2027
msgBox: {
2128
display: "flex",
@@ -32,6 +39,35 @@ const useStyles = makeStyles({
3239
backgroundColor: colors.red[700],
3340
},
3441
},
42+
notificationPaper: {
43+
display: "flex",
44+
borderRadius: 10,
45+
padding: 12,
46+
boxShadow: `0 2.8px 2.2px rgba(0, 0, 0, 0.034),
47+
0 6.7px 5.3px rgba(0, 0, 0, 0.048);`,
48+
backgroundColor: "#FFF",
49+
margin: 6,
50+
},
51+
icon: {
52+
borderLeft: `3px solid ${colors.blue[700]}`,
53+
color: colors.blue[700],
54+
"&.warning": {
55+
borderLeft: `3px solid ${colors.orange[700]}`,
56+
color: colors.orange[700],
57+
},
58+
"&.error": {
59+
borderLeft: `3px solid ${colors.red[700]}`,
60+
color: colors.red[700],
61+
},
62+
"&.info": {
63+
borderLeft: `3px solid ${colors.blue[700]}`,
64+
color: colors.blue[700],
65+
},
66+
"&.success": {
67+
borderLeft: `3px solid ${colors.green[700]}`,
68+
color: colors.green[700],
69+
},
70+
},
3571
})
3672

3773
const REFRESH_INTERVAL = 100
@@ -70,6 +106,13 @@ export const ToastProvider = ({ children }) => {
70106
life: msg.life - REFRESH_INTERVAL,
71107
}))
72108
.filter((msg) => msg.life > 0)
109+
} else if (action.type === "remove") {
110+
return state
111+
.map((msg) => ({
112+
...msg,
113+
life: msg.life - REFRESH_INTERVAL,
114+
}))
115+
.filter((msg) => msg.id !== action.id)
73116
}
74117
return state
75118
}, [])
@@ -95,13 +138,59 @@ export const ToastProvider = ({ children }) => {
95138
{toasts.map((msg) => (
96139
<Collapse key={msg.id} in={msg.life < msg.fullLife}>
97140
<Fade in={msg.life > 600}>
98-
<div className={classNames(c.msgBox, msg.type)}>
99-
{msg.message}
100-
</div>
141+
<Notification
142+
type={msg.type}
143+
message={msg.message}
144+
onClose={() => changeToasts({ type: "remove", id: msg.id })}
145+
/>
101146
</Fade>
102147
</Collapse>
103148
))}
104149
</div>
105150
</>
106151
)
107152
}
153+
154+
export const Notification = ({ type, message, onClose }) => {
155+
const classes = useStyles()
156+
let Icon = null
157+
switch (type) {
158+
case "success":
159+
Icon = SuccessIcon
160+
break
161+
case "warning":
162+
Icon = WarningIcon
163+
break
164+
case "error":
165+
Icon = ErrorIcon
166+
break
167+
default:
168+
Icon = InfoIcon
169+
}
170+
return (
171+
<paper
172+
className={classes.notificationPaper}
173+
style={{ position: "relative" }}
174+
>
175+
<div className={classNames(classes.icon, type)}>
176+
<Icon fontSize="large" style={{ padding: 12 }} />
177+
</div>
178+
<div style={{ margin: "auto 0" }}>
179+
<Typography variant="body1">{message}</Typography>
180+
</div>
181+
<div
182+
style={{
183+
position: "absolute",
184+
top: -10,
185+
right: -10,
186+
backgroundColor: "#FFF",
187+
borderRadius: 100,
188+
}}
189+
>
190+
<IconButton color="action" size="small">
191+
<CloseIcon onClick={onClose} />
192+
</IconButton>
193+
</div>
194+
</paper>
195+
)
196+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// @flow
2+
3+
import React from "react"
4+
5+
import { storiesOf } from "@storybook/react"
6+
import { action } from "@storybook/addon-actions"
7+
8+
import { Notification } from "./"
9+
10+
storiesOf("toast notification", module).add("Basic", () => (
11+
<div>
12+
<Notification
13+
type="info"
14+
message="This is main info message text"
15+
onClose={action("onClose")}
16+
/>
17+
<Notification
18+
type="error"
19+
message="This is main error message text"
20+
onClose={action("onClose")}
21+
/>
22+
<Notification
23+
type="success"
24+
message="This is main success message text"
25+
onClose={action("onClose")}
26+
/>
27+
<Notification
28+
type="warning"
29+
message="This is main warning message text"
30+
onClose={action("onClose")}
31+
/>
32+
<Notification
33+
type="notUsedType"
34+
message="This is main notUsedType message text"
35+
onClose={action("onClose")}
36+
/>
37+
</div>
38+
))

0 commit comments

Comments
 (0)