Implement action for toggling favorite status
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 23 Jul 2018 22:12:35 +0000 (00:12 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 23 Jul 2018 22:12:35 +0000 (00:12 +0200)
Feature #13784

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/store/favorites/favorites-actions.ts
src/store/favorites/favorites-reducer.ts
src/store/project/project-action.ts
src/views-components/context-menu/action-sets/project-action-set.ts

index d7e14a2bfbc2f7898f8f7438122ca92dbcd480de..ddb1492fa159dfc192195fc9d27fb0a5e6934368 100644 (file)
@@ -5,16 +5,35 @@
 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<string[]>(),
     UPDATE_FAVORITES: ofType<Record<string, boolean>>()
 }, { tag: 'type', value: 'payload' });
 
 export type FavoritesAction = UnionOf<typeof favoritesActions>;
 
-export const checkPresenceInFavorites = (userUuid: string, resourceUuids: string[]) =>
-    (dispatch: Dispatch) => {
+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)
index 9599db078551d3cacf0778931787293f22d88e43..e38ea954e1fde469919f5f8de5eafe72bc5fb70f 100644 (file)
@@ -12,4 +12,4 @@ export const favoritesReducer = (state: FavoritesState = {}, action: FavoritesAc
         default: () => state
     });
 
-export const isFavorite = (uuid: string, state: FavoritesState) => state[uuid] === true;
\ No newline at end of file
+export const checkFavorite = (uuid: string, state: FavoritesState) => state[uuid] === true;
\ No newline at end of file
index dae245cf97c9fcf70e615b71c69afa24435dd538..77223e9e41cd00cc31c073691ac3d2cb50fa7b5c 100644 (file)
@@ -35,8 +35,7 @@ export const getProjectList = (parentUuid: string = '') => (dispatch: Dispatch,
             .addEqual("ownerUuid", parentUuid)
     }).then(({ items: projects }) => {
         dispatch(projectActions.PROJECTS_SUCCESS({ projects, parentItemId: parentUuid }));
-        const { user } = getState().auth;
-        dispatch<any>(checkPresenceInFavorites(user ? user.uuid : "", projects.map(project => project.uuid)));
+        dispatch<any>(checkPresenceInFavorites(projects.map(project => project.uuid)));
         return projects;
     });
 };
index 2b3364913aa47d60f234fcb90a63ce155a2f75f4..e0284e85ef670293b63271948909bd0041d899f5 100644 (file)
@@ -6,6 +6,7 @@ import { ContextMenuActionSet } from "../context-menu-action-set";
 import { projectActions } from "../../../store/project/project-action";
 import { ShareIcon, NewProjectIcon } from "../../../components/icon/icon";
 import { FavoriteActionText, FavoriteActionIcon } from "./favorite-action";
+import { favoritesActions, toggleFavorite } from "../../../store/favorites/favorites-actions";
 
 export const projectActionSet: ContextMenuActionSet = [[{
     icon: NewProjectIcon,
@@ -16,5 +17,7 @@ export const projectActionSet: ContextMenuActionSet = [[{
 }, {
     name: FavoriteActionText,
     icon: FavoriteActionIcon,
-    execute: (dispatch, resource) => { alert(resource); }
+    execute: (dispatch, resource) => {
+        dispatch<any>(toggleFavorite(resource.uuid));
+    }
 }]];