// Copyright (C) The Arvados Authors. All rights reserved. // // SPDX-License-Identifier: AGPL-3.0 import { unionize, ofType, UnionOf } from "unionize"; import { Dispatch } from "../../../node_modules/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 = (resourceUuid: string) => (dispatch: Dispatch, getState: () => RootState) => { const userUuid = getState().auth.user!.uuid; dispatch(favoritesActions.TOGGLE_FAVORITE({ resourceUuid })); const isFavorite = checkFavorite(resourceUuid, getState().favorites); const promise = isFavorite ? favoriteService.delete({ userUuid, resourceUuid }) : favoriteService.create({ userUuid, resourceUuid }); promise .then(fav => { dispatch(favoritesActions.UPDATE_FAVORITES({ [resourceUuid]: !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)); }); };