21200: copied branch from arvados-workbench2 Arvados-DCO-1.1-Signed-off-by: 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 { loadPublicFavoritesTree } from "store/side-panel-tree/side-panel-tree-actions";
13
14 export const publicFavoritesActions = unionize({
15     TOGGLE_PUBLIC_FAVORITE: ofType<{ resourceUuid: string }>(),
16     CHECK_PRESENCE_IN_PUBLIC_FAVORITES: ofType<string[]>(),
17     UPDATE_PUBLIC_FAVORITES: ofType<Record<string, boolean>>()
18 });
19
20 export type PublicFavoritesAction = UnionOf<typeof publicFavoritesActions>;
21
22 export const togglePublicFavorite = (resource: { uuid: string; name: string }) =>
23     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
24         dispatch(progressIndicatorActions.START_WORKING("togglePublicFavorite"));
25         const uuidPrefix = getState().auth.config.uuidPrefix;
26         const uuid = `${uuidPrefix}-j7d0g-publicfavorites`;
27         dispatch(publicFavoritesActions.TOGGLE_PUBLIC_FAVORITE({ resourceUuid: resource.uuid }));
28         const isPublicFavorite = checkPublicFavorite(resource.uuid, getState().publicFavorites);
29         dispatch(snackbarActions.OPEN_SNACKBAR({
30             message: isPublicFavorite
31                 ? "Removing from public favorites..."
32                 : "Adding to public favorites...",
33             kind: SnackbarKind.INFO
34         }));
35
36         const promise: any = isPublicFavorite
37             ? services.favoriteService.delete({ userUuid: uuid, resourceUuid: resource.uuid })
38             : services.favoriteService.create({ userUuid: uuid, resource });
39
40         return promise
41             .then(() => {
42                 dispatch(publicFavoritesActions.UPDATE_PUBLIC_FAVORITES({ [resource.uuid]: !isPublicFavorite }));
43                 dispatch(snackbarActions.CLOSE_SNACKBAR());
44                 dispatch(snackbarActions.OPEN_SNACKBAR({
45                     message: isPublicFavorite
46                         ? "Removed from public favorites"
47                         : "Added to public favorites",
48                     hideDuration: 2000,
49                     kind: SnackbarKind.SUCCESS
50                 }));
51                 dispatch(progressIndicatorActions.STOP_WORKING("togglePublicFavorite"));
52                 dispatch<any>(loadPublicFavoritesTree())
53             })
54             .catch((e: any) => {
55                 dispatch(progressIndicatorActions.STOP_WORKING("togglePublicFavorite"));
56                 throw e;
57             });
58     };
59
60 export const updatePublicFavorites = (resourceUuids: string[]) =>
61     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
62         const uuidPrefix = getState().auth.config.uuidPrefix;
63         const uuid = `${uuidPrefix}-j7d0g-publicfavorites`;
64         dispatch(publicFavoritesActions.CHECK_PRESENCE_IN_PUBLIC_FAVORITES(resourceUuids));
65         services.favoriteService
66             .checkPresenceInFavorites(uuid, resourceUuids)
67             .then((results: any) => {
68                 dispatch(publicFavoritesActions.UPDATE_PUBLIC_FAVORITES(results));
69             });
70     };
71
72 export const getIsAdmin = () =>
73     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
74         const resource = getState().auth.user!.isAdmin;
75         return resource;
76     };