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