21535: added timeouts to test spec Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[arvados.git] / services / workbench2 / src / store / public-favorites / public-favorites-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { unionize, ofType, UnionOf } from "common/unionize";
6 import { Dispatch } from "redux";
7 import { RootState } from "../store";
8 import { checkPublicFavorite } from "./public-favorites-reducer";
9 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
10 import { ServiceRepository } from "services/services";
11 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
12 import { addDisabledButton, removeDisabledButton } from "store/multiselect/multiselect-actions";
13 import { MultiSelectMenuActionNames } from "views-components/multiselect-toolbar/ms-menu-actions";
14 import { loadPublicFavoritesTree } from "store/side-panel-tree/side-panel-tree-actions";
15
16 export const publicFavoritesActions = unionize({
17     TOGGLE_PUBLIC_FAVORITE: ofType<{ resourceUuid: string }>(),
18     CHECK_PRESENCE_IN_PUBLIC_FAVORITES: ofType<string[]>(),
19     UPDATE_PUBLIC_FAVORITES: ofType<Record<string, boolean>>()
20 });
21
22 export type PublicFavoritesAction = UnionOf<typeof publicFavoritesActions>;
23
24 export const togglePublicFavorite = (resource: { uuid: string; name: string }) =>
25     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
26         dispatch(progressIndicatorActions.START_WORKING("togglePublicFavorite"));
27         dispatch<any>(addDisabledButton(MultiSelectMenuActionNames.ADD_TO_PUBLIC_FAVORITES))
28         const uuidPrefix = getState().auth.config.uuidPrefix;
29         const uuid = `${uuidPrefix}-j7d0g-publicfavorites`;
30         dispatch(publicFavoritesActions.TOGGLE_PUBLIC_FAVORITE({ resourceUuid: resource.uuid }));
31         const isPublicFavorite = checkPublicFavorite(resource.uuid, getState().publicFavorites);
32         dispatch(snackbarActions.OPEN_SNACKBAR({
33             message: isPublicFavorite
34                 ? "Removing from public favorites..."
35                 : "Adding to public favorites...",
36             kind: SnackbarKind.INFO
37         }));
38
39         const promise: any = isPublicFavorite
40             ? services.favoriteService.delete({ userUuid: uuid, resourceUuid: resource.uuid })
41             : services.favoriteService.create({ userUuid: uuid, resource });
42
43         return promise
44             .then(() => {
45                 dispatch(publicFavoritesActions.UPDATE_PUBLIC_FAVORITES({ [resource.uuid]: !isPublicFavorite }));
46                 dispatch(snackbarActions.CLOSE_SNACKBAR());
47                 dispatch(snackbarActions.OPEN_SNACKBAR({
48                     message: isPublicFavorite
49                         ? "Removed from public favorites"
50                         : "Added to public favorites",
51                     hideDuration: 2000,
52                     kind: SnackbarKind.SUCCESS
53                 }));
54                 dispatch<any>(removeDisabledButton(MultiSelectMenuActionNames.ADD_TO_PUBLIC_FAVORITES))
55                 dispatch(progressIndicatorActions.STOP_WORKING("togglePublicFavorite"));
56                 dispatch<any>(loadPublicFavoritesTree())
57             })
58             .catch((e: any) => {
59                 dispatch(progressIndicatorActions.STOP_WORKING("togglePublicFavorite"));
60                 throw e;
61             });
62     };
63
64 export const updatePublicFavorites = (resourceUuids: string[]) =>
65     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
66         const uuidPrefix = getState().auth.config.uuidPrefix;
67         const uuid = `${uuidPrefix}-j7d0g-publicfavorites`;
68         dispatch(publicFavoritesActions.CHECK_PRESENCE_IN_PUBLIC_FAVORITES(resourceUuids));
69         services.favoriteService
70             .checkPresenceInFavorites(uuid, resourceUuids)
71             .then((results: any) => {
72                 dispatch(publicFavoritesActions.UPDATE_PUBLIC_FAVORITES(results));
73             });
74     };
75
76 export const getIsAdmin = () =>
77     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
78         const resource = getState().auth.user!.isAdmin;
79         return resource;
80     };