aretherecookies-mobile/js/enhancers/imagesEnhancers.js
2020-04-17 22:50:23 -05:00

39 lines
959 B
JavaScript

// @flow
import { withProps } from 'recompose';
import { emit } from '../streams/ImagesStream';
import { addImage, getImages } from '../apis/ImagesApi';
import AuthManager from '../AuthManager';
import { map } from 'ramda';
export const withImages = withProps({
addImage: async ({ productId, imageUri }) => {
try {
if (!AuthManager.user) {
throw new Error('You need to be logged in to add images');
}
const username = AuthManager.user.name;
const { url, date } = await addImage({ productId, username, imageUri });
emit({
url,
username,
date,
food_item_id: productId,
});
} catch (error) {
// TODO error handling in the UI
console.error(error); // eslint-disable-line
}
},
getImages: async ({ productId }) => {
try {
const images = await getImages(productId);
map(emit, images);
} catch (error) {
// TODO error handling in the UI
console.error(error); // eslint-disable-line
}
},
});