diff --git a/lib/reducers/draft-reducer.js b/lib/reducers/draft-reducer.js new file mode 100644 --- /dev/null +++ b/lib/reducers/draft-reducer.js @@ -0,0 +1,77 @@ +// @flow + +import { + moveDraftActionType, + setDraftStoreDrafts, + updateDraftActionType, +} from '../actions/draft-actions'; +import type { DraftStore, DraftStoreOperation } from '../types/draft-types'; +import type { BaseAction } from '../types/redux-types'; + +type ReduceDraftStoreResult = { + +draftStoreOperations: $ReadOnlyArray, + +draftStore: DraftStore, +}; +function reduceDraftStore( + draftStore: DraftStore, + action: BaseAction, +): ReduceDraftStoreResult { + if (action.type === updateDraftActionType) { + const key = action.payload.key; + const text = action.payload.text; + + const draftStoreOperations = [ + { + type: 'update', + payload: { key: key, text: text }, + }, + ]; + return { + draftStoreOperations, + draftStore: { + ...draftStore, + drafts: { + ...draftStore.drafts, + [key]: text, + }, + }, + }; + } else if (action.type === moveDraftActionType) { + const oldKey = action.payload.oldKey; + const newKey = action.payload.newKey; + + const draftStoreOperations = [ + { + type: 'move', + payload: { oldKey: oldKey, newKey: newKey }, + }, + ]; + + const { [oldKey]: text, ...draftsWithoutOldKey } = draftStore.drafts; + return { + draftStoreOperations, + draftStore: { + ...draftStore, + drafts: { + ...draftsWithoutOldKey, + [newKey]: text, + }, + }, + }; + } else if (action.type === setDraftStoreDrafts) { + const drafts = {}; + for (const dbDraftInfo of action.payload) { + drafts[dbDraftInfo.key] = dbDraftInfo.text; + } + return { + draftStoreOperations: [], + draftStore: { + ...draftStore, + drafts: drafts, + }, + }; + } + return { draftStore, draftStoreOperations: [] }; +} + +export { reduceDraftStore }; diff --git a/lib/reducers/master-reducer.js b/lib/reducers/master-reducer.js --- a/lib/reducers/master-reducer.js +++ b/lib/reducers/master-reducer.js @@ -11,6 +11,7 @@ import reduceCalendarFilters from './calendar-filters-reducer'; import reduceConnectionInfo from './connection-reducer'; import reduceDataLoaded from './data-loaded-reducer'; +import { reduceDraftStore } from './draft-reducer'; import reduceEnabledApps from './enabled-apps-reducer'; import { reduceEntryInfos } from './entry-reducer'; import reduceLifecycleState from './lifecycle-state-reducer'; @@ -75,10 +76,16 @@ } } + const { draftStore, draftStoreOperations } = reduceDraftStore( + state.draftStore, + action, + ); + return { state: { ...state, navInfo: reduceBaseNavInfo(state.navInfo, action), + draftStore, entryStore, loadingStatuses: reduceLoadingStatuses(state.loadingStatuses, action), currentUserInfo: reduceCurrentUserInfo(state.currentUserInfo, action), @@ -100,6 +107,7 @@ dataLoaded: reduceDataLoaded(state.dataLoaded, action), }, storeOperations: { + draftStoreOperations, threadStoreOperations, messageStoreOperations, }, diff --git a/lib/types/store-ops-types.js b/lib/types/store-ops-types.js --- a/lib/types/store-ops-types.js +++ b/lib/types/store-ops-types.js @@ -1,8 +1,11 @@ // @flow + +import type { DraftStoreOperation } from './draft-types'; import type { MessageStoreOperation } from './message-types'; import type { ThreadStoreOperation } from './thread-types'; export type StoreOperations = { + +draftStoreOperations: $ReadOnlyArray, +threadStoreOperations: $ReadOnlyArray, +messageStoreOperations: $ReadOnlyArray, };