2017-09-03 19:41:52 -05:00
|
|
|
// @flow
|
2019-09-21 15:45:07 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import { View, Text, ScrollView, RefreshControl } from "react-native";
|
|
|
|
|
import FoodItemTile from "../components/FoodItemTile";
|
|
|
|
|
import FoodItemList from "../components/FoodItemList";
|
|
|
|
|
import typeof FoodItemRecord from "../records/FoodItemRecord";
|
|
|
|
|
import { withFoodItemsAsSeq } from "../enhancers/foodItemEnhancers";
|
|
|
|
|
import { type SetSeq } from "immutable";
|
|
|
|
|
import { compose, pure, withState, withHandlers, lifecycle } from "recompose";
|
|
|
|
|
import { withFilter } from "../enhancers/filterEnhancers";
|
|
|
|
|
import { withRouterContext } from "../enhancers/routeEnhancers";
|
|
|
|
|
import FullScreenMessage from "../components/FullScreenMessage";
|
2017-09-03 19:41:52 -05:00
|
|
|
|
2018-08-25 10:12:52 -05:00
|
|
|
type Props = {
|
|
|
|
|
foodItemsSeq: SetSeq<FoodItemRecord>,
|
|
|
|
|
isRefreshing: boolean,
|
|
|
|
|
onRefresh: () => Promise<any>,
|
|
|
|
|
onPulldown: () => {},
|
2019-09-21 15:45:07 +00:00
|
|
|
isFilterDirty: boolean
|
2018-08-25 10:12:52 -05:00
|
|
|
};
|
2017-09-03 19:41:52 -05:00
|
|
|
|
2018-08-25 10:12:52 -05:00
|
|
|
const FoodList = (props: Props) => {
|
2019-06-22 15:48:52 +00:00
|
|
|
const { foodItemsSeq, isRefreshing, onPulldown, isFilterDirty } = props;
|
2017-09-03 19:41:52 -05:00
|
|
|
|
2018-08-25 10:12:52 -05:00
|
|
|
const refreshing = isRefreshing || !foodItemsSeq;
|
|
|
|
|
const showNoResults = !isRefreshing && isFilterDirty && foodItemsSeq && !foodItemsSeq.size;
|
2018-07-21 14:21:15 -05:00
|
|
|
|
2018-08-25 10:12:52 -05:00
|
|
|
return (
|
|
|
|
|
<View style={{ flex: 1 }}>
|
|
|
|
|
{showNoResults && (
|
|
|
|
|
<FullScreenMessage>
|
|
|
|
|
<Text style={{ fontSize: 16 }}>No food matched your search.</Text>
|
|
|
|
|
<Text style={{ fontSize: 16 }}>Check your filters and try again</Text>
|
|
|
|
|
</FullScreenMessage>
|
|
|
|
|
)}
|
|
|
|
|
{!showNoResults && (
|
2018-05-06 10:37:47 -05:00
|
|
|
<ScrollView
|
2018-07-01 11:54:16 -05:00
|
|
|
style={{ flex: 1 }}
|
2019-09-21 15:45:07 +00:00
|
|
|
refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onPulldown} />}
|
|
|
|
|
>
|
2018-08-25 10:12:52 -05:00
|
|
|
<FoodItemList foodItemsSeq={foodItemsSeq}>
|
|
|
|
|
{(foodItem: FoodItemRecord) => <FoodItemTile key={foodItem.id} foodItem={foodItem} />}
|
|
|
|
|
</FoodItemList>
|
2017-09-03 19:41:52 -05:00
|
|
|
</ScrollView>
|
2018-08-25 10:12:52 -05:00
|
|
|
)}
|
|
|
|
|
</View>
|
|
|
|
|
);
|
|
|
|
|
};
|
2018-04-28 13:12:38 -05:00
|
|
|
|
2018-05-06 10:37:47 -05:00
|
|
|
export default compose(
|
|
|
|
|
pure,
|
2018-08-25 10:12:52 -05:00
|
|
|
withRouterContext,
|
2018-05-06 10:37:47 -05:00
|
|
|
withFoodItemsAsSeq,
|
2018-08-25 10:12:52 -05:00
|
|
|
withFilter,
|
2019-09-21 15:45:07 +00:00
|
|
|
withState("isRefreshing", "setRefreshing", false),
|
2018-05-06 10:37:47 -05:00
|
|
|
withHandlers({
|
2018-11-18 11:14:26 -06:00
|
|
|
onPulldown: ({ setRefreshing, onRefresh, router }) => async () => {
|
|
|
|
|
try {
|
|
|
|
|
setRefreshing(true);
|
|
|
|
|
await onRefresh();
|
|
|
|
|
} catch (error) {
|
2019-09-21 15:45:07 +00:00
|
|
|
console.error(error); // eslint-disable-line no-console
|
|
|
|
|
router.history.push("/zipcode");
|
2018-11-18 11:14:26 -06:00
|
|
|
} finally {
|
|
|
|
|
setTimeout(() => setRefreshing(false), 1000);
|
|
|
|
|
}
|
2019-09-21 15:45:07 +00:00
|
|
|
}
|
2018-11-18 11:14:26 -06:00
|
|
|
}),
|
|
|
|
|
lifecycle({
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.props.onPulldown();
|
2019-09-21 15:45:07 +00:00
|
|
|
}
|
2018-05-06 10:37:47 -05:00
|
|
|
})
|
|
|
|
|
)(FoodList);
|