79 lines
2.2 KiB
JavaScript
79 lines
2.2 KiB
JavaScript
import Constants from 'constants'; // eslint-disable-line node/no-deprecated-api
|
|
|
|
const reducers = {};
|
|
const defaultState = {
|
|
stack: [{ route: Constants.DRAWER_ROUTE_DISCOVER, params: {} }], // Discover is always the first drawer route
|
|
playerVisible: false,
|
|
currentRoute: null,
|
|
};
|
|
|
|
reducers[Constants.ACTION_SET_PLAYER_VISIBLE] = (state, action) =>
|
|
Object.assign({}, state, {
|
|
playerVisible: action.data.visible,
|
|
});
|
|
|
|
reducers[Constants.ACTION_PUSH_DRAWER_STACK] = (state, action) => {
|
|
const { routeName, params } = action.data;
|
|
const newStack = state.stack.slice();
|
|
|
|
const lastRoute = newStack[newStack.length - 1].route;
|
|
let canPushStack = routeName !== lastRoute;
|
|
if (
|
|
lastRoute === Constants.DRAWER_ROUTE_CHANNEL_CREATOR_FORM &&
|
|
routeName === Constants.DRAWER_ROUTE_CHANNEL_CREATOR
|
|
) {
|
|
canPushStack = false;
|
|
}
|
|
if (lastRoute === Constants.DRAWER_ROUTE_PUBLISH_FORM && routeName === Constants.DRAWER_ROUTE_PUBLISH) {
|
|
canPushStack = false;
|
|
}
|
|
|
|
if (canPushStack) {
|
|
newStack.push({ route: routeName, params });
|
|
}
|
|
|
|
return {
|
|
...state,
|
|
stack: newStack,
|
|
};
|
|
};
|
|
|
|
reducers[Constants.ACTION_POP_DRAWER_STACK] = (state, action) => {
|
|
// We don't want to pop the Discover route, since it's always expected to be the first
|
|
const newStack = state.stack.length === 1 ? state.stack.slice() : state.stack.slice(0, state.stack.length - 1);
|
|
|
|
return {
|
|
...state,
|
|
stack: newStack,
|
|
};
|
|
};
|
|
|
|
// TODO: The ACTION_REACT_NAVIGATION_*** reducers are a workaround for the react
|
|
// navigation event listeners (willFocus, didFocus, etc) not working with the
|
|
// react-navigation-redux-helpers package.
|
|
reducers[Constants.ACTION_REACT_NAVGIATION_RESET] = (state, action) => {
|
|
return {
|
|
...state,
|
|
currentRoute: Constants.DRAWER_ROUTE_DISCOVER, // default to Discover upon reset
|
|
};
|
|
};
|
|
|
|
reducers[Constants.ACTION_REACT_NAVIGATION_NAVIGATE] = (state, action) => {
|
|
return {
|
|
...state,
|
|
currentRoute: action.routeName,
|
|
};
|
|
};
|
|
|
|
reducers[Constants.ACTION_REACT_NAVIGATION_REPLACE] = (state, action) => {
|
|
return {
|
|
...state,
|
|
currentRoute: action.routeName,
|
|
};
|
|
};
|
|
|
|
export default function reducer(state = defaultState, action) {
|
|
const handler = reducers[action.type];
|
|
if (handler) return handler(state, action);
|
|
return state;
|
|
}
|