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
|
import { fromJS, List, Map } from 'immutable';
import notif from 'ba-api/notifMessage';
import dummy from 'ba-api/dummyContents';
import {
FETCH_TIMELINE_DATA,
POST,
TOGGLE_LIKE,
FETCH_COMMENT_DATA,
POST_COMMENT,
CLOSE_NOTIF
} from 'ba-actions/actionTypes';
import { getDate, getTime } from '../helpers/dateTimeHelper';
const initialState = {
dataTimeline: List([]),
commentIndex: 0,
notifMsg: '',
};
const icon = privacyType => {
switch (privacyType) {
case 'public':
return 'language';
case 'friends':
return 'people';
default:
return 'lock';
}
};
const buildTimeline = (text, image, privacy) => {
const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
const imageSrc = image !== undefined ? URL.createObjectURL(image[0]) : '';
return Map({
id,
name: 'John Doe',
date: getDate(),
time: getTime(),
icon: icon(privacy),
avatar: dummy.user.avatar,
image: imageSrc,
content: text,
liked: false,
comments: List([])
});
};
const buildComment = (message, curData) => {
const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
const newData = Map({
id,
from: 'John Doe',
avatar: dummy.user.avatar,
date: getDate(),
message,
});
return curData.push(newData);
};
const initialImmutableState = fromJS(initialState);
export default function reducer(state = initialImmutableState, action = {}) {
switch (action.type) {
case FETCH_TIMELINE_DATA:
return state.withMutations((mutableState) => {
const items = fromJS(action.items);
mutableState.set('dataTimeline', items);
});
case POST:
return state.withMutations((mutableState) => {
mutableState
.update(
'dataTimeline',
dataTimeline => dataTimeline.unshift(
buildTimeline(action.text, action.media, action.privacy)
)
)
.set('notifMsg', notif.posted);
});
case TOGGLE_LIKE:
return state.withMutations((mutableState) => {
const index = state.get('dataTimeline').indexOf(action.item);
mutableState.update('dataTimeline', dataTimeline => dataTimeline
.setIn([index, 'liked'], !state.getIn(['dataTimeline', index, 'liked']))
);
});
case FETCH_COMMENT_DATA:
return state.withMutations((mutableState) => {
const index = state.get('dataTimeline').indexOf(action.item);
mutableState.set('commentIndex', index);
});
case POST_COMMENT:
return state.withMutations((mutableState) => {
mutableState
.update('dataTimeline',
dataTimeline => dataTimeline.setIn(
[state.get('commentIndex'), 'comments'],
buildComment(action.comment, state.getIn(['dataTimeline', state.get('commentIndex'), 'comments']))
)
)
.set('notifMsg', notif.commented);
});
case CLOSE_NOTIF:
return state.withMutations((mutableState) => {
mutableState.set('notifMsg', '');
});
default:
return state;
}
}
|