X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/c632f0edf3f863fd29efb90e010f286b40cb9e92..1a59e5dcc15a9a1aebfd15a08903fcb75efd2aac:/src/store/favorites/favorites-actions.ts diff --git a/src/store/favorites/favorites-actions.ts b/src/store/favorites/favorites-actions.ts index eb4f6490..5a3001fb 100644 --- a/src/store/favorites/favorites-actions.ts +++ b/src/store/favorites/favorites-actions.ts @@ -2,30 +2,37 @@ // // 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 { snackbarActions, SnackbarKind } from "../snackbar/snackbar-actions"; +import { ServiceRepository } from "~/services/services"; +import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions"; 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 => { + dispatch(progressIndicatorActions.START_WORKING("toggleFavorite")); 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); + dispatch(snackbarActions.OPEN_SNACKBAR({ + message: isFavorite + ? "Removing from favorites..." + : "Adding to favorites..." + })); + const promise: any = isFavorite - ? favoriteService.delete({ userUuid, resourceUuid: resource.uuid }) - : favoriteService.create({ userUuid, resource }); + ? services.favoriteService.delete({ userUuid, resourceUuid: resource.uuid }) + : services.favoriteService.create({ userUuid, resource }); return promise .then(() => { @@ -35,19 +42,24 @@ export const toggleFavorite = (resource: { uuid: string; name: string }) => message: isFavorite ? "Removed from favorites" : "Added to favorites", - hideDuration: 2000 + hideDuration: 2000, + kind: SnackbarKind.SUCCESS })); + dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite")); + }) + .catch((e: any) => { + dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite")); + throw e; }); }; -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)); }); }; -