1 import { END, eventChannel } from 'redux-saga';
2 import { put, select, take, takeLeading } from 'redux-saga/effects';
4 import { moveItems } 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 { BatchItemRevisions, ItemRevision } from '@proton/pass/types';
15 import noop from '@proton/utils/noop';
17 export type BulkMoveItemsChannel = RequestProgress<
19 BatchItemRevisions & { movedItems: ItemRevision[]; destinationShareId: string }
22 export const bulkMoveChannel = (items: ItemRevision[], destinationShareId: string) =>
23 eventChannel<BulkMoveItemsChannel>((emitter) => {
24 moveItems(items, destinationShareId, (data, progress) => emitter({ type: 'progress', progress, data }))
25 .then((result) => emitter({ type: 'done', result }))
26 .catch((error) => emitter({ type: 'error', error }))
27 .finally(() => emitter(END));
32 function* itemBulkMoveWorker(
33 { onItemsUpdated }: RootSagaOptions,
34 { payload, meta }: ReturnType<typeof itemBulkMoveIntent>
36 const requestId = meta.request.id;
37 const { selected, shareId } = payload;
38 const items = (yield select(selectBulkSelection(selected))) as ItemRevision[];
39 const itemsToMove = items.filter((item) => item.shareId !== shareId);
40 const channel = bulkMoveChannel(itemsToMove, shareId);
43 const action: BulkMoveItemsChannel = yield take(channel);
46 if (action.type === 'progress') yield put(itemBulkMoveProgress(requestId, action.progress, action.data));
47 if (action.type === 'done') yield put(itemBulkMoveSuccess(requestId, {}));
48 if (action.type === 'error') yield put(itemBulkMoveFailure(requestId, payload, action.error));
52 export default function* watcher(options: RootSagaOptions) {
53 yield takeLeading(itemBulkMoveIntent.match, itemBulkMoveWorker, options);