-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 });
+export const toggleFavorite = (resource: { uuid: string; name: string }) =>
+ (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
+ const userUuid = getUserUuid(getState());
+ if (!userUuid) {
+ return Promise.reject("No user");
+ }
+ dispatch(progressIndicatorActions.START_WORKING("toggleFavorite"));
+ 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
+ }));