19231: Add smaller page sizes (10 and 20 items) to load faster
[arvados-workbench2.git] / src / store / public-favorites / public-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 "common/unionize";
6 import { Dispatch } from "redux";
7 import { RootState } from "../store";
8 import { checkPublicFavorite } from "./public-favorites-reducer";
9 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
10 import { ServiceRepository } from "services/services";
11 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
12
13 export const publicFavoritesActions = unionize({
14     TOGGLE_PUBLIC_FAVORITE: ofType<{ resourceUuid: string }>(),
15     CHECK_PRESENCE_IN_PUBLIC_FAVORITES: ofType<string[]>(),
16     UPDATE_PUBLIC_FAVORITES: ofType<Record<string, boolean>>()
17 });
18
19 export type PublicFavoritesAction = UnionOf<typeof publicFavoritesActions>;
20
21 export const togglePublicFavorite = (resource: { uuid: string; name: string }) =>
22     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
23         dispatch(progressIndicatorActions.START_WORKING("togglePublicFavorite"));
24         const uuidPrefix = getState().auth.config.uuidPrefix;
25         const uuid = `${uuidPrefix}-j7d0g-publicfavorites`;
26         dispatch(publicFavoritesActions.TOGGLE_PUBLIC_FAVORITE({ resourceUuid: resource.uuid }));
27         const isPublicFavorite = checkPublicFavorite(resource.uuid, getState().publicFavorites);
28         dispatch(snackbarActions.OPEN_SNACKBAR({
29             message: isPublicFavorite
30                 ? "Removing from public favorites..."
31                 : "Adding to public favorites...",
32             kind: SnackbarKind.INFO
33         }));
34
35         const promise: any = isPublicFavorite
36             ? services.favoriteService.delete({ userUuid: uuid, resourceUuid: resource.uuid })
37             : services.favoriteService.create({ userUuid: uuid, resource });
38
39         return promise
40             .then(() => {
41                 dispatch(publicFavoritesActions.UPDATE_PUBLIC_FAVORITES({ [resource.uuid]: !isPublicFavorite }));
42                 dispatch(snackbarActions.CLOSE_SNACKBAR());
43                 dispatch(snackbarActions.OPEN_SNACKBAR({
44                     message: isPublicFavorite
45                         ? "Removed from public favorites"
46                         : "Added to public favorites",
47                     hideDuration: 2000,
48                     kind: SnackbarKind.SUCCESS
49                 }));
50                 dispatch(progressIndicatorActions.STOP_WORKING("togglePublicFavorite"));
51             })
52             .catch((e: any) => {
53                 dispatch(progressIndicatorActions.STOP_WORKING("togglePublicFavorite"));
54                 throw e;
55             });
56     };
57
58 export const updatePublicFavorites = (resourceUuids: string[]) =>
59     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
60         const uuidPrefix = getState().auth.config.uuidPrefix;
61         const uuid = `${uuidPrefix}-j7d0g-publicfavorites`;
62         dispatch(publicFavoritesActions.CHECK_PRESENCE_IN_PUBLIC_FAVORITES(resourceUuids));
63         services.favoriteService
64             .checkPresenceInFavorites(uuid, resourceUuids)
65             .then((results: any) => {
66                 dispatch(publicFavoritesActions.UPDATE_PUBLIC_FAVORITES(results));
67             });
68     };
69
70 export const getIsAdmin = () =>
71     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72         const resource = getState().auth.user!.isAdmin;
73         return resource;
74     };