-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
203 lines (175 loc) · 5.45 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { combineReducers, createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';
import { connectRouter, routerMiddleware } from 'connected-react-router';
import { api, crashReporter } from '@plone/volto/middleware';
import { matchPath } from 'react-router';
import { settings, addonReducers } from '~/config'; // This needs to come before reducers import
import reducers from '~/reducers';
import routes from '~/routes';
import { createInstance } from '@datapunt/matomo-tracker-react';
let matomo;
if (__CLIENT__) {
matomo = createInstance({
urlBase: 'https://matomo.eea.europa.eu/',
siteId: settings.matomoSiteId, // optional, default value: `1`
// trackerUrl: 'https://LINK.TO.DOMAIN/tracking.php', // optional, default value: `${urlBase}matomo.php`
// srcUrl: 'https://LINK.TO.DOMAIN/tracking.js', // optional, default value: `${urlBase}matomo.js`
});
}
const defaultRoutes = routes[0].routes;
const PREFETCH_ROUTER_LOCATION_CHANGE = 'PREFETCH_ROUTER_LOCATION_CHANGE';
const matchCurrentPath = path => {
// const pathsList = ['/', '/**'];
let alreadyMatched;
for (let pathOption of defaultRoutes) {
const match = matchPath(path, pathOption);
// console.debug('pathOption', alreadyMatched, path, pathOption, match);
if (
match &&
!alreadyMatched &&
(match.path === '/**' || match.path === '/')
) {
return true;
}
if (match) {
alreadyMatched = true;
}
}
};
const precacheContentStart = ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
return next(action);
}
switch (action.type) {
case '@@router/LOCATION_CHANGE':
if (action.payload?.location?.state?.isFromLogin) return next(action);
console.log('action', action.type);
if (!action.payload?.prefetched) {
const path = action.payload.location.pathname;
// TODO: use getBaseUrl based matching
const isGetContent = matchCurrentPath(path);
const expand =
isGetContent && settings.contentExpand?.length
? `&expand=${settings.contentExpand.join(',')}`
: '';
const fullObjects = `${isGetContent ? '?fullobjects' : ''}${expand}`;
const url = `${path}${fullObjects}`;
if (!isGetContent) return next(action);
const prefetchAction = {
type: PREFETCH_ROUTER_LOCATION_CHANGE,
path,
originalAction: action,
request: {
op: 'get',
path: url,
},
};
// console.debug('Start prefetch', url);
return next(prefetchAction);
}
return next(action);
default:
return next(action);
}
};
const precacheContentEnd = ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
return next(action);
}
const type = `${PREFETCH_ROUTER_LOCATION_CHANGE}_SUCCESS`;
if (action.type === type) {
console.log('prefetch action end', action);
return dispatch({
...action.originalAction,
payload: {
...action.originalAction.payload,
prefetched: action.result,
},
});
}
// console.log('precacheContentEnd', action.type);
return next(action);
};
const optimizeProvidersFetch = ({ getState, dispatch }) => next => action => {
if (typeof action === 'function') {
return next(action);
}
if (action.type === 'GET_DATA_FROM_PROVIDER') {
const { data_providers } = getState();
const { path } = action.request;
if (
data_providers.requested.includes(path) ||
Object.keys(data_providers.data).includes(path)
) {
// console.debug(`provider already fetched: ${path}`);
return;
}
return next(action);
}
return next(action);
};
function prefetch(state = {}, action = {}) {
switch (action.type) {
case `@@router/LOCATION_CHANGE`:
// console.debug('action location change', action);
return action.payload?.prefetched
? {
...state,
[action.payload.location.pathname]: action.payload.prefetched,
}
: state;
default:
return state;
}
}
const trackMatomo = ({ getState, dispatch }) => next => action => {
if (typeof action === 'function') {
return next(action);
}
switch (action.type) {
case `@@router/LOCATION_CHANGE`:
if (__CLIENT__ && action.payload?.prefetched) {
const content = action.payload.prefetched;
matomo.trackPageView({
documentTitle: content.title || 'untitled',
href: content['@id']
.replace(settings.apiPath, '')
.replace(settings.internalApiPath, ''),
});
}
break;
default:
}
return next(action);
};
const configureStore = (initialState, history, apiHelper) => {
const defaultMiddleware = [
optimizeProvidersFetch,
precacheContentStart,
routerMiddleware(history),
crashReporter,
thunk,
api(apiHelper),
precacheContentEnd,
];
if (settings.matomoSiteId) {
defaultMiddleware.push(trackMatomo);
}
const middlewares = composeWithDevTools(
applyMiddleware(...defaultMiddleware),
);
// console.log('addonrecuders store.js', addonReducers);
const store = createStore(
combineReducers({
router: connectRouter(history),
...reducers,
...addonReducers,
prefetch,
}),
initialState,
middlewares,
);
return store;
};
export default configureStore;