// Copyright (C) The Arvados Authors. All rights reserved. // // SPDX-License-Identifier: AGPL-3.0 import { unionize, ofType, UnionOf } from "unionize"; import { Dispatch } from "redux"; import { favoriteService } from "../../services/services"; import { RootState } from "../store"; import { checkFavorite } from "./favorites-reducer"; export const favoritesActions = unionize({ TOGGLE_FAVORITE: ofType<{ resourceUuid: string }>(), CHECK_PRESENCE_IN_FAVORITES: ofType(), UPDATE_FAVORITES: ofType>() }, { tag: 'type', value: 'payload' }); export type FavoritesAction = UnionOf; export const toggleFavorite = (resource: { uuid: string; name: string }) => (dispatch: Dispatch, getState: () => RootState) => { const userUuid = getState().auth.user!.uuid; dispatch(favoritesActions.TOGGLE_FAVORITE({ resourceUuid: resource.uuid })); const isFavorite = checkFavorite(resource.uuid, getState().favorites); const promise = isFavorite ? favoriteService.delete({ userUuid, resourceUuid: resource.uuid }) : favoriteService.create({ userUuid, resource }); promise .then(fav => { dispatch(favoritesActions.UPDATE_FAVORITES({ [resource.uuid]: !isFavorite })); }); }; export const checkPresenceInFavorites = (resourceUuids: string[]) => (dispatch: Dispatch, getState: () => RootState) => { const userUuid = getState().auth.user!.uuid; dispatch(favoritesActions.CHECK_PRESENCE_IN_FAVORITES(resourceUuids)); favoriteService .checkPresenceInFavorites(userUuid, resourceUuids) .then(results => { dispatch(favoritesActions.UPDATE_FAVORITES(results)); }); };