2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2017-08-30 14:48:32 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
const getCurrentPath = () => {
|
|
|
|
const { hash } = document.location;
|
|
|
|
if (hash !== '') return hash.replace(/^#/, '');
|
2018-05-25 16:44:48 +02:00
|
|
|
return '/discover';
|
2017-08-30 14:48:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const reducers = {};
|
|
|
|
const defaultState = {
|
2017-12-21 18:32:51 +01:00
|
|
|
currentPath: getCurrentPath(),
|
|
|
|
pathAfterAuth: '/discover',
|
2017-08-30 14:48:32 +02:00
|
|
|
index: 0,
|
|
|
|
stack: [],
|
|
|
|
};
|
|
|
|
|
2017-12-28 00:48:11 +01:00
|
|
|
reducers[ACTIONS.DAEMON_READY] = state => {
|
2017-08-30 14:48:32 +02:00
|
|
|
const { currentPath } = state;
|
|
|
|
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
stack: [{ path: currentPath, scrollY: 0 }],
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-12-28 00:48:11 +01:00
|
|
|
reducers[ACTIONS.CHANGE_AFTER_AUTH_PATH] = (state, action) =>
|
|
|
|
Object.assign({}, state, {
|
2017-08-30 14:48:32 +02:00
|
|
|
pathAfterAuth: action.data.path,
|
|
|
|
});
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
reducers[ACTIONS.HISTORY_NAVIGATE] = (state, action) => {
|
2017-08-30 14:48:32 +02:00
|
|
|
const { stack, index } = state;
|
2017-12-21 18:32:51 +01:00
|
|
|
const { url: path, index: newIndex } = action.data;
|
2017-08-30 14:48:32 +02:00
|
|
|
|
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;
|
2017-08-31 14:59:29 +02:00
|
|
|
} else if (!stack[index] || stack[index].path !== path) {
|
2017-09-17 20:26:55 +02:00
|
|
|
// ^ Check for duplicated
|
2017-08-30 14:48:32 +02:00
|
|
|
newState.stack = [...stack.slice(0, index + 1), { path, scrollY: 0 }];
|
|
|
|
newState.index = newState.stack.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.assign({}, state, newState);
|
|
|
|
};
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
reducers[ACTIONS.WINDOW_SCROLLED] = (state, action) => {
|
|
|
|
const { index } = state;
|
2017-08-30 14:48:32 +02:00
|
|
|
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,
|
2017-08-30 14:48:32 +02:00
|
|
|
};
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-08-30 14:48:32 +02:00
|
|
|
return state;
|
|
|
|
}
|