lbry-desktop/src/renderer/redux/actions/navigation.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

import * as ACTIONS from 'constants/action_types';
import { selectHistoryStack, selectHistoryIndex } from 'redux/selectors/navigation';
import { toQueryString } from 'util/query_params';
export function doNavigate(path, params = {}, options = {}) {
return function(dispatch) {
if (!path) {
return;
}
let url = path;
if (params && Object.values(params).length) {
2017-12-13 22:36:30 +01:00
url += `?${toQueryString(params)}`;
}
const { scrollY } = options;
dispatch({
type: ACTIONS.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) {
if (pathAfterAuth) {
dispatch({
type: ACTIONS.CHANGE_AFTER_AUTH_PATH,
data: {
path: `${pathAfterAuth}?${toQueryString(params)}`,
},
});
}
dispatch(doNavigate('/auth'));
};
}
export function doHistoryTraverse(dispatch, state, modifier) {
const stack = selectHistoryStack(state);
const index = selectHistoryIndex(state) + modifier;
if (index >= 0 && index < stack.length) {
const historyItem = stack[index];
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) {
dispatch({
type: ACTIONS.WINDOW_SCROLLED,
data: { scrollY: scroll },
});
};
}