1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
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>>()
19 export type FavoritesAction = UnionOf<typeof favoritesActions>;
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({
29 ? "Removing from favorites..."
30 : "Adding to favorites...",
31 kind: SnackbarKind.INFO
34 const promise: any = isFavorite
35 ? services.favoriteService.delete({ userUuid, resourceUuid: resource.uuid })
36 : services.favoriteService.create({ userUuid, resource });
40 dispatch(favoritesActions.UPDATE_FAVORITES({ [resource.uuid]: !isFavorite }));
41 dispatch(snackbarActions.CLOSE_SNACKBAR());
42 dispatch(snackbarActions.OPEN_SNACKBAR({
44 ? "Removed from favorites"
45 : "Added to favorites",
47 kind: SnackbarKind.SUCCESS
49 dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
52 dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
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));