PliWould/components/ProductList.tsx

61 lines
1.7 KiB
TypeScript
Raw Normal View History

import { Dimensions, ScrollView, StyleSheet } from "react-native";
2024-07-01 04:49:41 +02:00
import { ProductTile } from "./ProductTile";
import { Product, product_type_t } from "@/lib/product";
import { useState } from "react";
2024-07-01 04:49:41 +02:00
import { selectProducts } from "@/features/product/productSlice";
import { useAppSelector } from "@/app/store";
2024-07-01 04:49:41 +02:00
const windowDimensions = Dimensions.get('window');
2024-07-01 04:49:41 +02:00
export type ProductSelectionProps = {
onProductSelected?: (product: Product) => any;
productType?: product_type_t;
};
2024-07-01 04:49:41 +02:00
export default function ProductList({
productType,
onProductSelected,
}: ProductSelectionProps) {
const [activeProduct, setActiveProduct] = useState(null as null | Product);
const products = useAppSelector(selectProducts)
.filter((p) => !!p)
.filter((p: Product) => (!productType) || p.type === productType)
.filter((p) => {
return !!p.dimensions;
});
2024-07-01 04:49:41 +02:00
function doOnProductSelected(product: Product) {
setActiveProduct(product);
onProductSelected && onProductSelected(product);
}
2024-07-01 04:49:41 +02:00
return (
<ScrollView style={styles.productSelectorFlatList} contentContainerStyle={styles.content} aria-label="product list">
{products.map((product) => {
2024-07-01 04:49:41 +02:00
return (
<ProductTile
product={product}
onProductSelected={doOnProductSelected}
isActive={activeProduct === product}
key={product.id}
/>
2024-07-01 04:49:41 +02:00
);
})}
</ScrollView>
);
2024-07-01 04:49:41 +02:00
}
const styles = StyleSheet.create({
productSelectorFlatList: {
padding: 10,
margin: 10,
height: windowDimensions.height - 200,
width: windowDimensions.width,
},
content: {
alignItems: "flex-start",
flexWrap: "wrap",
flexDirection: "row",
}
});