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
|
import { fromJS, List, Map } from 'immutable';
import notif from 'ba-api/notifMessage';
import {
FETCH_CALENDAR_DATA,
ADD_EVENT,
DISCARD_EVENT,
SUBMIT_EVENT,
DELETE_EVENT,
CLOSE_NOTIF
} from 'ba-actions/actionTypes';
const initialState = {
events: List([]),
openFrm: false,
formValues: Map(),
notifMsg: '',
};
const initForm = Map({
title: '',
start: new Date(),
end: new Date(),
hexColor: 'F8BBD0',
});
const initialImmutableState = fromJS(initialState);
export default function reducer(state = initialImmutableState, action = {}) {
switch (action.type) {
case FETCH_CALENDAR_DATA:
return state.withMutations((mutableState) => {
const items = fromJS(action.items);
mutableState.set('events', items);
});
case ADD_EVENT:
return state.withMutations((mutableState) => {
mutableState
.set('openFrm', true)
.set('formValues', initForm);
});
case DISCARD_EVENT:
return state.withMutations((mutableState) => {
mutableState
.set('openFrm', false)
.set('formValues', Map())
.set('notifMsg', notif.discard);
});
case SUBMIT_EVENT:
return state.withMutations((mutableState) => {
const initItem = Map(action.newEvent);
const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
const newItem = initItem
.update('id', (val = id) => val)
.set('start', action.newEvent.get('start')._d || action.newEvent.get('start'))
.set('end', action.newEvent.get('end')._d || action.newEvent.get('end'));
mutableState.update('events', events => events.push(newItem));
mutableState
.set('formValues', Map())
.set('openFrm', false)
.set('notifMsg', notif.saved);
});
case DELETE_EVENT:
return state.withMutations((mutableState) => {
const eventItem = state.get('events')
.find(obj => (
obj.get('id') === action.event.id
));
const index = state.get('events').indexOf(eventItem);
mutableState
.update('events', events => events.splice(index, 1))
.set('notifMsg', notif.removed);
});
case CLOSE_NOTIF:
return state.withMutations((mutableState) => {
mutableState.set('notifMsg', '');
});
default:
return state;
}
}
|