aretherecookies-mobile/js/pages/FoodItemDetail.js

136 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-04-01 20:53:37 -05:00
// @flow
import React, { Component } from 'react';
2017-04-23 20:15:46 -05:00
import { Image, View } from 'react-native';
2017-04-01 20:53:37 -05:00
import theme from '../ui-theme';
import { StrongText, SubText } from '../components/ItemTile';
import typeof FoodItemRecord from '../records/FoodItemRecord';
import { type Quantity } from '../constants/QuantityConstants';
2017-06-03 19:43:45 -05:00
import typeof PlaceRecord from '../records/PlaceRecord';
import { compose, pure } from 'recompose';
2017-04-14 12:28:48 -05:00
import IconButton from '../components/IconButton';
import { withFoodItem } from '../enhancers/foodItemEnhancers';
import { withPlaceForFoodItem } from '../enhancers/placeEnhancers';
2017-04-20 16:28:13 -05:00
import Carousel from 'react-native-looped-carousel';
2017-04-23 20:15:46 -05:00
import CountBadge from '../components/CountBadge';
import QuantityPicker from '../components/QuantityPicker';
import { routeWithTitle } from '../helpers/RouteHelpers';
import { Link } from 'react-router-native';
import moment from 'moment';
import { withUpdateQuantity } from '../enhancers/quantityEnhancers';
2017-04-14 12:28:48 -05:00
2017-04-23 20:15:46 -05:00
const { foodItemDetails: style } = theme;
2017-04-01 20:53:37 -05:00
2017-04-23 20:15:46 -05:00
const stretchedStyle = { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 };
const contentTileStyle = {
backgroundColor: 'white',
padding: 15,
marginBottom: 10,
2017-04-23 20:15:46 -05:00
};
2017-04-20 16:28:13 -05:00
2017-04-01 20:53:37 -05:00
export class FoodItemDetail extends Component {
2017-04-20 06:32:13 -05:00
static displayName = 'FoodItemDetail';
2017-04-01 20:53:37 -05:00
2017-04-20 06:32:13 -05:00
props: {
foodItem: FoodItemRecord,
2017-06-03 19:43:45 -05:00
place: PlaceRecord,
updateQuantity: ({ foodItemId: string, quantity: Quantity }) => void,
2017-04-20 06:32:13 -05:00
};
2017-04-01 20:53:37 -05:00
2017-04-20 06:32:13 -05:00
state: {
2017-04-20 16:28:13 -05:00
isFavorite: boolean,
currentImage: number,
2017-04-20 06:32:13 -05:00
};
2017-04-14 21:47:54 -05:00
2017-04-20 16:28:13 -05:00
state = {
currentImage: 0,
};
2017-04-14 21:47:54 -05:00
2017-04-23 20:15:46 -05:00
// TODO placeholder implementation until we get a backend
2017-04-20 16:28:13 -05:00
addToFaves = () =>
this.setState(prevState => ({
isFavorite: !prevState.isFavorite,
}));
2017-04-14 21:47:54 -05:00
updateAmount = (quantity: Quantity) => {
const { updateQuantity, foodItem: { id: foodItemId } } = this.props;
updateQuantity({ foodItemId, quantity });
};
2017-04-14 21:47:54 -05:00
2017-04-23 20:15:46 -05:00
// TODO
2017-04-20 06:32:13 -05:00
addPhoto = () => {};
2017-04-14 21:47:54 -05:00
2017-06-03 19:43:45 -05:00
changeCurrentImage = (currentImage: number) => this.setState({ currentImage });
2017-04-14 21:47:54 -05:00
2017-04-20 06:32:13 -05:00
render() {
const { foodItem, place } = this.props;
2017-07-23 19:58:10 -05:00
if (!foodItem || !place) {
return <View />;
}
2017-04-20 16:28:13 -05:00
const { images, quantity } = foodItem;
const viewableImages = images.filter(image => !!image);
2017-04-20 06:32:13 -05:00
return (
2017-04-20 16:28:13 -05:00
<View style={{ ...theme.page.container }}>
2017-04-20 08:32:57 -05:00
<View style={{ flex: 3 }}>
2017-11-11 20:15:19 -06:00
{viewableImages.size === 1 && (
<Image style={stretchedStyle} source={{ uri: viewableImages.get(0) }} />
)}
{viewableImages.size > 1 && (
2017-04-23 20:15:46 -05:00
<Carousel autoplay={false} onAnimateNextPage={this.changeCurrentImage} style={stretchedStyle}>
2017-11-11 20:15:19 -06:00
{viewableImages.map(uri => (
2017-04-20 16:28:13 -05:00
<Image key={uri} style={{ flex: 1, resizeMode: 'stretch' }} source={{ uri }} />
2017-11-11 20:15:19 -06:00
))}
</Carousel>
)}
2017-08-05 20:38:29 -05:00
<CountBadge currentImage={this.state.currentImage + 1} totalCount={viewableImages.size} />
2017-04-20 06:32:13 -05:00
</View>
<View style={{ ...contentTileStyle }}>
<Link
to={routeWithTitle(`/place/${place.id || ''}`, place.name)}
underlayColor={theme.itemTile.pressHighlightColor}
>
<View>
2017-11-11 20:15:19 -06:00
<StrongText>{place.name}</StrongText>
<SubText>{place.address}</SubText>
</View>
</Link>
2017-04-20 06:32:13 -05:00
</View>
2017-04-20 08:32:57 -05:00
<View
style={{
flex: 1,
2017-04-23 20:15:46 -05:00
...contentTileStyle,
2017-04-20 08:32:57 -05:00
}}
>
2017-06-11 18:32:21 -05:00
<QuantityPicker
quantity={quantity}
2017-06-11 18:32:21 -05:00
onValueChange={this.updateAmount}
style={{ flex: 1, marginBottom: 10 }}
2017-06-11 18:32:21 -05:00
/>
<SubText>Last updated at {moment(foodItem.lastupdated).format('h:mm A on MMM D, YYYY')}</SubText>
2017-04-20 08:32:57 -05:00
</View>
2017-04-23 20:15:46 -05:00
<View style={{ flex: 2, ...contentTileStyle }}>
<IconButton
glyph="stars"
text="Add to Faves"
onPress={this.addToFaves}
color={style.actionIconColor}
/>
<IconButton
glyph="insert-photo"
text="Add Photo"
onPress={this.addPhoto}
color={style.actionIconColor}
/>
2017-04-20 08:32:57 -05:00
</View>
2017-04-20 06:32:13 -05:00
</View>
);
}
2017-04-01 20:53:37 -05:00
}
const enhance = compose(pure, withFoodItem, withPlaceForFoodItem, withUpdateQuantity);
2017-04-01 20:53:37 -05:00
export default enhance(FoodItemDetail);