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..."
33 const promise: any = isFavorite
34 ? services.favoriteService.delete({ userUuid, resourceUuid: resource.uuid })
35 : services.favoriteService.create({ userUuid, resource });
39 dispatch(favoritesActions.UPDATE_FAVORITES({ [resource.uuid]: !isFavorite }));
40 dispatch(snackbarActions.CLOSE_SNACKBAR());
41 dispatch(snackbarActions.OPEN_SNACKBAR({
43 ? "Removed from favorites"
44 : "Added to favorites",
46 kind: SnackbarKind.SUCCESS
48 dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
51 dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
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));