diff --git a/lib/reducers/draft-reducer.js b/lib/reducers/draft-reducer.js index 2b82ee25f..67bfc678a 100644 --- a/lib/reducers/draft-reducer.js +++ b/lib/reducers/draft-reducer.js @@ -1,91 +1,125 @@ // @flow import { setClientDBStoreActionType } from '../actions/client-db-store-actions.js'; import { moveDraftActionType, updateDraftActionType, } from '../actions/draft-actions.js'; import { deleteKeyserverAccountActionTypes, logOutActionTypes, + deleteAccountActionTypes, } 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'; type ReduceDraftStoreResult = { +draftStoreOperations: $ReadOnlyArray, +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 === deleteAccountActionTypes.success ) { return { draftStoreOperations: [{ type: 'remove_all' }], draftStore: { drafts: {} }, }; + } else if (action.type === deleteKeyserverAccountActionTypes.success) { + return removeKeyserversDraftsFromStore( + draftStore, + action.payload.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; const draftStoreOperations = [ { type: 'update', payload: { key, text }, }, ]; return { draftStoreOperations, draftStore: { ...draftStore, drafts: { ...draftStore.drafts, [key]: text, }, }, }; } else if (action.type === moveDraftActionType) { const { oldKey, newKey } = action.payload; const draftStoreOperations = [ { type: 'move', payload: { oldKey, newKey }, }, ]; const { [oldKey]: text, ...draftsWithoutOldKey } = draftStore.drafts; return { draftStoreOperations, draftStore: { ...draftStore, drafts: { ...draftsWithoutOldKey, [newKey]: text, }, }, }; } else if (action.type === setClientDBStoreActionType) { const drafts: { [string]: string } = {}; for (const dbDraftInfo of action.payload.drafts) { drafts[dbDraftInfo.key] = dbDraftInfo.text; } return { draftStoreOperations: [], draftStore: { ...draftStore, drafts: drafts, }, }; } 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 index 000000000..ece63e57c --- /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); + }); +});