1 import { END, eventChannel } from 'redux-saga';
2 import { put, select, take, takeLeading } from 'redux-saga/effects';
4 import { trashItems } from '@proton/pass/lib/items/item.requests';
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, ItemRevisionResponse } from '@proton/pass/types';
15 import noop from '@proton/utils/noop';
17 type BulkTrashChannel = RequestProgress<ItemRevisionResponse[], BatchItemRevisionIDs>;
19 function* itemBulkTrashWorker(
20 { onItemsUpdated }: RootSagaOptions,
21 { payload: { selected }, meta }: ReturnType<typeof itemBulkTrashIntent>
23 const items = (yield select(selectBulkSelection(selected))) as ItemRevision[];
25 const progressChannel = eventChannel<BulkTrashChannel>((emitter) => {
26 trashItems(items, (data, progress) => emitter({ type: 'progress', progress, data }))
27 .then((result) => emitter({ type: 'done', result }))
28 .catch((error) => emitter({ type: 'error', error }))
29 .finally(() => emitter(END));
35 const action: BulkTrashChannel = yield take(progressChannel);
38 if (action.type === 'progress') yield put(itemBulkTrashProgress(meta.request.id, action.progress, action.data));
39 if (action.type === 'done') yield put(itemBulkTrashSuccess(meta.request.id, {}));
40 if (action.type === 'error') yield put(itemBulkTrashFailure(meta.request.id, {}, action.error));
44 export default function* watcher(options: RootSagaOptions) {
45 yield takeLeading(itemBulkTrashIntent.match, itemBulkTrashWorker, options);