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