X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/98237b19875f6437fa1e01845ad9c911d886cf04..6149700b801fc2cdb83b0e700bbb319850ed471d:/src/store/favorites/favorites-actions.ts diff --git a/src/store/favorites/favorites-actions.ts b/src/store/favorites/favorites-actions.ts index 4a176e303c..e5a8e591d2 100644 --- a/src/store/favorites/favorites-actions.ts +++ b/src/store/favorites/favorites-actions.ts @@ -2,43 +2,51 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { unionize, ofType, UnionOf } from "unionize"; +import { unionize, ofType, UnionOf } from "~/common/unionize"; import { Dispatch } from "redux"; -import { favoriteService } from "../../services/services"; import { RootState } from "../store"; import { checkFavorite } from "./favorites-reducer"; +import { snackbarActions } from "../snackbar/snackbar-actions"; +import { ServiceRepository } from "~/services/services"; 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): Promise => { + (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise => { const userUuid = getState().auth.user!.uuid; dispatch(favoritesActions.TOGGLE_FAVORITE({ resourceUuid: resource.uuid })); + dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Working..." })); const isFavorite = checkFavorite(resource.uuid, getState().favorites); - const promise = isFavorite - ? favoriteService.delete({ userUuid, resourceUuid: resource.uuid }) - : favoriteService.create({ userUuid, resource }); + const promise: any = isFavorite + ? services.favoriteService.delete({ userUuid, resourceUuid: resource.uuid }) + : services.favoriteService.create({ userUuid, resource }); return promise - .then(fav => { + .then(() => { dispatch(favoritesActions.UPDATE_FAVORITES({ [resource.uuid]: !isFavorite })); + dispatch(snackbarActions.CLOSE_SNACKBAR()); + dispatch(snackbarActions.OPEN_SNACKBAR({ + message: isFavorite + ? "Removed from favorites" + : "Added to favorites", + hideDuration: 2000 + })); }); }; -export const checkPresenceInFavorites = (resourceUuids: string[]) => - (dispatch: Dispatch, getState: () => RootState) => { +export const updateFavorites = (resourceUuids: string[]) => + (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { const userUuid = getState().auth.user!.uuid; dispatch(favoritesActions.CHECK_PRESENCE_IN_FAVORITES(resourceUuids)); - favoriteService + services.favoriteService .checkPresenceInFavorites(userUuid, resourceUuids) - .then(results => { + .then((results: any) => { dispatch(favoritesActions.UPDATE_FAVORITES(results)); }); }; -