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

73 lines
1.7 KiB
JavaScript
Raw Normal View History

import * as ACTIONS from 'constants/action_types';
const getCurrentPath = () => {
const { hash } = document.location;
if (hash !== '') return hash.replace(/^#/, '');
2018-05-25 16:44:48 +02:00
return '/discover';
};
const reducers = {};
const defaultState = {
currentPath: getCurrentPath(),
pathAfterAuth: '/discover',
index: 0,
stack: [],
};
reducers[ACTIONS.DAEMON_READY] = state => {
const { currentPath } = state;
return Object.assign({}, state, {
stack: [{ path: currentPath, scrollY: 0 }],
});
};
reducers[ACTIONS.CHANGE_AFTER_AUTH_PATH] = (state, action) =>
Object.assign({}, state, {
pathAfterAuth: action.data.path,
});
reducers[ACTIONS.HISTORY_NAVIGATE] = (state, action) => {
const { stack, index } = state;
const { url: path, index: newIndex } = action.data;
2017-12-13 22:36:30 +01:00
const newState = {
2017-09-17 20:26:55 +02:00
currentPath: path,
};
2017-11-17 22:27:35 +01:00
if (newIndex >= 0) {
newState.index = newIndex;
} else if (!stack[index] || stack[index].path !== path) {
2017-09-17 20:26:55 +02:00
// ^ Check for duplicated
newState.stack = [...stack.slice(0, index + 1), { path, scrollY: 0 }];
newState.index = newState.stack.length - 1;
}
return Object.assign({}, state, newState);
};
reducers[ACTIONS.WINDOW_SCROLLED] = (state, action) => {
const { index } = state;
const { scrollY } = action.data;
return Object.assign({}, state, {
stack: state.stack.map((stackItem, itemIndex) => {
if (itemIndex !== index) {
return stackItem;
}
return {
...stackItem,
2017-12-13 22:36:30 +01:00
scrollY,
};
}),
});
};
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
2017-12-05 09:04:00 +01:00
if (handler) {
2017-12-12 07:18:25 +01:00
return handler(state, action);
2017-12-05 09:04:00 +01:00
}
return state;
}