Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / store / sagas / vaults / vault-delete.saga.ts
blob57c5e05b4d902abd831c5f5e73407133f656bda4
1 import { put, select, takeEvery } from 'redux-saga/effects';
3 import { PassCrypto } from '@proton/pass/lib/crypto';
4 import { deleteVault } from '@proton/pass/lib/vaults/vault.requests';
5 import {
6     getUserAccessIntent,
7     vaultDeleteFailure,
8     vaultDeleteIntent,
9     vaultDeleteSuccess,
10 } from '@proton/pass/store/actions';
11 import { withRevalidate } from '@proton/pass/store/request/enhancers';
12 import { selectUserDefaultShareID } from '@proton/pass/store/selectors';
13 import type { RootSagaOptions } from '@proton/pass/store/types';
14 import type { Maybe } from '@proton/pass/types';
16 function* deleteVaultWorker(
17     { onItemsUpdated, getAuthStore }: RootSagaOptions,
18     { payload: { shareId, content }, meta }: ReturnType<typeof vaultDeleteIntent>
19 ) {
20     try {
21         yield deleteVault(shareId);
22         PassCrypto.removeShare(shareId);
24         /* Handle edge case when the alias sync vault is deleted:
25          * we check the new alias sync vault from BE in the user access route */
26         const userID = getAuthStore().getUserID();
27         const aliasSyncShareId: Maybe<string> = yield select(selectUserDefaultShareID);
28         if (shareId === aliasSyncShareId) yield put(withRevalidate(getUserAccessIntent(userID!)));
30         yield put(vaultDeleteSuccess(meta.request.id, { shareId, content }));
31         onItemsUpdated?.();
32     } catch (e) {
33         yield put(vaultDeleteFailure(meta.request.id, { shareId, content }, e));
34     }
37 export default function* watcher(options: RootSagaOptions) {
38     yield takeEvery(vaultDeleteIntent.match, deleteVaultWorker, options);