-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathState.js
100 lines (92 loc) · 2.14 KB
/
State.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
97
98
99
100
const initialState = {
user: null,
appointments: [],
perks: [],
defaultPerkImage: '',
};
export default function reducer(state = initialState, action) {
switch (action.type) {
case 'SET_USER':
return {
...state,
user: { id: action.id, ...action.user },
};
case 'ADD_PERK':
return {
...state,
perks: [...state.perks, { id: action.id, ...(action.perk) }]
}
case 'MODIFY_PERK':
{
const updatedPerks = state.perks.map(perk => { if (perk.id == action.id) { perk = { id: action.id, ...action.perk } } return perk });
return Object.assign({}, state, { perks: updatedPerks });
}
case 'DELETE_PERK':
{
const updatedPerks = state.perks.filter(perk => perk.id !== action.id);
return Object.assign({}, state, { perks: updatedPerks });
}
case 'SET_PERK':
const updatedAppointments = state.appointments.map((appointment) => { if (appointment.id == action.appointmentId) { appointment.perk = action.perkId; } return appointment; });
return Object.assign({}, state, { appointments: updatedAppointments });
case 'ADD_APPOINTMENT':
return {
...state,
appointments: [...state.appointments, { id: action.id, ...(action.appointment) }]
}
case 'ADD_DEFAULT_PERK_IMAGE':
return {
...state,
defaultPerkImage: action.url,
}
default:
return state;
}
};
export function setUser(id, user) {
return {
type: 'SET_USER',
id,
user,
};
};
export function addPerk(id, perk) {
return {
type: 'ADD_PERK',
id,
perk,
};
};
export function modifyPerk(id, perk) {
return {
type: 'MODIFY_PERK',
id,
perk,
};
};
export function deletePerk(id) {
return {
type: 'DELETE_PERK',
id,
};
};
export function setPerk(appointmentId, perkId) {
return {
type: 'SET_PERK',
appointmentId,
perkId
};
};
export function addAppointment(id, appointment) {
return {
type: 'ADD_APPOINTMENT',
id,
appointment,
};
};
export function addDefaultPerkImage(url) {
return {
type: 'ADD_DEFAULT_PERK_IMAGE',
url,
};
};