Add navigation fro favorites panel, load favorites panel after page refresh
[arvados.git] / src / store / favorites / favorites-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { unionize, ofType, UnionOf } from "unionize";
6 import { Dispatch } from "redux";
7 import { favoriteService } from "../../services/services";
8 import { RootState } from "../store";
9 import { checkFavorite } from "./favorites-reducer";
10
11 export const favoritesActions = unionize({
12     TOGGLE_FAVORITE: ofType<{ resourceUuid: string }>(),
13     CHECK_PRESENCE_IN_FAVORITES: ofType<string[]>(),
14     UPDATE_FAVORITES: ofType<Record<string, boolean>>()
15 }, { tag: 'type', value: 'payload' });
16
17 export type FavoritesAction = UnionOf<typeof favoritesActions>;
18
19 export const toggleFavorite = (resource: { uuid: string; name: string }) =>
20     (dispatch: Dispatch, getState: () => RootState): Promise<any> => {
21         const userUuid = getState().auth.user!.uuid;
22         dispatch(favoritesActions.TOGGLE_FAVORITE({ resourceUuid: resource.uuid }));
23         const isFavorite = checkFavorite(resource.uuid, getState().favorites);
24         const promise = isFavorite
25             ? favoriteService.delete({ userUuid, resourceUuid: resource.uuid })
26             : favoriteService.create({ userUuid, resource });
27
28         return promise
29             .then(fav => {
30                 dispatch(favoritesActions.UPDATE_FAVORITES({ [resource.uuid]: !isFavorite }));
31             });
32     };
33
34 export const checkPresenceInFavorites = (resourceUuids: string[]) =>
35     (dispatch: Dispatch, getState: () => RootState) => {
36         const userUuid = getState().auth.user!.uuid;
37         dispatch(favoritesActions.CHECK_PRESENCE_IN_FAVORITES(resourceUuids));
38         favoriteService
39             .checkPresenceInFavorites(userUuid, resourceUuids)
40             .then(results => {
41                 dispatch(favoritesActions.UPDATE_FAVORITES(results));
42             });
43     };
44