Skip to content

Commit 4c3007d

Browse files
committed
Add material notifications
1 parent ced547d commit 4c3007d

File tree

2 files changed

+86
-111
lines changed

2 files changed

+86
-111
lines changed

src/components/Layout/Layout.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import BadgesPage from '../../pages/badge'
3838
import CarouselsPage from '../../pages/carousel'
3939
import CardsPage from '../../pages/cards'
4040
import ModalsPage from '../../pages/modal'
41-
// import NotificationsPage from '../../pages/notifications'
41+
import NotificationsPage from '../../pages/notifications'
4242
import NavbarsPage from '../../pages/nav'
4343
import TooltipsPage from '../../pages/tooltips'
4444
import TabsPage from '../../pages/tabs'
@@ -129,7 +129,7 @@ function Layout(props) {
129129
<Route path="/app/ui/cards" component={CardsPage} />
130130
<Route path="/app/ui/widget" component={WidgetsPage} />
131131
<Route path="/app/ui/progress" component={ProgressPage} />
132-
{/*<Route path="/app/ui/notifications" component={NotificationsPage} />*/}
132+
<Route path="/app/ui/notifications" component={NotificationsPage} />
133133

134134
<Route exact path="/app/forms" render={() => <Redirect to="/app/forms/elements" />} />
135135
<Route path="/app/forms/elements" component={FormsElements} />

src/pages/notifications/Notifications.js

Lines changed: 84 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import React, { useState } from "react";
2-
import { Grid, Box } from "@mui/material";
3-
import { toast } from "react-toastify";
4-
import classnames from "classnames";
2+
import {Grid, Box, Snackbar, Alert} from "@mui/material";
53

64
// styles
7-
import "react-toastify/dist/ReactToastify.css";
5+
import classnames from "classnames";
86
import useStyles from "./styles";
97

108
// components
@@ -13,21 +11,39 @@ import Notification from "../../components/Notification";
1311
import Code from "../../components/Code";
1412
import { Typography, Button } from "../../components/Wrappers";
1513

16-
const positions = [
17-
toast.POSITION.TOP_LEFT,
18-
toast.POSITION.TOP_CENTER,
19-
toast.POSITION.TOP_RIGHT,
20-
toast.POSITION.BOTTOM_LEFT,
21-
toast.POSITION.BOTTOM_CENTER,
22-
toast.POSITION.BOTTOM_RIGHT
23-
];
2414

2515
export default function NotificationsPage(props) {
26-
var classes = useStyles();
16+
let classes = useStyles();
17+
18+
const [type, setType] = useState({
19+
type: 'success',
20+
message: 'This is a success message!'
21+
})
22+
const [state, setState] = useState({
23+
open: false,
24+
vertical: 'top',
25+
horizontal: 'center',
26+
});
27+
28+
const { vertical, horizontal, open } = state;
29+
30+
const handleClick = (newState, positionId) => () => {
31+
setState({ open: true, ...newState });
32+
setNotificationPosition(positionId)
33+
};
34+
35+
const handleClose = () => {
36+
setState({ ...state, open: false });
37+
};
2738

2839
// local
29-
var [notificationsPosition, setNotificationPosition] = useState(2);
30-
var [errorToastId, setErrorToastId] = useState(null);
40+
let [notificationsPosition, setNotificationPosition] = useState(2);
41+
let [errorToastId, setErrorToastId] = useState(null);
42+
43+
const handleChange = (type) => {
44+
setType(type)
45+
setState({...state, open: true})
46+
}
3147

3248
return (
3349
<>
@@ -46,19 +62,29 @@ export default function NotificationsPage(props) {
4662
<div className={classes.layoutContainer}>
4763
<div className={classes.layoutButtonsRow}>
4864
<button
49-
onClick={() => changeNotificationPosition(0)}
65+
onClick={handleClick({
66+
vertical: 'top',
67+
horizontal: 'left',
68+
}, 0)}
69+
5070
className={classnames(classes.layoutButton, {
5171
[classes.layoutButtonActive]: notificationsPosition === 0
5272
})}
5373
/>
5474
<button
55-
onClick={() => changeNotificationPosition(1)}
75+
onClick={handleClick({
76+
vertical: 'top',
77+
horizontal: 'center',
78+
}, 1)}
5679
className={classnames(classes.layoutButton, {
5780
[classes.layoutButtonActive]: notificationsPosition === 1
5881
})}
5982
/>
6083
<button
61-
onClick={() => changeNotificationPosition(2)}
84+
onClick={handleClick({
85+
vertical: 'top',
86+
horizontal: 'right',
87+
}, 2)}
6288
className={classnames(classes.layoutButton, {
6389
[classes.layoutButtonActive]: notificationsPosition === 2
6490
})}
@@ -69,19 +95,28 @@ export default function NotificationsPage(props) {
6995
</Typography>
7096
<div className={classes.layoutButtonsRow}>
7197
<button
72-
onClick={() => changeNotificationPosition(3)}
98+
onClick={handleClick({
99+
vertical: 'bottom',
100+
horizontal: 'left',
101+
}, 3)}
73102
className={classnames(classes.layoutButton, {
74103
[classes.layoutButtonActive]: notificationsPosition === 3
75104
})}
76105
/>
77106
<button
78-
onClick={() => changeNotificationPosition(4)}
107+
onClick={handleClick({
108+
vertical: 'bottom',
109+
horizontal: 'center',
110+
}, 4)}
79111
className={classnames(classes.layoutButton, {
80112
[classes.layoutButtonActive]: notificationsPosition === 4
81113
})}
82114
/>
83115
<button
84-
onClick={() => changeNotificationPosition(5)}
116+
onClick={handleClick({
117+
vertical: 'bottom',
118+
horizontal: 'right',
119+
}, 5)}
85120
className={classnames(classes.layoutButton, {
86121
[classes.layoutButtonActive]: notificationsPosition === 5
87122
})}
@@ -102,23 +137,23 @@ export default function NotificationsPage(props) {
102137
<Button
103138
variant="contained"
104139
color="primary"
105-
onClick={() => handleNotificationCall("info")}
140+
onClick={() => handleChange({type: 'info', message: 'This is a info message!'})}
106141
className={classnames(classes.notificationCallButton)}
107142
>
108143
Info Message
109144
</Button>
110145
<Button
111146
variant="contained"
112147
color="secondary"
113-
onClick={() => handleNotificationCall("error")}
148+
onClick={() => handleChange({type: 'error', message: 'This is a error message!'})}
114149
className={classnames(classes.notificationCallButton)}
115150
>
116151
Error + Retry Message
117152
</Button>
118153
<Button
119154
variant="contained"
120155
color="success"
121-
onClick={() => handleNotificationCall("success")}
156+
onClick={() => handleChange({type: "success", message: 'This is a success message!' })}
122157
className={classnames(classes.notificationCallButton)}
123158
>
124159
Success Message
@@ -132,23 +167,26 @@ export default function NotificationsPage(props) {
132167
</Typography>
133168
<Typography>
134169
Notifications are created with the help of{" "}
135-
<a href="https://github.com/fkhadra/react-toastify">
136-
react-toastify
170+
<a href="https://mui.com/material-ui/react-snackbar/">
171+
react-snackbar
137172
</a>
138173
</Typography>
139174
<Code>{`
140175
// import needed components, functions and styles
141-
import { ToastContainer, toast } from 'react-toastify';
142-
import 'react-toastify/dist/ReactToastify.css';
176+
import Snackbar from '@mui/material/Snackbar';
143177
144-
const Page = () => {
145-
<div>
146-
<ToastContainer />
147-
<button onClick={() => toast('Toast Message')}>
148-
show notification
149-
</button>
150-
</div>
151-
};
178+
return (
179+
<div>
180+
<Button onClick={handleClick}>Open simple snackbar</Button>
181+
<Snackbar
182+
open={open}
183+
autoHideDuration={6000}
184+
onClose={handleClose}
185+
message="Note archived"
186+
action={action}
187+
/>
188+
</div>
189+
);
152190
`}</Code>
153191
<Box py={1}>
154192
<Typography variant="caption">
@@ -301,79 +339,16 @@ export default function NotificationsPage(props) {
301339
</Widget>
302340
</Grid>
303341
</Grid>
342+
<Snackbar
343+
anchorOrigin={{ vertical, horizontal }}
344+
open={open}
345+
onClose={handleClose}
346+
key={vertical + horizontal}
347+
>
348+
<Alert onClose={handleClose} severity={type.type} sx={{ width: '100%' }}>
349+
{type.message}
350+
</Alert>
351+
</Snackbar>
304352
</>
305353
);
306-
307-
// #############################################################
308-
function sendNotification(componentProps, options) {
309-
return toast(
310-
<Notification
311-
{...componentProps}
312-
className={classes.notificationComponent}
313-
/>,
314-
options
315-
);
316-
}
317-
318-
function retryErrorNotification() {
319-
var componentProps = {
320-
type: "message",
321-
message: "Message was sent successfully!",
322-
variant: "contained",
323-
color: "success"
324-
};
325-
toast.update(errorToastId, {
326-
render: <Notification {...componentProps} />,
327-
type: "success"
328-
});
329-
setErrorToastId(null);
330-
}
331-
332-
function handleNotificationCall(notificationType) {
333-
var componentProps;
334-
335-
if (errorToastId && notificationType === "error") return;
336-
337-
switch (notificationType) {
338-
case "info":
339-
componentProps = {
340-
type: "feedback",
341-
message: "New user feedback received",
342-
variant: "contained",
343-
color: "primary"
344-
};
345-
break;
346-
case "error":
347-
componentProps = {
348-
type: "message",
349-
message: "Message was not sent!",
350-
variant: "contained",
351-
color: "secondary",
352-
extraButton: "Resend",
353-
extraButtonClick: retryErrorNotification
354-
};
355-
break;
356-
default:
357-
componentProps = {
358-
type: "shipped",
359-
message: "The item was shipped",
360-
variant: "contained",
361-
color: "success"
362-
};
363-
}
364-
365-
var toastId = sendNotification(componentProps, {
366-
type: notificationType,
367-
position: positions[notificationsPosition],
368-
progressClassName: classes.progress,
369-
onClose: notificationType === "error" && (() => setErrorToastId(null)),
370-
className: classes.notification
371-
});
372-
373-
if (notificationType === "error") setErrorToastId(toastId);
374-
}
375-
376-
function changeNotificationPosition(positionId) {
377-
setNotificationPosition(positionId);
378-
}
379354
}

0 commit comments

Comments
 (0)