@@ -15,37 +15,35 @@ type ToasterToast = ToastProps & {
15
15
action ?: ToastActionElement ;
16
16
} ;
17
17
18
- const actionTypes = {
19
- ADD_TOAST : "ADD_TOAST" ,
20
- UPDATE_TOAST : "UPDATE_TOAST" ,
21
- DISMISS_TOAST : "DISMISS_TOAST" ,
22
- REMOVE_TOAST : "REMOVE_TOAST" ,
23
- } as const ;
24
-
25
18
let count = 0 ;
26
19
27
20
function genId ( ) {
28
21
count = ( count + 1 ) % Number . MAX_SAFE_INTEGER ;
29
22
return count . toString ( ) ;
30
23
}
31
24
32
- type ActionType = typeof actionTypes ;
25
+ const enum ActionType {
26
+ ADD_TOAST = "ADD_TOAST" ,
27
+ UPDATE_TOAST = "UPDATE_TOAST" ,
28
+ DISMISS_TOAST = "DISMISS_TOAST" ,
29
+ REMOVE_TOAST = "REMOVE_TOAST" ,
30
+ }
33
31
34
32
type Action =
35
33
| {
36
- type : ActionType [ " ADD_TOAST" ] ;
34
+ type : ActionType . ADD_TOAST ;
37
35
toast : ToasterToast ;
38
36
}
39
37
| {
40
- type : ActionType [ " UPDATE_TOAST" ] ;
38
+ type : ActionType . UPDATE_TOAST ;
41
39
toast : Partial < ToasterToast > ;
42
40
}
43
41
| {
44
- type : ActionType [ " DISMISS_TOAST" ] ;
42
+ type : ActionType . DISMISS_TOAST ;
45
43
toastId ?: ToasterToast [ "id" ] ;
46
44
}
47
45
| {
48
- type : ActionType [ " REMOVE_TOAST" ] ;
46
+ type : ActionType . REMOVE_TOAST ;
49
47
toastId ?: ToasterToast [ "id" ] ;
50
48
} ;
51
49
@@ -63,7 +61,7 @@ const addToRemoveQueue = (toastId: string) => {
63
61
const timeout = setTimeout ( ( ) => {
64
62
toastTimeouts . delete ( toastId ) ;
65
63
dispatch ( {
66
- type : " REMOVE_TOAST" ,
64
+ type : ActionType . REMOVE_TOAST ,
67
65
toastId : toastId ,
68
66
} ) ;
69
67
} , TOAST_REMOVE_DELAY ) ;
@@ -73,21 +71,21 @@ const addToRemoveQueue = (toastId: string) => {
73
71
74
72
export const reducer = ( state : State , action : Action ) : State => {
75
73
switch ( action . type ) {
76
- case " ADD_TOAST" :
74
+ case ActionType . ADD_TOAST :
77
75
return {
78
76
...state ,
79
77
toasts : [ action . toast , ...state . toasts ] . slice ( 0 , TOAST_LIMIT ) ,
80
78
} ;
81
79
82
- case " UPDATE_TOAST" :
80
+ case ActionType . UPDATE_TOAST :
83
81
return {
84
82
...state ,
85
83
toasts : state . toasts . map ( ( t ) =>
86
84
t . id === action . toast . id ? { ...t , ...action . toast } : t ,
87
85
) ,
88
86
} ;
89
87
90
- case " DISMISS_TOAST" : {
88
+ case ActionType . DISMISS_TOAST : {
91
89
const { toastId } = action ;
92
90
93
91
// ! Side effects ! - This could be extracted into a dismissToast() action,
@@ -112,7 +110,7 @@ export const reducer = (state: State, action: Action): State => {
112
110
) ,
113
111
} ;
114
112
}
115
- case " REMOVE_TOAST" :
113
+ case ActionType . REMOVE_TOAST :
116
114
if ( action . toastId === undefined ) {
117
115
return {
118
116
...state ,
@@ -144,13 +142,14 @@ function toast({ ...props }: Toast) {
144
142
145
143
const update = ( props : ToasterToast ) =>
146
144
dispatch ( {
147
- type : " UPDATE_TOAST" ,
145
+ type : ActionType . UPDATE_TOAST ,
148
146
toast : { ...props , id } ,
149
147
} ) ;
150
- const dismiss = ( ) => dispatch ( { type : "DISMISS_TOAST" , toastId : id } ) ;
148
+ const dismiss = ( ) =>
149
+ dispatch ( { type : ActionType . DISMISS_TOAST , toastId : id } ) ;
151
150
152
151
dispatch ( {
153
- type : " ADD_TOAST" ,
152
+ type : ActionType . ADD_TOAST ,
154
153
toast : {
155
154
...props ,
156
155
id,
@@ -184,7 +183,8 @@ function useToast() {
184
183
return {
185
184
...state ,
186
185
toast,
187
- dismiss : ( toastId ?: string ) => dispatch ( { type : "DISMISS_TOAST" , toastId } ) ,
186
+ dismiss : ( toastId ?: string ) =>
187
+ dispatch ( { type : ActionType . DISMISS_TOAST , toastId } ) ,
188
188
} ;
189
189
}
190
190
0 commit comments