1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { unionize, ofType, UnionOf } from "unionize";
6 import { Dispatch } from "redux";
7 import { favoriteService } from "../../services/services";
8 import { RootState } from "../store";
9 import { checkFavorite } from "./favorites-reducer";
11 export const favoritesActions = unionize({
12 TOGGLE_FAVORITE: ofType<{ resourceUuid: string }>(),
13 CHECK_PRESENCE_IN_FAVORITES: ofType<string[]>(),
14 UPDATE_FAVORITES: ofType<Record<string, boolean>>()
15 }, { tag: 'type', value: 'payload' });
17 export type FavoritesAction = UnionOf<typeof favoritesActions>;
19 export const toggleFavorite = (resource: { uuid: string; name: string }) =>
20 (dispatch: Dispatch, getState: () => RootState): Promise<any> => {
21 const userUuid = getState().auth.user!.uuid;
22 dispatch(favoritesActions.TOGGLE_FAVORITE({ resourceUuid: resource.uuid }));
23 const isFavorite = checkFavorite(resource.uuid, getState().favorites);
24 const promise = isFavorite
25 ? favoriteService.delete({ userUuid, resourceUuid: resource.uuid })
26 : favoriteService.create({ userUuid, resource });
30 dispatch(favoritesActions.UPDATE_FAVORITES({ [resource.uuid]: !isFavorite }));
34 export const checkPresenceInFavorites = (resourceUuids: string[]) =>
35 (dispatch: Dispatch, getState: () => RootState) => {
36 const userUuid = getState().auth.user!.uuid;
37 dispatch(favoritesActions.CHECK_PRESENCE_IN_FAVORITES(resourceUuids));
39 .checkPresenceInFavorites(userUuid, resourceUuids)
41 dispatch(favoritesActions.UPDATE_FAVORITES(results));