2018-04-08 10:54:29 -05:00
|
|
|
// @flow
|
2020-04-17 22:50:23 -05: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({
|
2020-04-17 22:50:23 -05:00
|
|
|
addImage: async ({ productId, imageUri }) => {
|
2018-04-08 10:54:29 -05:00
|
|
|
try {
|
|
|
|
|
if (!AuthManager.user) {
|
2020-04-17 22:50:23 -05: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;
|
|
|
|
|
|
2020-04-17 22:50:23 -05:00
|
|
|
const { url, date } = await addImage({ productId, username, imageUri });
|
2018-04-08 10:54:29 -05:00
|
|
|
|
|
|
|
|
emit({
|
|
|
|
|
url,
|
|
|
|
|
username,
|
|
|
|
|
date,
|
2020-04-17 22:50:23 -05:00
|
|
|
food_item_id: productId,
|
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
|
|
|
}
|
|
|
|
|
},
|
2020-04-17 22:50:23 -05:00
|
|
|
getImages: async ({ productId }) => {
|
2018-04-08 10:54:29 -05:00
|
|
|
try {
|
2020-04-17 22:50:23 -05:00
|
|
|
const images = await getImages(productId);
|
2018-04-08 10:54:29 -05:00
|
|
|
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
|
|
|
}
|
2020-04-17 22:50:23 -05:00
|
|
|
},
|
2018-04-08 10:54:29 -05:00
|
|
|
});
|