Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / store / favorites / 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 { getUserUuid } from "common/getuser";
9 import { checkFavorite } from "./favorites-reducer";
10 import { snackbarActions, SnackbarKind } from "../snackbar/snackbar-actions";
11 import { ServiceRepository } from "services/services";
12 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
13 import { MultiSelectMenuActionNames } from "views-components/multiselect-toolbar/ms-menu-actions";
14 import { addDisabledButton, removeDisabledButton } from "store/multiselect/multiselect-actions";
15 import { loadFavoritesTree} from "store/side-panel-tree/side-panel-tree-actions";
16
17 export const favoritesActions = unionize({
18     TOGGLE_FAVORITE: ofType<{ resourceUuid: string }>(),
19     CHECK_PRESENCE_IN_FAVORITES: ofType<string[]>(),
20     UPDATE_FAVORITES: ofType<Record<string, boolean>>()
21 });
22
23 export type FavoritesAction = UnionOf<typeof favoritesActions>;
24
25 export const toggleFavorite = (resource: { uuid: string; name: string }) =>
26     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
27         const userUuid = getUserUuid(getState());
28         if (!userUuid) {
29             return Promise.reject("No user");
30         }
31         dispatch(progressIndicatorActions.START_WORKING("toggleFavorite"));
32         dispatch<any>(addDisabledButton(MultiSelectMenuActionNames.ADD_TO_FAVORITES))
33         dispatch(favoritesActions.TOGGLE_FAVORITE({ resourceUuid: resource.uuid }));
34         const isFavorite = checkFavorite(resource.uuid, getState().favorites);
35         dispatch(snackbarActions.OPEN_SNACKBAR({
36             message: isFavorite
37                 ? "Removing from favorites..."
38                 : "Adding to favorites...",
39             kind: SnackbarKind.INFO
40         }));
41
42         const promise: any = isFavorite
43             ? services.favoriteService.delete({ userUuid, resourceUuid: resource.uuid })
44             : services.favoriteService.create({ userUuid, resource });
45
46         return promise
47             .then(() => {
48                 dispatch(favoritesActions.UPDATE_FAVORITES({ [resource.uuid]: !isFavorite }));
49                 dispatch(snackbarActions.CLOSE_SNACKBAR());
50                 dispatch(snackbarActions.OPEN_SNACKBAR({
51                     message: isFavorite
52                         ? "Removed from favorites"
53                         : "Added to favorites",
54                     hideDuration: 2000,
55                     kind: SnackbarKind.SUCCESS
56                 }));
57                 dispatch<any>(removeDisabledButton(MultiSelectMenuActionNames.ADD_TO_FAVORITES))
58                 dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
59                 dispatch<any>(loadFavoritesTree())
60             })
61             .catch((e: any) => {
62                 dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
63                 throw e;
64             });
65     };
66
67 export const updateFavorites = (resourceUuids: string[]) =>
68     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
69         const userUuid = getUserUuid(getState());
70         if (!userUuid) { return; }
71         dispatch(favoritesActions.CHECK_PRESENCE_IN_FAVORITES(resourceUuids));
72         services.favoriteService
73             .checkPresenceInFavorites(userUuid, resourceUuids)
74             .then((results: any) => {
75                 dispatch(favoritesActions.UPDATE_FAVORITES(results));
76             });
77     };