mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 04:24:56 -06:00
65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
import React from 'react';
|
|
import { View, FlatList } from 'react-native';
|
|
import { type SetSeq } from 'immutable';
|
|
import ProductRecord from '../records/ProductRecord';
|
|
import { compose, pure } from 'recompose';
|
|
import { pipe } from 'ramda';
|
|
import { withFilter } from '../enhancers/filterEnhancers';
|
|
import { compareQuantity } from '../helpers/QuantityHelpers';
|
|
|
|
const sortByDistance = (productsSeq: SetSeq<ProductRecord>): SetSeq<ProductRecord> => {
|
|
return productsSeq.sort((left, right) => left.distance - right.distance);
|
|
};
|
|
|
|
const sortByLastUpdated = (productsSeq: SetSeq<ProductRecord>): SetSeq<ProductRecord> => {
|
|
return productsSeq.sort((left, right) => right.lastupdated - left.lastupdated);
|
|
};
|
|
|
|
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;
|
|
});
|
|
};
|
|
|
|
const getSortBy = filter => {
|
|
switch (filter.orderby) {
|
|
case 'quantity':
|
|
return sortByQuantity;
|
|
case 'lastupdated':
|
|
return sortByLastUpdated;
|
|
default:
|
|
return sortByDistance;
|
|
}
|
|
};
|
|
|
|
const intoArray = (productsSeq: SetSeq<ProductRecord>): Array<ProductRecord> =>
|
|
productsSeq ? productsSeq.toArray() : [];
|
|
|
|
const ProductList = props => {
|
|
const { filter, productsSeq, children } = props;
|
|
|
|
if (!productsSeq) {
|
|
return null;
|
|
}
|
|
|
|
const items = pipe(
|
|
getSortBy(filter),
|
|
intoArray
|
|
)(productsSeq);
|
|
|
|
return (
|
|
<View style={{ flexShrink: 2 }}>
|
|
<FlatList
|
|
data={items}
|
|
renderItem={({ item }) => children(item)}
|
|
keyExtractor={item => item.id}
|
|
/>
|
|
</View>
|
|
);
|
|
};
|
|
|
|
export default compose(
|
|
pure,
|
|
withFilter
|
|
)(ProductList);
|