UI related states: manage disable scrolling

This commit is contained in:
Vesa Luusua 2017-07-04 21:26:29 +03:00
parent 2f546c50ed
commit e35eda5c71
2 changed files with 55 additions and 1 deletions

53
src/ducks/UI.duck.js Normal file
View file

@ -0,0 +1,53 @@
// ================ Action types ================ //
export const DISABLE_SCROLLING = 'app/UI/DISABLE_SCROLLING';
// ================ Reducer ================ //
const initialState = {
disableScrollRequests: [],
};
export default function reducer(state = initialState, action = {}) {
const { type, payload } = action;
switch (type) {
case DISABLE_SCROLLING: {
const { componentId, disableScrolling } = payload;
const disableScrollRequests = state.disableScrollRequests;
const componentIdExists = disableScrollRequests.find(c => c.componentId === componentId);
if (componentIdExists) {
const disableScrollRequestArray = disableScrollRequests.map(c => {
return c.componentId === componentId ? { ...c, disableScrolling } : c;
});
return { ...state, disableScrollRequests: [...disableScrollRequestArray] };
}
const disableScrollRequestArray = [
...disableScrollRequests,
{ componentId, disableScrolling },
];
return {
...state,
disableScrollRequests: disableScrollRequestArray,
};
}
default:
return state;
}
}
// ================ Action creators ================ //
export const manageDisableScrolling = (componentId, disableScrolling) => ({
type: DISABLE_SCROLLING,
payload: { componentId, disableScrolling },
});
// ================ Selectors ================ //
export const isScrollingDisabled = state => {
const { disableScrollRequests } = state.UI;
return disableScrollRequests.some(r => r.disableScrolling);
};

View file

@ -8,7 +8,8 @@ import { reducer as form } from 'redux-form';
import Auth from './Auth.duck';
import FlashNotification from './FlashNotification.duck';
import LocationFilter from './LocationFilter.duck';
import UI from './UI.duck';
import marketplaceData from './marketplaceData.duck';
import user from './user.duck';
export { form, Auth, FlashNotification, LocationFilter, marketplaceData, user };
export { form, Auth, FlashNotification, LocationFilter, UI, marketplaceData, user };