lbry-desktop/ui/js/redux/actions/navigation.js

79 lines
1.8 KiB
JavaScript
Raw Normal View History

import * as types from "constants/action_types";
import {
2017-09-17 20:26:55 +02:00
computePageFromPath,
selectPageTitle,
selectCurrentPage,
selectCurrentParams,
selectHistoryStack,
selectHistoryIndex,
2017-11-13 22:02:23 +01:00
} from "redux/selectors/navigation";
import { doSearch } from "redux/actions/search";
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-11-17 22:27:35 +01:00
const scrollY = options.scrollY;
dispatch({
type: types.HISTORY_NAVIGATE,
2017-11-17 22:27:35 +01:00
data: { url, index: options.index, scrollY },
});
};
}
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 },
});
};
}