1
1
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" ;
5
3
6
4
// styles
7
- import "react-toastify/dist/ReactToastify.css ";
5
+ import classnames from "classnames ";
8
6
import useStyles from "./styles" ;
9
7
10
8
// components
@@ -13,21 +11,39 @@ import Notification from "../../components/Notification";
13
11
import Code from "../../components/Code" ;
14
12
import { Typography , Button } from "../../components/Wrappers" ;
15
13
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
- ] ;
24
14
25
15
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
+ } ;
27
38
28
39
// 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
+ }
31
47
32
48
return (
33
49
< >
@@ -46,19 +62,29 @@ export default function NotificationsPage(props) {
46
62
< div className = { classes . layoutContainer } >
47
63
< div className = { classes . layoutButtonsRow } >
48
64
< button
49
- onClick = { ( ) => changeNotificationPosition ( 0 ) }
65
+ onClick = { handleClick ( {
66
+ vertical : 'top' ,
67
+ horizontal : 'left' ,
68
+ } , 0 ) }
69
+
50
70
className = { classnames ( classes . layoutButton , {
51
71
[ classes . layoutButtonActive ] : notificationsPosition === 0
52
72
} ) }
53
73
/>
54
74
< button
55
- onClick = { ( ) => changeNotificationPosition ( 1 ) }
75
+ onClick = { handleClick ( {
76
+ vertical : 'top' ,
77
+ horizontal : 'center' ,
78
+ } , 1 ) }
56
79
className = { classnames ( classes . layoutButton , {
57
80
[ classes . layoutButtonActive ] : notificationsPosition === 1
58
81
} ) }
59
82
/>
60
83
< button
61
- onClick = { ( ) => changeNotificationPosition ( 2 ) }
84
+ onClick = { handleClick ( {
85
+ vertical : 'top' ,
86
+ horizontal : 'right' ,
87
+ } , 2 ) }
62
88
className = { classnames ( classes . layoutButton , {
63
89
[ classes . layoutButtonActive ] : notificationsPosition === 2
64
90
} ) }
@@ -69,19 +95,28 @@ export default function NotificationsPage(props) {
69
95
</ Typography >
70
96
< div className = { classes . layoutButtonsRow } >
71
97
< button
72
- onClick = { ( ) => changeNotificationPosition ( 3 ) }
98
+ onClick = { handleClick ( {
99
+ vertical : 'bottom' ,
100
+ horizontal : 'left' ,
101
+ } , 3 ) }
73
102
className = { classnames ( classes . layoutButton , {
74
103
[ classes . layoutButtonActive ] : notificationsPosition === 3
75
104
} ) }
76
105
/>
77
106
< button
78
- onClick = { ( ) => changeNotificationPosition ( 4 ) }
107
+ onClick = { handleClick ( {
108
+ vertical : 'bottom' ,
109
+ horizontal : 'center' ,
110
+ } , 4 ) }
79
111
className = { classnames ( classes . layoutButton , {
80
112
[ classes . layoutButtonActive ] : notificationsPosition === 4
81
113
} ) }
82
114
/>
83
115
< button
84
- onClick = { ( ) => changeNotificationPosition ( 5 ) }
116
+ onClick = { handleClick ( {
117
+ vertical : 'bottom' ,
118
+ horizontal : 'right' ,
119
+ } , 5 ) }
85
120
className = { classnames ( classes . layoutButton , {
86
121
[ classes . layoutButtonActive ] : notificationsPosition === 5
87
122
} ) }
@@ -102,23 +137,23 @@ export default function NotificationsPage(props) {
102
137
< Button
103
138
variant = "contained"
104
139
color = "primary"
105
- onClick = { ( ) => handleNotificationCall ( " info" ) }
140
+ onClick = { ( ) => handleChange ( { type : ' info' , message : 'This is a info message!' } ) }
106
141
className = { classnames ( classes . notificationCallButton ) }
107
142
>
108
143
Info Message
109
144
</ Button >
110
145
< Button
111
146
variant = "contained"
112
147
color = "secondary"
113
- onClick = { ( ) => handleNotificationCall ( " error" ) }
148
+ onClick = { ( ) => handleChange ( { type : ' error' , message : 'This is a error message!' } ) }
114
149
className = { classnames ( classes . notificationCallButton ) }
115
150
>
116
151
Error + Retry Message
117
152
</ Button >
118
153
< Button
119
154
variant = "contained"
120
155
color = "success"
121
- onClick = { ( ) => handleNotificationCall ( "success" ) }
156
+ onClick = { ( ) => handleChange ( { type : "success" , message : 'This is a success message!' } ) }
122
157
className = { classnames ( classes . notificationCallButton ) }
123
158
>
124
159
Success Message
@@ -132,23 +167,26 @@ export default function NotificationsPage(props) {
132
167
</ Typography >
133
168
< Typography >
134
169
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
137
172
</ a >
138
173
</ Typography >
139
174
< Code > { `
140
175
// 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';
143
177
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
+ );
152
190
` } </ Code >
153
191
< Box py = { 1 } >
154
192
< Typography variant = "caption" >
@@ -301,79 +339,16 @@ export default function NotificationsPage(props) {
301
339
</ Widget >
302
340
</ Grid >
303
341
</ 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 >
304
352
</ >
305
353
) ;
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
- }
379
354
}
0 commit comments