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