2017-08-30 14:48:32 +02:00
|
|
|
import * as types from "constants/action_types";
|
|
|
|
import {
|
2017-09-17 20:26:55 +02:00
|
|
|
computePageFromPath,
|
2017-08-30 14:48:32 +02:00
|
|
|
selectPageTitle,
|
|
|
|
selectCurrentPage,
|
|
|
|
selectCurrentParams,
|
|
|
|
selectHistoryStack,
|
|
|
|
selectHistoryIndex,
|
2017-11-13 22:02:23 +01:00
|
|
|
} from "redux/selectors/navigation";
|
|
|
|
import { doSearch } from "redux/actions/search";
|
2017-08-30 14:48:32 +02:00
|
|
|
import { toQueryString } from "util/query_params";
|
2017-12-12 07:18:25 +01:00
|
|
|
import amplitude from "amplitude-js";
|
2017-08-30 14:48:32 +02:00
|
|
|
|
|
|
|
export function doNavigate(path, params = {}, options = {}) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-17 22:27:35 +01:00
|
|
|
const scrollY = options.scrollY;
|
2017-08-30 14:48:32 +02:00
|
|
|
|
2017-12-12 07:18:25 +01:00
|
|
|
amplitude.getInstance().logEvent("NAVIGATION", { destination: url });
|
|
|
|
|
2017-08-30 14:48:32 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.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 = {}) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
if (pathAfterAuth) {
|
|
|
|
dispatch({
|
|
|
|
type: types.CHANGE_AFTER_AUTH_PATH,
|
|
|
|
data: {
|
|
|
|
path: `${pathAfterAuth}?${toQueryString(params)}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
dispatch(doNavigate("/auth"));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doHistoryTraverse(dispatch, state, modifier) {
|
|
|
|
const stack = selectHistoryStack(state),
|
|
|
|
index = selectHistoryIndex(state) + modifier;
|
|
|
|
|
|
|
|
if (index >= 0 && index < stack.length) {
|
|
|
|
const historyItem = stack[index];
|
|
|
|
return dispatch(
|
|
|
|
doNavigate(historyItem.path, {}, { scrollY: historyItem.scrollY, index })
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doHistoryBack() {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
return doHistoryTraverse(dispatch, getState(), -1);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doHistoryForward() {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
return doHistoryTraverse(dispatch, getState(), 1);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doRecordScroll(scroll) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
dispatch({
|
|
|
|
type: types.WINDOW_SCROLLED,
|
|
|
|
data: { scrollY: scroll },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|