2024-07-31 19:01:45 +02:00
|
|
|
import { ScrollView, StyleSheet } from "react-native";
|
2024-07-01 04:49:41 +02:00
|
|
|
import { ProductTile } from "./ProductTile";
|
2024-08-10 19:06:25 +02:00
|
|
|
import { Product, product_type_t } from "@/lib/product";
|
2024-07-31 19:01:45 +02:00
|
|
|
import { useState } from "react";
|
2024-07-01 04:49:41 +02:00
|
|
|
import { selectProducts } from "@/features/product/productSlice";
|
2024-07-01 21:23:45 +02:00
|
|
|
import { useAppSelector } from "@/app/store";
|
2024-07-01 04:49:41 +02:00
|
|
|
|
|
|
|
export type ProductSelectionProps = {
|
2024-07-01 21:23:45 +02:00
|
|
|
onProductSelected?: (product: Product) => any;
|
2024-08-10 19:06:25 +02:00
|
|
|
productType?: product_type_t;
|
2024-07-31 19:01:45 +02:00
|
|
|
};
|
2024-07-01 04:49:41 +02:00
|
|
|
|
2024-07-31 19:01:45 +02:00
|
|
|
export default function ProductList({
|
2024-08-10 19:06:25 +02:00
|
|
|
productType,
|
2024-07-31 19:01:45 +02:00
|
|
|
onProductSelected,
|
|
|
|
}: ProductSelectionProps) {
|
2024-07-01 21:23:45 +02:00
|
|
|
const [activeProduct, setActiveProduct] = useState(null as null | Product);
|
2024-08-10 19:06:25 +02:00
|
|
|
const products = useAppSelector(selectProducts)
|
|
|
|
.filter((p) => !!p)
|
2024-08-15 23:07:19 +02:00
|
|
|
.filter((p: Product) => (!productType) || p.type === productType)
|
2024-08-10 19:06:25 +02:00
|
|
|
.filter((p) => {
|
|
|
|
return !!p.dimensions;
|
|
|
|
});
|
2024-07-01 04:49:41 +02:00
|
|
|
|
2024-07-01 21:23:45 +02:00
|
|
|
function doOnProductSelected(product: Product) {
|
|
|
|
setActiveProduct(product);
|
|
|
|
onProductSelected && onProductSelected(product);
|
|
|
|
}
|
2024-07-01 04:49:41 +02:00
|
|
|
|
2024-07-01 21:23:45 +02:00
|
|
|
return (
|
2024-07-31 19:01:45 +02:00
|
|
|
<ScrollView scrollToOverflowEnabled={true} aria-label="product list">
|
|
|
|
{products.map((product) => {
|
2024-07-01 04:49:41 +02:00
|
|
|
return (
|
2024-07-01 21:23:45 +02:00
|
|
|
<ProductTile
|
|
|
|
product={product}
|
|
|
|
onProductSelected={doOnProductSelected}
|
|
|
|
isActive={activeProduct === product}
|
|
|
|
key={product.id}
|
|
|
|
/>
|
2024-07-01 04:49:41 +02:00
|
|
|
);
|
2024-07-01 21:23:45 +02:00
|
|
|
})}
|
|
|
|
</ScrollView>
|
2024-07-31 19:01:45 +02:00
|
|
|
);
|
2024-07-01 04:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2024-07-01 21:23:45 +02:00
|
|
|
productSelectorFlatList: {
|
|
|
|
padding: 10,
|
|
|
|
margin: 10,
|
|
|
|
},
|
2024-07-31 19:01:45 +02:00
|
|
|
});
|