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