aretherecookies-mobile/js/components/ProductsList.js

66 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-04-26 12:27:24 -05:00
import React from 'react';
import { View, FlatList } from 'react-native';
2017-06-03 19:43:45 -05:00
import { type SetSeq } from 'immutable';
2020-04-17 22:50:23 -05:00
import ProductRecord from '../records/ProductRecord';
import { compose, pure } from 'recompose';
import { pipe } from 'ramda';
import { withFilter } from '../enhancers/filterEnhancers';
import { compareQuantity } from '../helpers/QuantityHelpers';
2017-05-13 22:33:56 -05:00
2020-04-17 22:50:23 -05:00
const sortByDistance = (productsSeq: SetSeq<ProductRecord>): SetSeq<ProductRecord> => {
return productsSeq.sort((left, right) => left.distance - right.distance);
2017-05-25 21:44:57 -05:00
};
2020-04-17 22:50:23 -05:00
const sortByLastUpdated = (productsSeq: SetSeq<ProductRecord>): SetSeq<ProductRecord> => {
return productsSeq.sort((left, right) => right.lastupdated - left.lastupdated);
};
2020-04-17 22:50:23 -05:00
const sortByQuantity = (productsSeq: SetSeq<ProductRecord>): SetSeq<ProductRecord> => {
return productsSeq.sort((left, right) => {
const quantityCompare = compareQuantity(left.quantity, right.quantity);
return quantityCompare === 0 ? left.distance - right.distance : quantityCompare;
});
};
2017-05-25 21:44:57 -05:00
2020-04-26 12:27:24 -05:00
const getSortBy = filter => {
switch (filter.orderby) {
case 'quantity':
return sortByQuantity;
case 'lastupdated':
return sortByLastUpdated;
default:
return sortByDistance;
}
};
2017-05-13 22:33:56 -05:00
2020-04-17 22:50:23 -05:00
const intoArray = (productsSeq: SetSeq<ProductRecord>): Array<ProductRecord> =>
productsSeq ? productsSeq.toArray() : [];
2017-05-13 22:33:56 -05:00
2020-04-26 12:27:24 -05:00
const ProductList = props => {
2020-04-17 22:50:23 -05:00
const { filter, productsSeq, children } = props;
2020-04-17 22:50:23 -05:00
if (!productsSeq) {
return null;
}
const items = pipe(
getSortBy(filter),
intoArray
2020-04-17 22:50:23 -05:00
)(productsSeq);
2020-04-26 12:27:24 -05:00
return (
<View style={{ flexShrink: 2 }}>
<FlatList
data={items}
renderItem={({ item }) => children(item)}
keyExtractor={item => item.id}
/>
</View>
);
};
2017-05-13 22:33:56 -05:00
export default compose(
pure,
withFilter
2020-04-17 22:50:23 -05:00
)(ProductList);