1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 import { getResource } from '~/store/resources/resources';
13 import { LinkResource } from "~/models/link";
15 export const publicFavoritesActions = unionize({
16 TOGGLE_PUBLIC_FAVORITE: ofType<{ resourceUuid: string }>(),
17 CHECK_PRESENCE_IN_PUBLIC_FAVORITES: ofType<string[]>(),
18 UPDATE_PUBLIC_FAVORITES: ofType<Record<string, boolean>>()
21 export type PublicFavoritesAction = UnionOf<typeof publicFavoritesActions>;
23 export const togglePublicFavorite = (resource: { uuid: string; name: string }) =>
24 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
25 dispatch(progressIndicatorActions.START_WORKING("togglePublicFavorite"));
26 const uuidPrefix = getState().config.uuidPrefix;
27 const uuid = `${uuidPrefix}-j7d0g-fffffffffffffff`;
28 dispatch(publicFavoritesActions.TOGGLE_PUBLIC_FAVORITE({ resourceUuid: resource.uuid }));
29 const isPublicFavorite = checkPublicFavorite(resource.uuid, getState().publicFavorites);
30 dispatch(snackbarActions.OPEN_SNACKBAR({
31 message: isPublicFavorite
32 ? "Removing from public favorites..."
33 : "Adding to public favorites...",
34 kind: SnackbarKind.INFO
37 const promise: any = isPublicFavorite
38 ? services.favoriteService.delete({ userUuid: uuid, resourceUuid: resource.uuid })
39 : services.favoriteService.create({ userUuid: uuid, resource });
43 dispatch(publicFavoritesActions.UPDATE_PUBLIC_FAVORITES({ [resource.uuid]: !isPublicFavorite }));
44 dispatch(snackbarActions.CLOSE_SNACKBAR());
45 dispatch(snackbarActions.OPEN_SNACKBAR({
46 message: isPublicFavorite
47 ? "Removed from public favorites"
48 : "Added to public favorites",
50 kind: SnackbarKind.SUCCESS
52 dispatch(progressIndicatorActions.STOP_WORKING("togglePublicFavorite"));
55 dispatch(progressIndicatorActions.STOP_WORKING("togglePublicFavorite"));
60 export const updatePublicFavorites = (resourceUuids: string[]) =>
61 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
62 const uuidPrefix = getState().config.uuidPrefix;
63 const uuid = `${uuidPrefix}-j7d0g-fffffffffffffff`;
64 dispatch(publicFavoritesActions.CHECK_PRESENCE_IN_PUBLIC_FAVORITES(resourceUuids));
65 services.favoriteService
66 .checkPresenceInFavorites(uuid, resourceUuids)
67 .then((results: any) => {
68 dispatch(publicFavoritesActions.UPDATE_PUBLIC_FAVORITES(results));
72 export const getHeadUuid = (uuid: string) =>
73 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
74 const resource = getResource<LinkResource>(uuid)(getState().resources);
75 return resource!.headUuid;
78 export const getIsAdmin = () =>
79 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
80 const resource = getState().auth.user!.isAdmin;