X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/39c6f239172d5f2d92341f6c31de32e7c02ffa2d..f239b9e88677d82a48b6b565ab2fd407d1171729:/src/store/favorites/favorites-actions.ts diff --git a/src/store/favorites/favorites-actions.ts b/src/store/favorites/favorites-actions.ts index 225c9b35..38229dff 100644 --- a/src/store/favorites/favorites-actions.ts +++ b/src/store/favorites/favorites-actions.ts @@ -4,9 +4,10 @@ import { unionize, ofType, UnionOf } from "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 }>(), @@ -17,27 +18,35 @@ export const favoritesActions = unionize({ export type FavoritesAction = UnionOf; export const toggleFavorite = (resource: { uuid: string; name: string }) => - (dispatch: Dispatch, getState: () => RootState) => { + (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 }); - promise - .then(fav => { + return promise + .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) => { + (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)); }); };