api-response
[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         }));
32
33         const promise: any = isFavorite
34             ? services.favoriteService.delete({ userUuid, resourceUuid: resource.uuid })
35             : services.favoriteService.create({ userUuid, resource });
36
37         return promise
38             .then(() => {
39                 dispatch(favoritesActions.UPDATE_FAVORITES({ [resource.uuid]: !isFavorite }));
40                 dispatch(snackbarActions.CLOSE_SNACKBAR());
41                 dispatch(snackbarActions.OPEN_SNACKBAR({
42                     message: isFavorite
43                         ? "Removed from favorites"
44                         : "Added to favorites",
45                     hideDuration: 2000,
46                     kind: SnackbarKind.SUCCESS
47                 }));
48                 dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
49             })
50             .catch((e: any) => {
51                 dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
52                 throw e;
53             });
54     };
55
56 export const updateFavorites = (resourceUuids: string[]) =>
57     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
58         const userUuid = getState().auth.user!.uuid;
59         dispatch(favoritesActions.CHECK_PRESENCE_IN_FAVORITES(resourceUuids));
60         services.favoriteService
61             .checkPresenceInFavorites(userUuid, resourceUuids)
62             .then((results: any) => {
63                 dispatch(favoritesActions.UPDATE_FAVORITES(results));
64             });
65     };