19231: Add smaller page sizes (10 and 20 items) to load faster
[arvados-workbench2.git] / src / store / trash / trash-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from "store/store";
7 import { ServiceRepository } from "services/services";
8 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
9 import { trashPanelActions } from "store/trash-panel/trash-panel-action";
10 import { activateSidePanelTreeItem, loadSidePanelTreeProjects } from "store/side-panel-tree/side-panel-tree-actions";
11 import { projectPanelActions } from "store/project-panel/project-panel-action";
12 import { ResourceKind } from "models/resource";
13 import { navigateTo, navigateToTrash } from 'store/navigation/navigation-action';
14 import { matchCollectionRoute } from 'routes/routes';
15
16 export const toggleProjectTrashed = (uuid: string, ownerUuid: string, isTrashed: boolean) =>
17     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
18         let errorMessage = '';
19         let successMessage = '';
20         try {
21             if (isTrashed) {
22                 errorMessage = "Could not restore project from trash";
23                 successMessage = "Restored from trash";
24                 await services.groupsService.untrash(uuid);
25                 dispatch<any>(navigateTo(uuid));
26                 dispatch<any>(activateSidePanelTreeItem(uuid));
27             } else {
28                 errorMessage = "Could not move project to trash";
29                 successMessage = "Added to trash";
30                 await services.groupsService.trash(uuid);
31                 dispatch<any>(loadSidePanelTreeProjects(ownerUuid));
32                 dispatch<any>(navigateTo(ownerUuid));
33             }
34         } catch (e) {
35             dispatch(snackbarActions.OPEN_SNACKBAR({
36                 message: errorMessage,
37                 kind: SnackbarKind.ERROR
38             }));
39         }
40         dispatch(snackbarActions.OPEN_SNACKBAR({
41             message: successMessage,
42             hideDuration: 2000,
43             kind: SnackbarKind.SUCCESS
44         }));
45     };
46
47 export const toggleCollectionTrashed = (uuid: string, isTrashed: boolean) =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
49         let errorMessage = '';
50         let successMessage = '';
51         try {
52             if (isTrashed) {
53                 const { location } = getState().router;
54                 errorMessage = "Could not restore collection from trash";
55                 successMessage = "Restored from trash";
56                 await services.collectionService.untrash(uuid);
57                 if (matchCollectionRoute(location ? location.pathname : '')) {
58                     dispatch(navigateToTrash);
59                 }
60                 dispatch(trashPanelActions.REQUEST_ITEMS());
61             } else {
62                 errorMessage = "Could not move collection to trash";
63                 successMessage = "Added to trash";
64                 await services.collectionService.trash(uuid);
65                 dispatch(projectPanelActions.REQUEST_ITEMS());
66             }
67         } catch (e) {
68             dispatch(snackbarActions.OPEN_SNACKBAR({
69                 message: errorMessage,
70                 kind: SnackbarKind.ERROR
71             }));
72         }
73         dispatch(snackbarActions.OPEN_SNACKBAR({
74             message: successMessage,
75             hideDuration: 2000,
76             kind: SnackbarKind.SUCCESS
77         }));
78     };
79
80 export const toggleTrashed = (kind: ResourceKind, uuid: string, ownerUuid: string, isTrashed: boolean) =>
81     (dispatch: Dispatch) => {
82         if (kind === ResourceKind.PROJECT) {
83             dispatch<any>(toggleProjectTrashed(uuid, ownerUuid, isTrashed!!));
84         } else if (kind === ResourceKind.COLLECTION) {
85             dispatch<any>(toggleCollectionTrashed(uuid, isTrashed!!));
86         }
87     };