2024-06-27 23:31:59 +02:00
|
|
|
import { useAppDispatch, useAppSelector } from "@/app/store"
|
2024-06-30 18:37:27 +02:00
|
|
|
import { addAttribute, changeKey, deleteAttribute, deleteProduct, selectProductIds, selectProducts, updateAttribute, updateDimensions, updatePrice, updateProduct } from "@/features/product/productSlice"
|
2024-07-06 00:00:08 +02:00
|
|
|
import { Id, Product } from "@/lib/product";
|
|
|
|
import { dimensions_t } from "@/lib/dimensions_t";
|
2024-06-28 15:04:50 +02:00
|
|
|
import { FlatList, SafeAreaView, StyleSheet, Text } from "react-native";
|
2024-06-27 23:31:59 +02:00
|
|
|
import { ProductEditorItem } from "./ProductEditorItem";
|
|
|
|
|
2024-06-28 15:04:50 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2024-06-30 18:37:27 +02:00
|
|
|
function onAttributeDelete(product_id: string, attribute: string) {
|
|
|
|
dispatch(deleteAttribute({product_id: product_id, attribute}));
|
2024-06-27 23:31:59 +02:00
|
|
|
}
|
|
|
|
|
2024-06-30 18:37:27 +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 (
|
2024-07-01 21:23:45 +02:00
|
|
|
<SafeAreaView style={{overflow: "scroll"}}>
|
|
|
|
<Text>Edit Products</Text>
|
2024-06-27 23:31:59 +02:00
|
|
|
<FlatList
|
|
|
|
data={products}
|
2024-06-30 18:37:27 +02:00
|
|
|
keyExtractor={(p, i) => `product-${p.id}`}
|
2024-06-27 23:31:59 +02:00
|
|
|
renderItem={
|
|
|
|
({item}) => {
|
2024-06-30 18:37:27 +02:00
|
|
|
|
2024-06-27 23:31:59 +02:00
|
|
|
return (
|
|
|
|
<ProductEditorItem
|
|
|
|
product={item}
|
|
|
|
onProductDeleted={onProductDeleted}
|
2024-06-30 18:37:27 +02:00
|
|
|
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({
|
2024-06-29 15:09:22 +02:00
|
|
|
h1: {
|
|
|
|
textAlign: "center",
|
|
|
|
fontFamily: "sans-serif"
|
|
|
|
},
|
2024-06-27 23:31:59 +02:00
|
|
|
product: {
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|