PliWould/components/ProductEditor.tsx

77 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-06-27 23:31:59 +02:00
import { useAppDispatch, useAppSelector } from "@/app/store"
import { addAttribute, changeKey, deleteAttribute, deleteProduct, selectProductIds, selectProducts, updateAttribute, updateDimensions, updatePrice, updateProduct } from "@/features/product/productSlice"
import { Id, Product, dimensions_t } from "@/lib/product";
import { FlatList, SafeAreaView, StyleSheet, Text } from "react-native";
2024-06-27 23:31:59 +02:00
import { ProductEditorItem } from "./ProductEditorItem";
export const ProductEditor = ({}) => {
2024-06-27 23:31:59 +02:00
const products = useAppSelector(selectProducts) as Product [];
const dispatch = useAppDispatch();
function onProductDeleted(product_id: string) {
dispatch(deleteProduct(product_id));
}
function onAttributeDelete(product_id: string, attribute: string) {
dispatch(deleteAttribute({product_id: product_id, attribute}));
2024-06-27 23:31:59 +02:00
}
function onAttributeUpdated(product_id: string, attribute: string, value: string) {
dispatch(updateAttribute({product_id, attributeKey: attribute, attributeValue: value}));
}
function onAttributeAdded(product_id: Id) {
console.log("Adding attribute to %s", product_id);
dispatch(addAttribute(product_id));
}
function onPriceUpdated(product_id: string, pricePerUnit: number) {
dispatch(updatePrice({product_id, pricePerUnit}));
}
function onAttributeKeyChanged(product_id : string, oldKey : string, newKey : string) {
dispatch(changeKey({product_id, oldKey, newKey}))
}
function onDimensionUpdated(product_id: string, dimensions: dimensions_t) {
dispatch(updateDimensions({product_id, dimensions}));
}
2024-06-27 23:31:59 +02:00
return (
<SafeAreaView style={{overflow: "scroll"}}>
<Text>Edit Products</Text>
2024-06-27 23:31:59 +02:00
<FlatList
data={products}
keyExtractor={(p, i) => `product-${p.id}`}
2024-06-27 23:31:59 +02:00
renderItem={
({item}) => {
2024-06-27 23:31:59 +02:00
return (
<ProductEditorItem
product={item}
onProductDeleted={onProductDeleted}
onAttributeDeleted={onAttributeDelete}
onAttributeKeyChanged={onAttributeKeyChanged}
onAttributeUpdated={onAttributeUpdated}
onAttributeAdded={onAttributeAdded}
onPriceUpdated={onPriceUpdated}
onDimensionsUpdated={onDimensionUpdated}
2024-06-27 23:31:59 +02:00
/>
)
}
}
/>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
h1: {
textAlign: "center",
fontFamily: "sans-serif"
},
2024-06-27 23:31:59 +02:00
product: {
}
})