UI Improvements
[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 { checkFavorite } from "./favorites-reducer";
9 import { snackbarActions, SnackbarKind } from "../snackbar/snackbar-actions";
10 import { ServiceRepository } from "~/services/services";
11 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
12
13 export const favoritesActions = unionize({
14     TOGGLE_FAVORITE: ofType<{ resourceUuid: string }>(),
15     CHECK_PRESENCE_IN_FAVORITES: ofType<string[]>(),
16     UPDATE_FAVORITES: ofType<Record<string, boolean>>()
17 });
18
19 export type FavoritesAction = UnionOf<typeof favoritesActions>;
20
21 export const toggleFavorite = (resource: { uuid: string; name: string }) =>
22     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
23         dispatch(progressIndicatorActions.START_WORKING("toggleFavorite"));
24         const userUuid = getState().auth.user!.uuid;
25         dispatch(favoritesActions.TOGGLE_FAVORITE({ resourceUuid: resource.uuid }));
26         const isFavorite = checkFavorite(resource.uuid, getState().favorites);
27         dispatch(snackbarActions.OPEN_SNACKBAR({
28             message: isFavorite
29                 ? "Removing from favorites..."
30                 : "Adding to favorites...",
31             kind: SnackbarKind.INFO
32         }));
33
34         const promise: any = isFavorite
35             ? services.favoriteService.delete({ userUuid, resourceUuid: resource.uuid })
36             : services.favoriteService.create({ userUuid, resource });
37
38         return promise
39             .then(() => {
40                 dispatch(favoritesActions.UPDATE_FAVORITES({ [resource.uuid]: !isFavorite }));
41                 dispatch(snackbarActions.CLOSE_SNACKBAR());
42                 dispatch(snackbarActions.OPEN_SNACKBAR({
43                     message: isFavorite
44                         ? "Removed from favorites"
45                         : "Added to favorites",
46                     hideDuration: 2000,
47                     kind: SnackbarKind.SUCCESS
48                 }));
49                 dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
50             })
51             .catch((e: any) => {
52                 dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
53                 throw e;
54             });
55     };
56
57 export const updateFavorites = (resourceUuids: string[]) =>
58     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
59         const userUuid = getState().auth.user!.uuid;
60         dispatch(favoritesActions.CHECK_PRESENCE_IN_FAVORITES(resourceUuids));
61         services.favoriteService
62             .checkPresenceInFavorites(userUuid, resourceUuids)
63             .then((results: any) => {
64                 dispatch(favoritesActions.UPDATE_FAVORITES(results));
65             });
66     };