+export const toggleFavorite = (resource: { uuid: string; name: string }) =>
+ (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
+ dispatch(progressIndicatorActions.START_WORKING("toggleFavorite"));
+ const userUuid = getState().auth.user!.uuid;
+ dispatch(favoritesActions.TOGGLE_FAVORITE({ resourceUuid: resource.uuid }));
+ const isFavorite = checkFavorite(resource.uuid, getState().favorites);
+ dispatch(snackbarActions.OPEN_SNACKBAR({
+ message: isFavorite
+ ? "Removing from favorites..."
+ : "Adding to favorites...",
+ kind: SnackbarKind.INFO
+ }));
+
+ const promise: any = isFavorite
+ ? services.favoriteService.delete({ userUuid, resourceUuid: resource.uuid })
+ : services.favoriteService.create({ userUuid, resource });
+
+ 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,
+ kind: SnackbarKind.SUCCESS
+ }));
+ dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
+ })
+ .catch((e: any) => {
+ dispatch(progressIndicatorActions.STOP_WORKING("toggleFavorite"));
+ throw e;
+ });
+ };
+
+export const updateFavorites = (resourceUuids: string[]) =>
+ (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+ const userUuid = getState().auth.user!.uuid;