-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathstore.js
134 lines (120 loc) · 4 KB
/
store.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
import { createStore, combineReducers, compose, applyMiddleware } from 'redux';
import { SESSION_NAME } from 'constants';
import behavior from './reducers/behaviorReducer';
import messages from './reducers/messagesReducer';
import metadata from './reducers/metadataReducer';
import { getLocalSession } from './reducers/helper';
import * as actionTypes from './actions/actionTypes';
const cleanURL = (url) => {
const regexProtocolHostPort = /https?:\/\/(([A-Za-z0-9-])+(\.?))+[a-z]+(:[0-9]+)?/;
const regexLastTrailingSlash = /\/$|\/(?=\?)/;
return url.replace(regexProtocolHostPort, '').replace(regexLastTrailingSlash, '');
};
const trimQueryString = (url) => {
const regexQueryString = /\?.+$/;
return url.replace(regexQueryString, '');
};
function initStore(
connectingText,
socket,
storage,
docViewer = false,
onWidgetEvent,
) {
const customMiddleWare = store => next => (action) => {
const localSession = getLocalSession(storage, SESSION_NAME);
let sessionId = localSession
? localSession.session_id
: null;
if (!sessionId && socket.sessionId) {
sessionId = socket.sessionId;
}
const emitMessage = (payload) => {
const emit = () => {
socket.emit(
'user_uttered', {
message: payload,
customData: socket.customData,
session_id: sessionId
}
);
store.dispatch({
type: actionTypes.ADD_NEW_USER_MESSAGE,
text: 'text',
nextMessageIsTooltip: false,
hidden: true
});
};
if (socket.sessionConfirmed) {
emit();
} else {
socket.on('session_confirm', () => {
emit();
});
}
};
switch (action.type) {
case actionTypes.EMIT_NEW_USER_MESSAGE: {
emitMessage(action.text);
break;
}
case actionTypes.GET_OPEN_STATE: {
return store.getState().behavior.get('isChatOpen');
}
case actionTypes.GET_VISIBLE_STATE: {
return store.getState().behavior.get('isChatVisible');
}
case actionTypes.GET_FULLSCREEN_STATE: {
return store.getState().behavior.get('fullScreenMode');
}
case actionTypes.EVAL_URL: {
const pageCallbacks = store.getState().behavior.get('pageChangeCallbacks');
const pageCallbacksJs = pageCallbacks ? pageCallbacks.toJS() : {};
const newUrl = action.url;
if (!pageCallbacksJs.pageChanges) break;
if (store.getState().behavior.get('oldUrl') !== newUrl) {
const { pageChanges, errorIntent } = pageCallbacksJs;
const matched = pageChanges.some((callback) => {
if (callback.regex) {
if (newUrl.match(callback.url)) {
emitMessage(callback.callbackIntent);
return true;
}
} else {
let cleanCurrentUrl = cleanURL(newUrl);
let cleanCallBackUrl = cleanURL(callback.url);
if (!cleanCallBackUrl.match(/\?.+$/)) { // the callback does not have a querystring
cleanCurrentUrl = trimQueryString(cleanCurrentUrl);
cleanCallBackUrl = trimQueryString(cleanCallBackUrl);
}
if (cleanCurrentUrl === cleanCallBackUrl) {
emitMessage(callback.callbackIntent);
return true;
}
return false;
}
});
if (!matched) emitMessage(errorIntent);
}
break;
}
default: {
break;
}
}
// console.log('Middleware triggered:', action);
next(action);
};
const reducer = combineReducers({
behavior: behavior(connectingText, storage, docViewer, onWidgetEvent),
messages: messages(storage),
metadata: metadata(storage)
});
// eslint-disable-next-line no-underscore-dangle
const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
return createStore(
reducer,
composeEnhancer(applyMiddleware(customMiddleWare)),
);
}
export { initStore };