(state: State, action: Action)
| 72 | }; |
| 73 | |
| 74 | export const reducer = (state: State, action: Action): State => { |
| 75 | switch (action.type) { |
| 76 | case 'ADD_TOAST': |
| 77 | return { |
| 78 | ...state, |
| 79 | toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT), |
| 80 | }; |
| 81 | |
| 82 | case 'UPDATE_TOAST': |
| 83 | return { |
| 84 | ...state, |
| 85 | toasts: state.toasts.map((t) => |
| 86 | t.id === action.toast.id ? { ...t, ...action.toast } : t, |
| 87 | ), |
| 88 | }; |
| 89 | |
| 90 | case 'DISMISS_TOAST': { |
| 91 | const { toastId } = action; |
| 92 | |
| 93 | // ! Side effects ! - This could be extracted into a dismissToast() action, |
| 94 | // but I'll keep it here for simplicity |
| 95 | if (toastId) { |
| 96 | addToRemoveQueue(toastId); |
| 97 | } else { |
| 98 | state.toasts.forEach((toast) => { |
| 99 | addToRemoveQueue(toast.id); |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | return { |
| 104 | ...state, |
| 105 | toasts: state.toasts.map((t) => |
| 106 | t.id === toastId || toastId === undefined |
| 107 | ? { |
| 108 | ...t, |
| 109 | open: false, |
| 110 | } |
| 111 | : t, |
| 112 | ), |
| 113 | }; |
| 114 | } |
| 115 | case 'REMOVE_TOAST': |
| 116 | if (action.toastId === undefined) { |
| 117 | return { |
| 118 | ...state, |
| 119 | toasts: [], |
| 120 | }; |
| 121 | } |
| 122 | return { |
| 123 | ...state, |
| 124 | toasts: state.toasts.filter((t) => t.id !== action.toastId), |
| 125 | }; |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | const listeners: Array<(state: State) => void> = []; |
| 130 |
no test coverage detected