Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / store / reducers / alias.ts
blobcb8516f5100bccaf31a97a688e0868c3e5e58166
1 import type { Reducer } from 'redux';
3 import {
4     aliasDetailsSync,
5     getAliasDetailsSuccess,
6     getAliasOptionsSuccess,
7     itemCreationIntent,
8     itemEditIntent,
9 } from '@proton/pass/store/actions';
10 import type { MaybeNull } from '@proton/pass/types';
11 import type { AliasMailbox, AliasOptions } from '@proton/pass/types/data/alias';
12 import { merge } from '@proton/pass/utils/object/merge';
14 export type AliasState = {
15     aliasOptions: MaybeNull<AliasOptions>;
16     aliasDetails: { [aliasEmail: string]: AliasMailbox[] };
19 const getInitialState = (): AliasState => ({ aliasOptions: null, aliasDetails: {} });
21 const reducer: Reducer<AliasState> = (state = getInitialState(), action) => {
22     if (getAliasOptionsSuccess.match(action)) {
23         return merge(state, { aliasOptions: { ...action.payload.options } });
24     }
26     if (itemCreationIntent.match(action) && action.payload.type === 'alias') {
27         const {
28             payload: {
29                 extraData: { mailboxes, aliasEmail },
30             },
31         } = action;
33         return merge(state, {
34             aliasDetails: {
35                 [aliasEmail]: mailboxes,
36             },
37         });
38     }
40     if (getAliasDetailsSuccess.match(action)) {
41         const {
42             payload: { aliasEmail, mailboxes },
43         } = action;
44         return merge(state, { aliasDetails: { [aliasEmail]: mailboxes } });
45     }
47     if (aliasDetailsSync.match(action)) {
48         const {
49             payload: { aliasEmail, mailboxes },
50         } = action;
51         return merge(state, { aliasDetails: { [aliasEmail]: mailboxes } });
52     }
54     if (
55         itemEditIntent.match(action) &&
56         action.payload.type === 'alias' &&
57         action.payload.extraData &&
58         action.payload.extraData.aliasOwner
59     ) {
60         const {
61             extraData: { mailboxes, aliasEmail },
62         } = action.payload;
63         return merge(state, { aliasDetails: { [aliasEmail]: mailboxes } });
64     }
66     return state;
69 export default reducer;