Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / store / actions / creators / share.ts
blobacba7dc7158292a6b51b04bd8f3ab4142812a9f9
1 import { createAction } from '@reduxjs/toolkit';
2 import { c } from 'ttag';
4 import { isVaultShare } from '@proton/pass/lib/shares/share.predicates';
5 import { withCache } from '@proton/pass/store/actions/enhancers/cache';
6 import { withNotification } from '@proton/pass/store/actions/enhancers/notification';
7 import {
8     shareAccessOptionsRequest,
9     shareEditMemberRoleRequest,
10     shareLeaveRequest,
11     shareRemoveMemberRequest,
12 } from '@proton/pass/store/actions/requests';
13 import { withRequest, withRequestFailure, withRequestSuccess } from '@proton/pass/store/request/enhancers';
14 import { requestActionsFactory } from '@proton/pass/store/request/flow';
15 import type { SynchronizationResult } from '@proton/pass/store/sagas/client/sync';
16 import type { SelectedShare, Share, ShareAccessKeys, ShareRole } from '@proton/pass/types';
17 import type {
18     ShareAccessOptions,
19     ShareEditMemberAccessIntent,
20     ShareRemoveMemberAccessIntent,
21 } from '@proton/pass/types/data/shares.dto';
22 import { pipe } from '@proton/pass/utils/fp/pipe';
24 export const shareEditSync = createAction('share::edit:sync', (payload: { id: string; share: Share }) =>
25     withCache({ payload })
28 export const shareDeleteSync = createAction('share::delete::sync', (share: Share) =>
29     pipe(
30         withCache,
31         withNotification({
32             type: 'info',
33             text: isVaultShare(share)
34                 ? c('Info').t`Vault "${share.content.name}" was removed`
35                 : c('Info').t`An item previously shared with you was removed`,
36         })
37     )({ payload: { shareId: share.shareId } })
40 export const sharesSync = createAction('shares::sync', (payload: SynchronizationResult) => withCache({ payload }));
42 export const shareRemoveMemberAccessIntent = createAction(
43     'share::member::remove-access::intent',
44     (payload: ShareRemoveMemberAccessIntent) =>
45         withRequest({ status: 'start', id: shareRemoveMemberRequest(payload.userShareId) })({ payload })
48 export const shareRemoveMemberAccessSuccess = createAction(
49     'share::member::remove-access::success',
50     withRequestSuccess((shareId: string, userShareId: string) =>
51         pipe(
52             withCache,
53             withNotification({
54                 type: 'info',
55                 text: c('Info').t`User's access removed`,
56             })
57         )({ payload: { shareId, userShareId } })
58     )
61 export const shareRemoveMemberAccessFailure = createAction(
62     'share::member::remove-access::failure',
63     withRequestFailure((error: unknown) =>
64         withNotification({
65             type: 'error',
66             text: c('Error').t`Failed to remove user's access.`,
67             error,
68         })({ payload: {} })
69     )
72 export const shareEditMemberAccessIntent = createAction(
73     'share::member::edit-access::intent',
74     (payload: ShareEditMemberAccessIntent) =>
75         withRequest({ status: 'start', id: shareEditMemberRoleRequest(payload.userShareId) })({ payload })
78 export const shareEditMemberAccessSuccess = createAction(
79     'share::member::edit-access::success',
80     withRequestSuccess((shareId: string, userShareId: string, shareRoleId: ShareRole) =>
81         pipe(
82             withCache,
83             withNotification({
84                 type: 'info',
85                 text: c('Info').t`User's access successfully updated`,
86             })
87         )({ payload: { shareId, userShareId, shareRoleId } })
88     )
91 export const shareEditMemberAccessFailure = createAction(
92     'share::member:edit-access::failure',
93     withRequestFailure((error: unknown) =>
94         withNotification({
95             type: 'error',
96             text: c('Error').t`Failed to edit user's access.`,
97             error,
98         })({ payload: {} })
99     )
102 export const shareLeaveIntent = createAction('share::leave::intent', (payload: { shareId: string }) =>
103     pipe(
104         withRequest({ status: 'start', id: shareLeaveRequest(payload.shareId) }),
105         withNotification({
106             type: 'info',
107             expiration: -1,
108             loading: true,
109             text: c('Info').t`Leaving vault...`,
110         })
111     )({ payload })
114 export const shareLeaveSuccess = createAction(
115     'share::leave::success',
116     withRequestSuccess((shareId: string) =>
117         withNotification({
118             type: 'info',
119             text: c('Info').t`Successfully left the vault`,
120         })({ payload: { shareId } })
121     )
124 export const shareLeaveFailure = createAction(
125     'share::leave::failure',
126     withRequestFailure((error: unknown) =>
127         withNotification({
128             type: 'error',
129             text: c('Error').t`Could not leave vault.`,
130             error,
131         })({ payload: {}, error })
132     )
135 export const getShareAccessOptions = requestActionsFactory<SelectedShare, ShareAccessOptions>('share::access-options')({
136     requestId: ({ shareId }) => shareAccessOptionsRequest(shareId),
137     success: { config: { maxAge: 15 } },
138     failure: {
139         prepare: (error: unknown) =>
140             withNotification({
141                 type: 'error',
142                 text: c('Error').t`Could not resolve share members`,
143                 error,
144             })({ payload: {}, error }),
145     },
148 export const shareAccessChange = createAction('share::access::change', (payload: Pick<Share, ShareAccessKeys>) =>
149     withCache({ payload })