aretherecookies-mobile/js/enhancers/imagesEnhancers.js

40 lines
962 B
JavaScript
Raw Normal View History

2018-04-08 10:54:29 -05:00
// @flow
2019-09-21 15:45:07 +00:00
import { withProps } from "recompose";
import { emit } from "../streams/ImagesStream";
import { addImage, getImages } from "../apis/ImagesApi";
import AuthManager from "../AuthManager";
import { map } from "ramda";
2018-04-08 10:54:29 -05:00
export const withImages = withProps({
addImage: async ({ foodItemId, imageUri }) => {
try {
if (!AuthManager.user) {
2019-09-21 15:45:07 +00:00
throw new Error("You need to be logged in to add images");
2018-04-08 10:54:29 -05:00
}
const username = AuthManager.user.name;
const { url, date } = await addImage({ foodItemId, username, imageUri });
emit({
url,
username,
date,
2019-09-21 15:45:07 +00:00
food_item_id: foodItemId
2018-04-08 10:54:29 -05:00
});
} catch (error) {
// TODO error handling in the UI
2019-09-21 15:45:07 +00:00
console.error(error); // eslint-disable-line
2018-04-08 10:54:29 -05:00
}
},
getImages: async ({ foodItemId }) => {
try {
const images = await getImages(foodItemId);
map(emit, images);
} catch (error) {
// TODO error handling in the UI
2019-09-21 15:45:07 +00:00
console.error(error); // eslint-disable-line
2018-04-08 10:54:29 -05:00
}
2019-09-21 15:45:07 +00:00
}
2018-04-08 10:54:29 -05:00
});