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";
|
|
|
|
|
|
|
|
export function doNavigate(path, params = {}, options = {}) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
if (!path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let url = path;
|
|
|
|
if (params && Object.values(params).length) {
|
|
|
|
url += "?" + toQueryString(params);
|
|
|
|
}
|
|
|
|
|
2017-09-17 20:26:55 +02:00
|
|
|
const state = getState(),
|
|
|
|
currentPage = selectCurrentPage(state),
|
|
|
|
nextPage = computePageFromPath(path),
|
|
|
|
scrollY = options.scrollY;
|
2017-08-30 14:48:32 +02:00
|
|
|
|
2017-09-17 20:26:55 +02:00
|
|
|
if (currentPage != nextPage) {
|
|
|
|
//I wasn't seeing it scroll to the proper position without this -- possibly because the page isn't fully rendered? Not sure - Jeremy
|
|
|
|
setTimeout(() => {
|
|
|
|
window.scrollTo(0, scrollY ? scrollY : 0);
|
|
|
|
}, 100);
|
|
|
|
}
|
2017-08-30 14:48:32 +02:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.HISTORY_NAVIGATE,
|
|
|
|
data: { url, index: options.index },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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 },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|