-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducer.js
96 lines (88 loc) · 2.07 KB
/
reducer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { actionTypes } from "./actions";
export const initialState = {
user: null,
dialog: null,
snackbar: { queue: [], open: false, messageInfo: undefined },
menus: { account: null },
forms: {
review: {
brand: null,
product: null,
date_buy: new Date("2007-01"),
pros: "",
cons: "",
ratings: {},
brand_rating: 0,
brand_pros: "",
brand_cons: ""
}
}
};
export const reducer = (state, action) => {
switch (action.type) {
case actionTypes.SET_USER:
return { ...state, user: action.user };
case actionTypes.SET_DIALOG:
return { ...state, dialog: action.dialog };
case actionTypes.SHOW_SNACKBAR: {
const newQueue = [
...state.snackbar.queue,
{
key: new Date().getTime(),
variant: action.variant,
message: action.message,
actionLabel: action.actionLabel,
action: action.action
}
];
let open = undefined,
messageInfo = undefined;
if (!state.open) {
if (newQueue.length > 0) {
open = true;
messageInfo = newQueue.shift();
}
}
return {
...state,
snackbar: {
open: open ? open : state.open,
messageInfo: messageInfo ? messageInfo : state.open,
queue: newQueue
}
};
}
case actionTypes.SET_SNACKBAR:
return {
...state,
snackbar: {
...state.snackbar,
open: action.open,
messageInfo: action.messageInfo
? action.messageInfo
: state.snackbar.messageInfo
}
};
case actionTypes.FILL_FORM:
return {
...state,
forms: {
...state.forms,
[action.form]: {
...state.forms[action.form],
[action.field]: action.value
}
}
};
case actionTypes.SET_MENU_ANCHOR:
return {
...state,
menus: {
...state.menus,
[action.menu]: action.anchor
}
};
default:
return state;
}
};