-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventReducer.js
149 lines (140 loc) · 3.99 KB
/
eventReducer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {
LOAD_EVENTS,
LOAD_PARTICIPANTS,
SHOW_EVENT_DETAILS,
HIDE_EVENT_DETAILS,
EVENTS_LOADED,
PARTICIPANTS_LOADED,
CREATING_BOOKING,
BOOKING_CREATED,
CANCELING_PARTICIPANT,
PARTICIPANT_CANCELED
} from "./eventActions";
const modifyEvent = (evs, eventID, update) => {
const newEvents = evs.map(ev => {
if(ev.EventID === eventID) {
return {
...ev,
...update
};
}
else {
return ev;
}
});
return newEvents;
};
function eventReducer(state = {
openEvent: null,
events: null,
eventsLoading: false
}, action){
switch(action.type) {
case LOAD_EVENTS:
return {
...state,
eventsLoading : true
};
case LOAD_PARTICIPANTS: {
const toLoad = action.eventIDs.toDictionary(x => x);
return {
...state,
events: state.events.map(ev => {
if(toLoad[ev.EventID])
{
return {
...ev,
participantsLoading: true
};
} else {
return ev;
}
})
};
}
case SHOW_EVENT_DETAILS:
return {
...state,
openEvent: action.ev
};
case HIDE_EVENT_DETAILS:
return {
...state,
openEvent: null
};
case EVENTS_LOADED:
return {
...state,
eventsLoading: false,
events: action.events
};
case PARTICIPANTS_LOADED: {
const partsLookup = action.participants.toLookup(x => x.EventID);
const eventIDs = action.eventIDs.toDictionary(x => x);
return {
...state,
events: state.events.map(ev => {
if(partsLookup[ev.EventID])
{
return {
...ev,
Participants: partsLookup[ev.EventID],
participantsLoading: false
};
}
else if(eventIDs[ev.EventID]){
return {
...ev,
Participants: [],
participantsLoading: false
};
}
else{
return ev;
}
})
};
}
case CREATING_BOOKING : {
return {
...state,
events: modifyEvent(state.events, action.eventID, (ev) => {
ev.creatingBooking = true;
ev.modifyingBooking = true;
return ev;
})
};
}
case BOOKING_CREATED : {
return {
...state,
events: modifyEvent(state.events, action.eventID, {
creatingBooking: false,
modifyingBooking: false
})
};
}
case CANCELING_PARTICIPANT: {
var newState = {
...state,
events: modifyEvent(state.events, action.eventID, {
cancelingParticipant: true,
modifyingBooking: true
})
};
return newState;
}
case PARTICIPANT_CANCELED: {
return {
...state,
events: modifyEvent(state.events, action.eventID, {
creatingBooking: false,
modifyingBooking: false
})
};
}
default:
return state;
}
}
export default eventReducer;