Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / store / actions / creators / vault.ts
blobdcae930d248d1a8255009dc683b8dd4568f2f595
1 import { createAction } from '@reduxjs/toolkit';
2 import { c } from 'ttag';
4 import { withCache } from '@proton/pass/store/actions/enhancers/cache';
5 import type { ActionCallback } from '@proton/pass/store/actions/enhancers/callback';
6 import { withCallback } from '@proton/pass/store/actions/enhancers/callback';
7 import { withNotification } from '@proton/pass/store/actions/enhancers/notification';
8 import {
9     vaultCreateRequest,
10     vaultDeleteRequest,
11     vaultEditRequest,
12     vaultMoveAllItemsRequest,
13     vaultTransferOwnerRequest,
14 } from '@proton/pass/store/actions/requests';
15 import {
16     withRequest,
17     withRequestFailure,
18     withRequestProgress,
19     withRequestSuccess,
20 } from '@proton/pass/store/request/enhancers';
21 import type { BatchItemRevisions, ItemMoveDTO, ItemRevision, Share, ShareContent, ShareType } from '@proton/pass/types';
22 import type { VaultTransferOwnerIntent } from '@proton/pass/types/data/vault.dto';
23 import { pipe } from '@proton/pass/utils/fp/pipe';
24 import { uniqueId } from '@proton/pass/utils/string/unique-id';
26 export const vaultCreationIntent = createAction(
27     'vault::creation::intent',
29     (
30         payload: { content: ShareContent<ShareType.Vault> },
31         callback?: ActionCallback<ReturnType<typeof vaultCreationSuccess> | ReturnType<typeof vaultCreationFailure>>
32     ) => pipe(withRequest({ status: 'start', id: vaultCreateRequest(uniqueId()) }), withCallback(callback))({ payload })
35 export const vaultCreationFailure = createAction(
36     'vault::creation::failure',
37     withRequestFailure((payload: { content: ShareContent<ShareType.Vault> }, error: unknown) =>
38         withNotification({
39             type: 'error',
40             text: c('Error').t`Vault "${payload.content.name}" creation failed`,
41             error,
42         })({ payload, error })
43     )
46 /* This action will be intercepted by the events.saga to create
47  * the appropriate event loop channel upon successful creation */
48 export const vaultCreationSuccess = createAction(
49     'vault::creation::success',
50     withRequestSuccess(
51         (payload: { share: Share<ShareType.Vault> }) =>
52             pipe(
53                 withCache,
54                 withNotification({
55                     type: 'success',
56                     text: c('Info').t`Vault "${payload.share.content.name}" successfully created`,
57                 })
58             )({ payload }),
59         { data: true }
60     )
63 export const vaultEditIntent = createAction(
64     'vault::edit::intent',
65     (payload: { shareId: string; content: ShareContent<ShareType.Vault> }) =>
66         withRequest({ status: 'start', id: vaultEditRequest(payload.shareId) })({ payload })
69 export const vaultEditFailure = createAction(
70     'vault::edit::failure',
71     withRequestFailure((payload: { shareId: string; content: ShareContent<ShareType.Vault> }, error: unknown) =>
72         withNotification({
73             type: 'error',
74             text: c('Error').t`Updating vault "${payload.content.name}" failed`,
75             error,
76         })({ payload, error })
77     )
80 export const vaultEditSuccess = createAction(
81     'vault::edit::success',
82     withRequestSuccess((payload: { share: Share<ShareType.Vault> }) =>
83         pipe(
84             withCache,
85             withNotification({
86                 type: 'info',
87                 text: c('Info').t`Vault "${payload.share.content.name}" successfully updated`,
88             })
89         )({ payload })
90     )
93 export const vaultDeleteIntent = createAction(
94     'vault::delete::intent',
96     (payload: { shareId: string; content: ShareContent<ShareType.Vault> }) =>
97         pipe(
98             withRequest({ status: 'start', id: vaultDeleteRequest(payload.shareId) }),
99             withNotification({
100                 type: 'info',
101                 loading: true,
102                 text: c('Info').t`Deleting "${payload.content.name}"`,
103             })
104         )({ payload })
107 export const vaultDeleteFailure = createAction(
108     'vault::delete::failure',
109     withRequestFailure((payload: { shareId: string; content: ShareContent<ShareType.Vault> }, error: unknown) =>
110         withNotification({
111             type: 'error',
112             text: c('Error').t`Deleting vault "${payload.content.name}" failed`,
113             error,
114         })({ payload, error })
115     )
118 export const vaultDeleteSuccess = createAction(
119     'vault::delete::success',
120     withRequestSuccess((payload: { shareId: string; content: ShareContent<ShareType.Vault> }) =>
121         pipe(
122             withCache,
123             withNotification({
124                 type: 'info',
125                 text: c('Info').t`Vault "${payload.content.name}" successfully deleted`,
126             })
127         )({ payload })
128     )
131 export const vaultMoveAllItemsIntent = createAction(
132     'vault::move::items::intent',
133     (payload: { shareId: string; content: ShareContent<ShareType.Vault>; destinationShareId: string }) =>
134         pipe(
135             withRequest({ status: 'start', id: vaultMoveAllItemsRequest(payload.shareId) }),
136             withNotification({
137                 expiration: -1,
138                 type: 'info',
139                 loading: true,
140                 text: c('Info').t`Moving all items from "${payload.content.name}"`,
141             })
142         )({ payload })
145 export const vaultMoveAllItemsProgress = createAction(
146     'vault::move::items::progress',
147     withRequestProgress((payload: BatchItemRevisions & { movedItems: ItemRevision[]; destinationShareId: string }) =>
148         withCache({ payload })
149     )
152 export const vaultMoveAllItemsSuccess = createAction(
153     'vault::move::items::success',
154     withRequestSuccess((payload: { content: ShareContent<ShareType.Vault> }) =>
155         withNotification({
156             type: 'info',
157             text: c('Info').t`All items from "${payload.content.name}" successfully moved`,
158         })({ payload })
159     )
162 export const vaultMoveAllItemsFailure = createAction(
163     'vault::move::items::failure',
164     withRequestFailure((payload: { shareId: string; content: ShareContent<ShareType.Vault> }, error: unknown) =>
165         withNotification({
166             type: 'error',
167             text: c('Error').t`Failed to move all items from "${payload.content.name}"`,
168             error,
169         })({ payload, error })
170     )
173 export const vaultTransferOwnerIntent = createAction(
174     'vault::ownership::transfer::intent',
175     (payload: VaultTransferOwnerIntent) =>
176         withRequest({ status: 'start', id: vaultTransferOwnerRequest(payload.userShareId) })({ payload })
179 export const vaultTransferOwnershipSuccess = createAction(
180     'vault::ownership::transfer::success',
181     withRequestSuccess((shareId: string, userShareId: string) =>
182         pipe(
183             withCache,
184             withNotification({
185                 type: 'info',
186                 text: c('Info').t`Ownership successfully transfered. You are no long the owner of this vault.`,
187             })
188         )({ payload: { shareId, userShareId } })
189     )
192 export const vaultTransferOwnershipFailure = createAction(
193     'vault::ownership::transfer::failure',
194     withRequestFailure((error: unknown) =>
195         withNotification({
196             type: 'error',
197             text: c('Error').t`Failed to transfer this vault's ownership.`,
198             error,
199         })({ payload: {} })
200     )
203 export const sharedVaultCreated = createAction(
204     'vault::shared::created',
205     (payload: { share: Share<ShareType.Vault>; move?: ItemMoveDTO }) => withCache({ payload })