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';
8 itemBulkDeleteProgress,
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));
29 function* itemBulkDeleteWorker(
30 { onItemsUpdated }: RootSagaOptions,
31 { payload, meta }: ReturnType<typeof itemBulkDeleteIntent>
33 const requestId = meta.request.id;
34 const { selected } = payload;
35 const items = (yield select(selectBulkSelection(selected))) as ItemRevision[];
36 const progressChannel = bulkDeleteChannel(items);
39 const action: BulkDeleteChannel = yield take(progressChannel);
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));
48 export default function* watcher(options: RootSagaOptions) {
49 yield takeLeading(itemBulkDeleteIntent.match, itemBulkDeleteWorker, options);