|
1 | | -import { getType } from 'typesafe-actions'; |
| 1 | +import { createReducer } from 'typesafe-actions'; |
2 | 2 |
|
3 | | -import { actions, defaultNotificationsState, Types } from './index'; |
4 | | -import { defaultNotification } from './state'; |
| 3 | +import { actions } from './index'; |
| 4 | +import { defaultNotification, defaultNotificationsState } from './state'; |
5 | 5 |
|
6 | | -export default ( |
7 | | - state: Types.NotificationsState = defaultNotificationsState, |
8 | | - action: Types.NotificationsActions |
9 | | -): Types.NotificationsState => { |
10 | | - switch (action.type) { |
11 | | - // Info |
12 | | - case getType(actions.addInfo): |
13 | | - return [ |
14 | | - ...state, |
15 | | - { |
16 | | - ...defaultNotification, |
17 | | - content: action.payload, |
18 | | - variant: 'info', |
19 | | - id: Date.now() |
20 | | - } |
21 | | - ]; |
22 | | - |
23 | | - // Error |
24 | | - case getType(actions.addError): |
25 | | - return [ |
26 | | - ...state, |
27 | | - { |
28 | | - ...defaultNotification, |
29 | | - content: action.payload, |
30 | | - variant: 'error', |
31 | | - id: Date.now() |
32 | | - } |
33 | | - ]; |
34 | | - |
35 | | - // Success |
36 | | - case getType(actions.addSuccess): |
37 | | - return [ |
38 | | - ...state, |
39 | | - { |
40 | | - ...defaultNotification, |
41 | | - content: action.payload, |
42 | | - variant: 'success', |
43 | | - id: Date.now() |
44 | | - } |
45 | | - ]; |
46 | | - |
47 | | - case getType(actions.closeNotification): |
48 | | - return state.map(notification => ({ ...notification, open: false })); |
49 | | - |
50 | | - default: |
51 | | - return state; |
52 | | - } |
53 | | -}; |
| 6 | +export default createReducer(defaultNotificationsState) |
| 7 | + .handleAction(actions.addInfo, (state, action) => [ |
| 8 | + ...state, |
| 9 | + { |
| 10 | + ...defaultNotification, |
| 11 | + content: action.payload, |
| 12 | + variant: 'info', |
| 13 | + id: Date.now() |
| 14 | + } |
| 15 | + ]) |
| 16 | + .handleAction(actions.addError, (state, action) => [ |
| 17 | + ...state, |
| 18 | + { |
| 19 | + ...defaultNotification, |
| 20 | + content: action.payload, |
| 21 | + variant: 'error', |
| 22 | + id: Date.now() |
| 23 | + } |
| 24 | + ]) |
| 25 | + .handleAction(actions.addSuccess, (state, action) => [ |
| 26 | + ...state, |
| 27 | + { |
| 28 | + ...defaultNotification, |
| 29 | + content: action.payload, |
| 30 | + variant: 'success', |
| 31 | + id: Date.now() |
| 32 | + } |
| 33 | + ]) |
| 34 | + .handleAction(actions.closeNotification, (state, action) => |
| 35 | + state.map(notification => ({ |
| 36 | + ...notification, |
| 37 | + open: action.payload === notification.id ? false : notification.open |
| 38 | + })) |
| 39 | + ); |
0 commit comments