Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / store / sagas / items / item-bulk-delete.saga.ts
blob05fca8d4c8133f3e1cb8035e5984f3eee04141e1
1 import { END, eventChannel } from 'redux-saga';
2 import { put, select, take, takeLeading } from 'redux-saga/effects';
4 import { deleteItems } from '@proton/pass/lib/items/item.requests';
5 import {
6     itemBulkDeleteFailure,
7     itemBulkDeleteIntent,
8     itemBulkDeleteProgress,
9     itemBulkDeleteSuccess,
10 } from '@proton/pass/store/actions';
11 import type { RequestProgress } from '@proton/pass/store/request/types';
12 import { selectBulkSelection } from '@proton/pass/store/selectors';
13 import type { RootSagaOptions } from '@proton/pass/store/types';
14 import type { BatchItemRevisionIDs, ItemRevision } from '@proton/pass/types';
15 import noop from '@proton/utils/noop';
17 export type BulkDeleteChannel = RequestProgress<BatchItemRevisionIDs>;
19 export const bulkDeleteChannel = (items: ItemRevision[]) =>
20     eventChannel<BulkDeleteChannel>((emitter) => {
21         deleteItems(items, (data, progress) => emitter({ type: 'progress', progress, data }))
22             .then((result: any) => emitter({ type: 'done', result }))
23             .catch((error) => emitter({ type: 'error', error }))
24             .finally(() => emitter(END));
26         return noop;
27     });
29 function* itemBulkDeleteWorker(
30     { onItemsUpdated }: RootSagaOptions,
31     { payload, meta }: ReturnType<typeof itemBulkDeleteIntent>
32 ) {
33     const requestId = meta.request.id;
34     const { selected } = payload;
35     const items = (yield select(selectBulkSelection(selected))) as ItemRevision[];
36     const progressChannel = bulkDeleteChannel(items);
38     while (true) {
39         const action: BulkDeleteChannel = yield take(progressChannel);
40         onItemsUpdated?.();
42         if (action.type === 'progress') yield put(itemBulkDeleteProgress(requestId, action.progress, action.data));
43         if (action.type === 'done') yield put(itemBulkDeleteSuccess(requestId, {}));
44         if (action.type === 'error') yield put(itemBulkDeleteFailure(requestId, {}, action.error));
45     }
48 export default function* watcher(options: RootSagaOptions) {
49     yield takeLeading(itemBulkDeleteIntent.match, itemBulkDeleteWorker, options);