Merge branch 'IDTEAM-1.26.0' into 'main'
[ProtonMail-WebClient.git] / packages / pass / store / reducers / alias.ts
blobde840708ea065941c5673644e494e646c15c1efd
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 { AliasDetails, Maybe, MaybeNull } from '@proton/pass/types';
11 import type { AliasOptions } from '@proton/pass/types/data/alias';
12 import { merge } from '@proton/pass/utils/object/merge';
14 export type AliasDetailsState = Maybe<Omit<AliasDetails, 'aliasEmail'>>;
16 export type AliasState = {
17     aliasOptions: MaybeNull<AliasOptions>;
18     aliasDetails: { [aliasEmail: string]: AliasDetailsState };
21 const getInitialState = (): AliasState => ({ aliasOptions: null, aliasDetails: {} });
23 const reducer: Reducer<AliasState> = (state = getInitialState(), action) => {
24     if (getAliasOptionsSuccess.match(action)) {
25         return merge(state, { aliasOptions: { ...action.payload.options } });
26     }
28     if (itemCreationIntent.match(action) && action.payload.type === 'alias') {
29         const {
30             payload: {
31                 extraData: { mailboxes, aliasEmail },
32             },
33         } = action;
35         return merge(state, {
36             aliasDetails: {
37                 [aliasEmail]: { mailboxes },
38             },
39         });
40     }
42     if (getAliasDetailsSuccess.match(action) || aliasDetailsSync.match(action)) {
43         const {
44             payload: { aliasEmail, ...aliasDetails },
45         } = action;
47         return merge(state, { aliasDetails: { [aliasEmail]: aliasDetails } });
48     }
50     if (
51         itemEditIntent.match(action) &&
52         action.payload.type === 'alias' &&
53         action.payload.extraData &&
54         action.payload.extraData.aliasOwner
55     ) {
56         const {
57             extraData: { mailboxes, aliasEmail },
58         } = action.payload;
59         return merge(state, { aliasDetails: { [aliasEmail]: { mailboxes } } });
60     }
62     return state;
65 export default reducer;