diff --git a/web/redux/community-picker-reducer.js b/web/redux/community-picker-reducer.js new file mode 100644 --- /dev/null +++ b/web/redux/community-picker-reducer.js @@ -0,0 +1,61 @@ +// @flow + +import { nonThreadCalendarFilters } from 'lib/selectors/calendar-filter-selectors.js'; +import { + type CalendarFilter, + calendarThreadFilterTypes, +} from 'lib/types/filter-types.js'; +import type { ThreadStore } from 'lib/types/thread-types.js'; + +import { + updateCalendarCommunityFilter, + clearCalendarCommunityFilter, +} from './action-types.js'; +import type { Action, CommunityPickerStore } from './redux-setup'; +import { filterThreadIDsBelongingToCommunity } from '../selectors/calendar-selectors.js'; + +export type ReduceCommunityPickerStoreResult = { + +communityPickerStore: CommunityPickerStore, + +calendarFilters: $ReadOnlyArray, +}; + +export function reduceCommunityPickerStore( + communityPickerStore: CommunityPickerStore, + calendarFilters: $ReadOnlyArray, + threadStore: ThreadStore, + action: Action, +): ReduceCommunityPickerStoreResult { + if (action.type === updateCalendarCommunityFilter) { + const nonThreadFilters = nonThreadCalendarFilters(calendarFilters); + + const threadIDs = Array.from( + filterThreadIDsBelongingToCommunity( + action.payload, + threadStore.threadInfos, + ), + ); + return { + calendarFilters: [ + ...nonThreadFilters, + { + type: calendarThreadFilterTypes.THREAD_LIST, + threadIDs, + }, + ], + communityPickerStore: { + ...communityPickerStore, + calendar: action.payload, + }, + }; + } else if (action.type === clearCalendarCommunityFilter) { + const nonThreadFilters = nonThreadCalendarFilters(calendarFilters); + return { + calendarFilters: nonThreadFilters, + communityPickerStore: { + ...communityPickerStore, + calendar: null, + }, + }; + } + return { communityPickerStore, calendarFilters }; +} diff --git a/web/redux/redux-setup.js b/web/redux/redux-setup.js --- a/web/redux/redux-setup.js +++ b/web/redux/redux-setup.js @@ -8,7 +8,6 @@ deleteAccountActionTypes, } from 'lib/actions/user-actions.js'; import baseReducer from 'lib/reducers/master-reducer.js'; -import { nonThreadCalendarFilters } from 'lib/selectors/calendar-filter-selectors.js'; import { mostRecentlyReadThreadSelector } from 'lib/selectors/thread-selectors.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { invalidSessionDowngrade } from 'lib/shared/account-utils.js'; @@ -21,10 +20,7 @@ import type { DraftStore } from 'lib/types/draft-types.js'; import type { EnabledApps } from 'lib/types/enabled-apps.js'; import type { EntryStore } from 'lib/types/entry-types.js'; -import { - type CalendarFilter, - calendarThreadFilterTypes, -} from 'lib/types/filter-types.js'; +import { type CalendarFilter } from 'lib/types/filter-types.js'; import type { LifecycleState } from 'lib/types/lifecycle-state-types.js'; import type { LoadingStatus } from 'lib/types/loading-types.js'; import type { MessageStore } from 'lib/types/message-types.js'; @@ -45,6 +41,7 @@ updateCalendarCommunityFilter, clearCalendarCommunityFilter, } from './action-types.js'; +import { reduceCommunityPickerStore } from './community-picker-reducer.js'; import { reduceCryptoStore, setPrimaryIdentityKeys, @@ -55,7 +52,6 @@ import { reduceDeviceID } from './device-id-reducer.js'; import reduceNavInfo from './nav-reducer.js'; import { getVisibility } from './visibility.js'; -import { filterThreadIDsBelongingToCommunity } from '../selectors/calendar-selectors.js'; import { activeThreadSelector } from '../selectors/nav-selectors.js'; import { type NavInfo } from '../types/nav-types.js'; @@ -143,39 +139,6 @@ ...state, windowActive: action.payload, }); - } else if (action.type === updateCalendarCommunityFilter) { - const nonThreadFilters = nonThreadCalendarFilters(state.calendarFilters); - - const threadIDs = Array.from( - filterThreadIDsBelongingToCommunity( - action.payload, - state.threadStore.threadInfos, - ), - ); - return { - ...state, - calendarFilters: [ - ...nonThreadFilters, - { - type: calendarThreadFilterTypes.THREAD_LIST, - threadIDs, - }, - ], - communityPickerStore: { - ...state.communityPickerStore, - calendar: action.payload, - }, - }; - } else if (action.type === clearCalendarCommunityFilter) { - const nonThreadFilters = nonThreadCalendarFilters(state.calendarFilters); - return { - ...state, - calendarFilters: nonThreadFilters, - communityPickerStore: { - ...state.communityPickerStore, - calendar: null, - }, - }; } else if (action.type === setNewSessionActionType) { if ( invalidSessionDowngrade( @@ -213,11 +176,20 @@ action.type !== setPrimaryIdentityKeys && action.type !== setNotificationIdentityKeys && action.type !== setPickledPrimaryAccount && - action.type !== setPickledNotificationAccount + action.type !== setPickledNotificationAccount && + action.type !== updateCalendarCommunityFilter && + action.type !== clearCalendarCommunityFilter ) { state = baseReducer(state, action).state; } + const { communityPickerStore, calendarFilters } = reduceCommunityPickerStore( + state.communityPickerStore, + state.calendarFilters, + state.threadStore, + action, + ); + state = { ...state, navInfo: reduceNavInfo( @@ -227,6 +199,8 @@ ), deviceID: reduceDeviceID(state.deviceID, action), cryptoStore: reduceCryptoStore(state.cryptoStore, action), + communityPickerStore, + calendarFilters, }; return validateState(oldState, state);