/
TimeLineStore.js
150 lines (128 loc) · 3.45 KB
/
TimeLineStore.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
150
import moment from 'moment';
import {
TIMELINE_GROUPS_CHANGED,
TIMELINE_ITEMS_CHANGED,
MODAL_STATE_CHANGED
} from './';
const BASE_URL = 'http://localhost:3005';
export default function() {
let _observers = [];
let _data = {};
const subscribe = observer => {
_observers.push(observer);
};
const unsubscribe = observer => {
_observers = _observers.filter(item => item !== observer);
};
const isSubscribed = observer => {
return _observers.includes(observer);
};
const setState = merge => {
let old = {};
for (let changedItemKey in merge.payload) {
if (_data.hasOwnProperty(changedItemKey)) {
old[changedItemKey] = merge.payload[changedItemKey];
}
_data[changedItemKey] = merge.payload[changedItemKey];
}
_observers.forEach(observer => observer(merge, old));
};
const getState = () => {
return _data;
};
// Normal methodes will be changing the state
const handleToggleModal = () => {
const currentModalState = _data.isModalOpen || false;
setState({
type: MODAL_STATE_CHANGED,
payload: {
isModalOpen: !currentModalState
}
});
};
const getTimelineItems = () => {
fetch(`${BASE_URL}/api/timeline`)
.then(res => res.json())
.then(resJson => {
const groups = _extractGroups(resJson);
const items = _extractItems(resJson, groups);
setState({
type: TIMELINE_GROUPS_CHANGED,
payload: {
groups: groups
}
});
setState({
type: TIMELINE_ITEMS_CHANGED,
payload: {
items: items
}
});
});
};
const handleAddClass = (className, startingDate) => {
const date = new Date(startingDate);
const body = {
group_name: className,
starting_date: date.toISOString()
};
console.log(body);
fetch(`${BASE_URL}/api/groups`, {
method: 'POST',
headers: {
'Conetent-Type': 'application/json'
},
body: JSON.stringify(body)
}).then(res => {
console.log(res);
handleToggleModal();
});
};
// Helper methods
const _extractGroups = data => {
const allGroups = Object.keys(data).map(item => {
return {
id: item
};
});
return allGroups;
};
const _extractItems = (data, groups) => {
let allItems = [];
groups.forEach(group => {
const items = data[group.id];
items.sort((a, b) => a.position - b.position); // make sure it is sorted
let lastDate = ''; // will be overwritten by each module in a row
items.map(item => {
if (lastDate === '') lastDate = item.starting_date;
item.starting_date = lastDate;
item.ending_date = moment(lastDate).add(item.duration, 'weeks');
lastDate = moment(item.ending_date);
return item;
});
allItems = [...items, ...allItems];
});
return allItems.map(item => {
return {
start: moment(item.starting_date).format('YYYY-MM-DD'),
end: moment(item.ending_date).format('YYYY-MM-DD'),
content: item.module_name,
group: item.group_name,
group_id: item.id,
running_module_id: item.running_module_id,
className: item.module_name.split(' ').join('_'),
git_repo: item.git_repo
};
});
};
return {
subscribe,
unsubscribe,
isSubscribed,
getState,
setState,
getTimelineItems,
handleToggleModal,
handleAddClass
};
}