diff --git a/lib/reducers/draft-reducer.js b/lib/reducers/draft-reducer.js --- a/lib/reducers/draft-reducer.js +++ b/lib/reducers/draft-reducer.js @@ -6,9 +6,11 @@ updateDraftActionType, } from '../actions/draft-actions.js'; import { + keyserverAuthActionTypes, deleteKeyserverAccountActionTypes, logOutActionTypes, } from '../actions/user-actions.js'; +import { extractKeyserverIDFromID } from '../keyserver-conn/keyserver-call-utils.js'; import { setNewSessionActionType } from '../keyserver-conn/keyserver-conn-types.js'; import type { DraftStore, DraftStoreOperation } from '../types/draft-types.js'; import type { BaseAction } from '../types/redux-types.js'; @@ -18,20 +20,50 @@ +draftStore: DraftStore, }; +function removeKeyserversDraftsFromStore( + draftStore: DraftStore, + keyserverIDs: $ReadOnlyArray, +): ReduceDraftStoreResult { + const keyserverIDsSet = new Set(keyserverIDs); + + const drafts: { [key: string]: string } = {}; + const ids: string[] = []; + for (const key in draftStore.drafts) { + if (keyserverIDsSet.has(extractKeyserverIDFromID(key))) { + ids.push(key); + } else { + drafts[key] = draftStore.drafts[key]; + } + } + + return { + draftStoreOperations: [{ type: 'remove', payload: { ids } }], + draftStore: { ...draftStore, drafts }, + }; +} + function reduceDraftStore( draftStore: DraftStore, action: BaseAction, ): ReduceDraftStoreResult { if ( action.type === logOutActionTypes.success || - action.type === deleteKeyserverAccountActionTypes.success || - (action.type === setNewSessionActionType && - action.payload.sessionChange.cookieInvalidated) + action.type === deleteKeyserverAccountActionTypes.success ) { - return { - draftStoreOperations: [{ type: 'remove_all' }], - draftStore: { drafts: {} }, - }; + return removeKeyserversDraftsFromStore( + draftStore, + action.payload.keyserverIDs, + ); + } else if (action.type === keyserverAuthActionTypes.success) { + const keyserverIDs = Object.keys(action.payload.updatesCurrentAsOf); + return removeKeyserversDraftsFromStore(draftStore, keyserverIDs); + } else if ( + action.type === setNewSessionActionType && + action.payload.sessionChange.cookieInvalidated + ) { + return removeKeyserversDraftsFromStore(draftStore, [ + action.payload.keyserverID, + ]); } else if (action.type === updateDraftActionType) { const { key, text } = action.payload; @@ -88,4 +120,4 @@ return { draftStore, draftStoreOperations: [] }; } -export { reduceDraftStore }; +export { reduceDraftStore, removeKeyserversDraftsFromStore }; diff --git a/lib/reducers/draft-reducer.test.js b/lib/reducers/draft-reducer.test.js new file mode 100644 --- /dev/null +++ b/lib/reducers/draft-reducer.test.js @@ -0,0 +1,40 @@ +// @flow + +import { removeKeyserversDraftsFromStore } from './draft-reducer.js'; + +describe('removeKeyserversDraftsFromStore', () => { + const keyserver1 = '256'; + const keyserver2 = '100'; + const keyserver3 = '200'; + + const drafts1 = { + [keyserver1 + '|1']: 'test', + [keyserver1 + '|2']: 'test', + [keyserver1 + '|3']: 'test', + }; + const drafts2 = { + [keyserver2 + '|1']: 'test', + [keyserver2 + '|2']: 'test', + [keyserver2 + '|3']: 'test', + }; + const drafts3 = { + [keyserver3 + '|1']: 'test', + [keyserver3 + '|2']: 'test', + [keyserver3 + '|3']: 'test', + }; + + it('removes drafts of given keyservers', () => { + const result = removeKeyserversDraftsFromStore( + { drafts: { ...drafts1, ...drafts2, ...drafts3 } }, + [keyserver1, keyserver2], + ); + expect(result.draftStoreOperations).toEqual([ + { + type: 'remove', + payload: { ids: [...Object.keys(drafts1), ...Object.keys(drafts2)] }, + }, + ]); + + expect(result.draftStore.drafts).toEqual(drafts3); + }); +});