Refactor to apply global navigation actions
[arvados-workbench2.git] / src / store / favorites / favorites-actions.ts
index c38f4d1a1aa2690779692ff70e51c2cf1e276bfa..57eecf8f54d1ec51f24714d4e4762b88d014a4f6 100644 (file)
@@ -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,28 +18,35 @@ export const favoritesActions = unionize({
 export type FavoritesAction = UnionOf<typeof favoritesActions>;
 
 export const toggleFavorite = (resource: { uuid: string; name: string }) =>
-    (dispatch: Dispatch, getState: () => RootState) => {
+    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
         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: (any) = 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
+        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) => {
+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));
             });
     };
-