1 import { createAction } from '@reduxjs/toolkit';
2 import { c } from 'ttag';
4 import { ALIAS_DETAILS_MAX_AGE, ALIAS_OPTIONS_MAX_AGE } from '@proton/pass/constants';
5 import { isAliasDisabled } from '@proton/pass/lib/items/item.predicates';
6 import { withCache } from '@proton/pass/store/actions/enhancers/cache';
7 import { type ActionCallback, withCallback } from '@proton/pass/store/actions/enhancers/callback';
8 import { withNotification } from '@proton/pass/store/actions/enhancers/notification';
12 aliasSyncEnableRequest,
13 aliasSyncPendingRequest,
14 aliasSyncStatusRequest,
15 aliasSyncToggleStatusRequest,
16 } from '@proton/pass/store/actions/requests';
17 import { withRequest, withRequestFailure, withRequestSuccess } from '@proton/pass/store/request/enhancers';
18 import { requestActionsFactory } from '@proton/pass/store/request/flow';
27 } from '@proton/pass/types';
28 import { pipe } from '@proton/pass/utils/fp/pipe';
29 import { UNIX_MINUTE } from '@proton/pass/utils/time/constants';
31 export const getAliasOptionsIntent = createAction(
32 'alias::options::get::intent',
34 payload: { shareId: string },
35 callback?: ActionCallback<ReturnType<typeof getAliasOptionsSuccess> | ReturnType<typeof getAliasOptionsFailure>>
38 withRequest({ status: 'start', id: aliasOptionsRequest(payload.shareId) }),
39 withCallback(callback)
43 export const getAliasOptionsSuccess = createAction(
44 'alias::options::get::success',
45 withRequestSuccess((payload: { options: AliasOptions }) => withCache({ payload }), {
46 maxAge: ALIAS_OPTIONS_MAX_AGE,
50 export const getAliasOptionsFailure = createAction(
51 'alias::options::get::failure',
52 withRequestFailure((error: unknown) =>
53 withNotification({ type: 'error', text: c('Error').t`Requesting alias options failed`, error })({
60 export const getAliasDetailsIntent = createAction(
61 'alias::details::get::intent',
62 (payload: { shareId: string; itemId: string; aliasEmail: string }) =>
63 withRequest({ status: 'start', id: aliasDetailsRequest(payload.aliasEmail) })({ payload })
66 export const getAliasDetailsSuccess = createAction(
67 'alias::details::get::success',
68 withRequestSuccess((payload: { aliasEmail: string; mailboxes: AliasMailbox[] }) => withCache({ payload }), {
69 maxAge: ALIAS_DETAILS_MAX_AGE,
73 export const getAliasDetailsFailure = createAction(
74 'alias::details::get::failure',
75 withRequestFailure((payload: { aliasEmail: string }, error: unknown) => ({ payload, error }))
78 export const aliasDetailsSync = createAction(
79 'alias::details::sync',
80 (payload: { aliasEmail: string; mailboxes: AliasMailbox[] }) => withCache({ payload })
83 export const aliasSyncEnable = requestActionsFactory<ShareId, ShareId>('alias::sync::enable')({
84 requestId: aliasSyncEnableRequest,
88 text: c('Success').t`Aliases sync enabled successfully. SimpleLogin aliases may take some time to sync`,
90 })({ payload: { shareId } }),
95 text: c('Error').t`Failed to sync aliases`,
98 })({ payload: null }),
102 export const aliasSyncPending = requestActionsFactory<void, { items: ItemRevision[]; shareId: string }>(
103 'alias::sync::pending'
104 )({ requestId: aliasSyncPendingRequest });
106 export const aliasSyncStatus = requestActionsFactory<void, SlSyncStatusOutput, void>('alias::sync::status')({
107 requestId: aliasSyncStatusRequest,
108 success: { config: { maxAge: UNIX_MINUTE } },
111 export const aliasSyncStatusToggle = requestActionsFactory<AliasToggleStatusDTO, SelectedItem & { item: ItemRevision }>(
112 'alias::sync::status::toggle'
114 requestId: ({ shareId, itemId }) => aliasSyncToggleStatusRequest(shareId, itemId),
116 prepare: ({ shareId, itemId, item }) =>
121 text: isAliasDisabled(item)
123 .t`Alias succcessfully disabled. You will no longer receive emails sent to ${item.aliasEmail}`
125 .t`Alias succcessfully enabled. You can now receive emails sent to ${item.aliasEmail}`,
127 )({ payload: { shareId, itemId, item } }),
133 text: c('Error').t`Alias could not be updated at the moment. Please try again later`,