aretherecookies-mobile/js/pages/ProductList.js

65 lines
2 KiB
JavaScript
Raw Normal View History

2020-03-29 19:47:33 +00:00
import React from 'react';
import { View, Text, ScrollView, RefreshControl } from 'react-native';
2020-04-17 22:50:23 -05:00
import ProductTile from '../components/ProductTile';
import ProductsList from '../components/ProductsList';
import { withProductsAsSeq } from '../enhancers/productsEnhancers';
2020-03-29 19:47:33 +00:00
import { compose, pure, withState, withHandlers, lifecycle } from 'recompose';
import { withFilter } from '../enhancers/filterEnhancers';
import { withRouterContext } from '../enhancers/routeEnhancers';
import FullScreenMessage from '../components/FullScreenMessage';
import { withFaves } from '../enhancers/favesEnhancer';
2017-09-03 19:41:52 -05:00
2020-04-17 22:50:23 -05:00
const ProductList = props => {
const { productsSeq, isRefreshing, onPulldown, isFilterDirty } = props;
2017-09-03 19:41:52 -05:00
2020-04-17 22:50:23 -05:00
const refreshing = isRefreshing || !productsSeq;
const showNoResults = !isRefreshing && isFilterDirty && productsSeq && !productsSeq.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 products matched your search.</Text>
2018-08-25 10:12:52 -05:00
<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 }}
2020-03-29 19:47:33 +00:00
refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onPulldown} />}>
2020-04-17 22:50:23 -05:00
<ProductsList productsSeq={productsSeq}>
2020-04-26 12:27:24 -05:00
{product => <ProductTile product={product} />}
2020-04-17 22:50:23 -05:00
</ProductsList>
2017-09-03 19:41:52 -05:00
</ScrollView>
2018-08-25 10:12:52 -05:00
)}
</View>
);
};
2018-05-06 10:37:47 -05:00
export default compose(
pure,
2018-08-25 10:12:52 -05:00
withRouterContext,
2020-04-17 22:50:23 -05:00
withProductsAsSeq,
2018-08-25 10:12:52 -05:00
withFilter,
2020-03-29 19:47:33 +00:00
withFaves,
withState('isRefreshing', 'setRefreshing', false),
2018-05-06 10:37:47 -05:00
withHandlers({
2020-03-29 19:47:33 +00:00
onPulldown: ({ setRefreshing, onRefresh, getFaves, router }) => async () => {
2018-11-18 11:14:26 -06:00
try {
setRefreshing(true);
2020-03-29 19:47:33 +00:00
await Promise.all([onRefresh(), getFaves()]);
2018-11-18 11:14:26 -06:00
} catch (error) {
2019-09-21 15:45:07 +00:00
console.error(error); // eslint-disable-line no-console
2020-03-29 19:47:33 +00:00
router.history.push('/zipcode');
2018-11-18 11:14:26 -06:00
} finally {
setTimeout(() => setRefreshing(false), 1000);
}
2020-03-29 19:47:33 +00:00
},
2018-11-18 11:14:26 -06:00
}),
lifecycle({
componentDidMount() {
this.props.onPulldown();
2020-03-29 19:47:33 +00:00
},
2018-05-06 10:37:47 -05:00
})
2020-04-17 22:50:23 -05:00
)(ProductList);