PliWould/components/ProductList.tsx

46 lines
1.3 KiB
TypeScript
Raw Normal View History

import { FlatList, ScrollView, StyleSheet, Text, TouchableHighlight } from "react-native";
2024-07-01 04:49:41 +02:00
import { ProductTile } from "./ProductTile";
import { Id, Product } from "@/lib/product";
import { Key, useEffect, useState } from "react";
2024-07-01 04:49:41 +02:00
import { useSelector } from "react-redux";
import { selectProducts } from "@/features/product/productSlice";
import { useAppSelector } from "@/app/store";
2024-07-01 04:49:41 +02:00
export type ProductSelectionProps = {
onProductSelected?: (product: Product) => any;
2024-07-01 04:49:41 +02:00
}
export default function ProductList({ onProductSelected }: ProductSelectionProps) {
2024-07-01 04:49:41 +02:00
const [activeProduct, setActiveProduct] = useState(null as null | Product);
const products = useAppSelector(selectProducts).filter(p => (!!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 scrollToOverflowEnabled={true}>
{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,
},
2024-07-01 04:49:41 +02:00
})