-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathmessagesReducer.js
82 lines (77 loc) · 2.79 KB
/
messagesReducer.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
import { List, fromJS } from 'immutable';
import { MESSAGE_SENDER, SESSION_NAME } from 'constants';
import {
createButtons,
createNewMessage,
createCarousel,
createVideoSnippet,
createImageSnippet,
createComponentMessage,
storeMessageTo,
getLocalSession
} from './helper';
import * as actionTypes from '../actions/actionTypes';
export default function (storage) {
const initialState = List([]);
return function reducer(state = initialState, action) {
const storeMessage = storeMessageTo(storage);
switch (action.type) {
// Each change to the redux store's message list gets recorded to storage
case actionTypes.ADD_NEW_USER_MESSAGE: {
if (state.size === 0 && action.hidden) {
return state;
}
return storeMessage(
state.push(
createNewMessage(
action.text,
MESSAGE_SENDER.CLIENT,
action.nextMessageIsTooltip,
action.hidden
)
)
);
}
case actionTypes.ADD_NEW_RESPONSE_MESSAGE: {
return storeMessage(state.push(createNewMessage(action.text, MESSAGE_SENDER.RESPONSE)));
}
case actionTypes.ADD_CAROUSEL: {
return storeMessage(state.push(createCarousel(action.carousel, MESSAGE_SENDER.RESPONSE)));
}
case actionTypes.ADD_NEW_VIDEO_VIDREPLY: {
return storeMessage(state.push(createVideoSnippet(action.video, MESSAGE_SENDER.RESPONSE)));
}
case actionTypes.ADD_NEW_IMAGE_IMGREPLY: {
return storeMessage(state.push(createImageSnippet(action.image, MESSAGE_SENDER.RESPONSE)));
}
case actionTypes.ADD_BUTTONS: {
return storeMessage(state.push(createButtons(action.buttons, MESSAGE_SENDER.RESPONSE)));
}
case actionTypes.ADD_COMPONENT_MESSAGE: {
return storeMessage(state.push(createComponentMessage(action.component, action.props, action.showAvatar)));
}
case actionTypes.SET_BUTTONS: {
return storeMessage(state.setIn([action.id, 'chosenReply'], action.title));
}
case actionTypes.INSERT_NEW_USER_MESSAGE: {
return storeMessage(state.insert(action.index, createNewMessage(action.text, MESSAGE_SENDER.CLIENT)));
}
case actionTypes.DROP_MESSAGES: {
return storeMessage(initialState);
}
case actionTypes.SET_CUSTOM_CSS: {
return storeMessage(state.update(state.size - 1, message => message.set('customCss', fromJS(action.customCss))));
}
// Pull conversation from storage, parsing as immutable List
case actionTypes.PULL_SESSION: {
const localSession = getLocalSession(storage, SESSION_NAME);
if (localSession) {
return fromJS(localSession.conversation);
}
return state;
}
default:
return state;
}
};
}