Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / store / sagas / items / item-bulk-trash.saga.ts
blob3b95d7157b06af0e7a5c524c13156f00f8d1893a
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';
5 import {
6     itemBulkTrashFailure,
7     itemBulkTrashIntent,
8     itemBulkTrashProgress,
9     itemBulkTrashSuccess,
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>
22 ) {
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));
31         return noop;
32     });
34     while (true) {
35         const action: BulkTrashChannel = yield take(progressChannel);
36         onItemsUpdated?.();
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));
41     }
44 export default function* watcher(options: RootSagaOptions) {
45     yield takeLeading(itemBulkTrashIntent.match, itemBulkTrashWorker, options);