-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: logic for nav section instances processing
- Loading branch information
1 parent
991b0d9
commit d8655a9
Showing
7 changed files
with
134 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import {NavSectionInstance, NavSectionItemInstance} from '@models/navsection'; | ||
import store from '@redux/store'; | ||
import {shallowEqual} from 'react-redux'; | ||
import {updateNavSectionState} from '@redux/reducers/navSection'; | ||
import navSectionMap from './navSectionMap'; | ||
|
||
store.subscribe(() => { | ||
const state = store.getState(); | ||
const navSectionInstances: NavSectionInstance[] = []; | ||
const navSectionItemInstances: NavSectionItemInstance[] = []; | ||
const nextScopeMap: Record<string, Record<string, any>> = {}; | ||
|
||
navSectionMap.getAll().forEach(navSection => { | ||
const previousScope = state.navSection.scopeMap[navSection.id]; | ||
const nextScope = navSection.getScope(state); | ||
if (shallowEqual(previousScope, nextScope)) { | ||
return; | ||
} | ||
nextScopeMap[navSection.id] = nextScope; | ||
const itemHandler = navSection.itemHandler; | ||
let itemInstances: NavSectionItemInstance[] | undefined; | ||
let rawItems: any[] = []; | ||
if (itemHandler) { | ||
rawItems = (navSection.getItems && navSection.getItems(nextScope)) || []; | ||
itemInstances = rawItems?.map(rawItem => { | ||
return { | ||
name: itemHandler.getName(rawItem), | ||
id: itemHandler.getIdentifier(rawItem), | ||
isSelected: Boolean(itemHandler.isSelected && itemHandler.isSelected(rawItem, nextScope)), | ||
isHighlighted: Boolean(itemHandler.isHighlighted && itemHandler.isHighlighted(rawItem, nextScope)), | ||
isVisible: Boolean(itemHandler.isVisible && itemHandler.isVisible(rawItem, nextScope)), | ||
isDirty: Boolean(itemHandler.isDirty && itemHandler.isDirty(rawItem, nextScope)), | ||
isDisabled: Boolean(itemHandler.isDisabled && itemHandler.isDisabled(rawItem, nextScope)), | ||
shouldScrollIntoView: Boolean( | ||
itemHandler.shouldScrollIntoView && itemHandler.shouldScrollIntoView(rawItem, nextScope) | ||
), | ||
}; | ||
}); | ||
} | ||
const navSectionInstance: NavSectionInstance = { | ||
name: navSection.name, | ||
subsectionNames: navSection.subsectionNames, | ||
itemIds: itemInstances?.map(i => i.id), | ||
isLoading: Boolean(navSection.isLoading && navSection.isLoading(nextScope, rawItems)), | ||
isVisible: Boolean(navSection.isVisible && navSection.isVisible(nextScope, rawItems)), | ||
isInitialized: Boolean(navSection.isInitialized && navSection.isInitialized(nextScope, rawItems)), | ||
}; | ||
navSectionInstances.push(navSectionInstance); | ||
if (itemInstances) { | ||
navSectionItemInstances.push(...itemInstances); | ||
} | ||
}); | ||
|
||
// TODO: compute everything from useNavSection hook | ||
|
||
const updatedNavSectionItemInstances = navSectionItemInstances.filter(nextItemInstance => { | ||
return !shallowEqual(state.navSection.itemInstanceMap[nextItemInstance.id], nextItemInstance); | ||
}); | ||
|
||
const updatedNavSectionInstances = navSectionInstances.filter(nextInstance => { | ||
return !shallowEqual(state.navSection.instanceMap[nextInstance.name], nextInstance); | ||
}); | ||
|
||
store.dispatch( | ||
updateNavSectionState({ | ||
instances: updatedNavSectionInstances, | ||
itemInstances: updatedNavSectionItemInstances, | ||
scopeMap: nextScopeMap, | ||
}) | ||
); | ||
}); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {createSlice, Draft, PayloadAction} from '@reduxjs/toolkit'; | ||
import initialState from '@redux/initialState'; | ||
import {NavSectionState, UpdateNavSectionStatePayload} from '@models/navsection'; | ||
|
||
export const navSectionSlice = createSlice({ | ||
name: 'navSection', | ||
initialState: initialState.navSection, | ||
reducers: { | ||
updateNavSectionState: (state: Draft<NavSectionState>, action: PayloadAction<UpdateNavSectionStatePayload>) => { | ||
const {instances, itemInstances, scopeMap} = action.payload; | ||
instances.forEach(instance => { | ||
state.instanceMap[instance.name] = instance; | ||
}); | ||
itemInstances.forEach(itemInstance => { | ||
state.itemInstanceMap[itemInstance.id]; | ||
}); | ||
state.scopeMap = scopeMap; | ||
}, | ||
}, | ||
}); | ||
|
||
export const {updateNavSectionState} = navSectionSlice.actions; | ||
export default navSectionSlice.reducer; |
This file contains 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