2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2017-12-28 00:48:11 +01:00
|
|
|
import { selectHistoryIndex, selectHistoryStack } from 'redux/selectors/navigation';
|
2017-12-21 18:32:51 +01:00
|
|
|
import { toQueryString } from 'util/query_params';
|
2018-02-16 09:47:52 +01:00
|
|
|
import analytics from 'analytics';
|
2017-08-30 14:48:32 +02:00
|
|
|
|
|
|
|
export function doNavigate(path, params = {}, options = {}) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-08-30 14:48:32 +02:00
|
|
|
if (!path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let url = path;
|
|
|
|
if (params && Object.values(params).length) {
|
2017-12-13 22:36:30 +01:00
|
|
|
url += `?${toQueryString(params)}`;
|
2017-08-30 14:48:32 +02:00
|
|
|
}
|
|
|
|
|
2018-02-16 09:47:52 +01:00
|
|
|
analytics.track('NAVIGATION', { destination: url });
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
const { scrollY } = options;
|
2017-08-30 14:48:32 +02:00
|
|
|
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.HISTORY_NAVIGATE,
|
2017-11-17 22:27:35 +01:00
|
|
|
data: { url, index: options.index, scrollY },
|
2017-08-30 14:48:32 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doAuthNavigate(pathAfterAuth = null, params = {}) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-08-30 14:48:32 +02:00
|
|
|
if (pathAfterAuth) {
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.CHANGE_AFTER_AUTH_PATH,
|
2017-08-30 14:48:32 +02:00
|
|
|
data: {
|
|
|
|
path: `${pathAfterAuth}?${toQueryString(params)}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch(doNavigate('/auth'));
|
2017-08-30 14:48:32 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doHistoryTraverse(dispatch, state, modifier) {
|
2017-12-21 18:32:51 +01:00
|
|
|
const stack = selectHistoryStack(state);
|
|
|
|
const index = selectHistoryIndex(state) + modifier;
|
2017-08-30 14:48:32 +02:00
|
|
|
|
|
|
|
if (index >= 0 && index < stack.length) {
|
|
|
|
const historyItem = stack[index];
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch(doNavigate(historyItem.path, {}, { scrollY: historyItem.scrollY, index }));
|
2017-08-30 14:48:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doHistoryBack() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return (dispatch, getState) => doHistoryTraverse(dispatch, getState(), -1);
|
2017-08-30 14:48:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doHistoryForward() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return (dispatch, getState) => doHistoryTraverse(dispatch, getState(), 1);
|
2017-08-30 14:48:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doRecordScroll(scroll) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-08-30 14:48:32 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.WINDOW_SCROLLED,
|
2017-08-30 14:48:32 +02:00
|
|
|
data: { scrollY: scroll },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|