Instantly share code, notes, and snippets.
Last active
May 24, 2017 10:05
-
Star
0
(0)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save hieuhani/01ed66beff0309ff4113cb9505afacef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fromJS, List } from 'immutable'; | |
import camelize from 'camelize'; | |
import { | |
GET_RESTAURANT_MENUS_REQUEST, | |
GET_RESTAURANT_MENUS_SUCCESS, | |
GET_RESTAURANT_MENUS_ERROR, | |
SET_ACTIVE_MENU_CONTAINER_ID, | |
SET_ACTIVE_MENU_ITEM_ID, | |
PUT_UPDATE_MENU_CONTAINER_REQUEST, | |
PUT_UPDATE_MENU_CONTAINER_SUCCESS, | |
PUT_UPDATE_MENU_CONTAINER_ERROR, | |
DELETE_MENU_CONTAINER_REQUEST, | |
DELETE_MENU_CONTAINER_SUCCESS, | |
DELETE_MENU_CONTAINER_ERROR, | |
} from './constants'; | |
const initialState = fromJS({ | |
gettingMenus: false, | |
updatingMenu: false, | |
deletingMenu: false, | |
menus: [], | |
}); | |
function menusReducer(state = initialState, action) { | |
const { type, payload } = action; | |
switch (type) { | |
case GET_RESTAURANT_MENUS_REQUEST: | |
return state | |
.set('gettingMenus', true); | |
case GET_RESTAURANT_MENUS_SUCCESS: { | |
let restaurantMenus = fromJS(camelize(payload.result)); | |
if (restaurantMenus.size > 0) { | |
restaurantMenus = restaurantMenus.map((menu) => { | |
let menuItems = menu.get('menuItems', List()); | |
if (menuItems.size > 0) { | |
menuItems = menuItems.set(0, menuItems.get(0).set('selected', true)); | |
} | |
return menu.set('menuItems', menuItems); | |
}); | |
restaurantMenus = restaurantMenus.set(0, restaurantMenus.first().set('selected', true)); | |
} | |
return state | |
.set('gettingMenus', false) | |
.set('menus', restaurantMenus); | |
} | |
case SET_ACTIVE_MENU_CONTAINER_ID: | |
return state.set('menus', state.get('menus').map((menu) => { | |
if (menu.get('id') === payload.menuID) { | |
return menu.set('selected', true); | |
} | |
if (menu.get('containersLength') > 0) { | |
let hasSelectedChild = false; | |
const menuContainers = menu.get('menuContainers').map((subMenu) => { | |
if (subMenu.get('id') === payload.menuID) { | |
hasSelectedChild = true; | |
return subMenu.set('selected', true); | |
} | |
return subMenu.set('selected', false); | |
}); | |
return menu | |
.set('menuContainers', menuContainers) | |
.set('selected', false) | |
.set('hasSelectedChild', hasSelectedChild); | |
} | |
return menu | |
.set('selected', false).set('hasSelectedChild', false); | |
})); | |
case SET_ACTIVE_MENU_ITEM_ID: { | |
let currentMenus = state.get('menus'); | |
currentMenus = currentMenus.map((menu) => { | |
if (menu.get('id') === payload.menuID) { | |
let menuItems = menu.get('menuItems', List()); | |
if (menuItems.size > 0) { | |
menuItems = menuItems.map((menuItem) => (menuItem.set('selected', menuItem.get('id') === payload.menuItemID))); | |
return menu.set('menuItems', menuItems); | |
} | |
return menu; | |
} | |
if (menu.get('containersLength') > 0) { | |
const subMenus = menu.get('menuContainers').map((subMenu) => { | |
let menuItems = subMenu.get('menuItems', List()); | |
if (menuItems.size > 0) { | |
menuItems = menuItems.map((menuItem) => (menuItem.set('selected', menuItem.get('id') === payload.menuItemID))); | |
return subMenu.set('menuItems', menuItems); | |
} | |
return subMenu; | |
}); | |
return menu.set('menuContainers', subMenus); | |
} | |
return menu; | |
}); | |
return state.set('menus', currentMenus); | |
} | |
case GET_RESTAURANT_MENUS_ERROR: | |
return state | |
.set('gettingMenus', false); | |
case PUT_UPDATE_MENU_CONTAINER_REQUEST: | |
return state | |
.set('updatingMenu', true); | |
case PUT_UPDATE_MENU_CONTAINER_SUCCESS: | |
return state | |
.set('updatingMenu', false) | |
.set('menus', state.get('menus').map((menu) => { | |
if (menu.get('id') === payload.id) { | |
return fromJS(camelize(payload)); | |
} else if (menu.get('menuContainers').size > 0) { | |
return menu.set('menuContainers', menu.get('menuContainers').map((subMenu) => { | |
if (subMenu.get('id') === payload.id) { | |
return fromJS(camelize(payload)); | |
} | |
return subMenu; | |
})); | |
} | |
return menu; | |
})); | |
case PUT_UPDATE_MENU_CONTAINER_ERROR: | |
return state | |
.set('updatingMenu', false); | |
case DELETE_MENU_CONTAINER_REQUEST: | |
return state | |
.set('deletingMenu', true); | |
case DELETE_MENU_CONTAINER_SUCCESS: // TODO: Need to support delete sub menu | |
return state | |
.set('deletingMenu', false) | |
.set('menus', state.get('menus').filter((menu) => (menu.get('id') !== payload.menuContainerID))); | |
case DELETE_MENU_CONTAINER_ERROR: | |
return state | |
.set('deletingMenu', false); | |
default: | |
return state; | |
} | |
} | |
export default menusReducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment