2024-06-27 23:31:59 +02:00
|
|
|
import { Product } from "@/lib/product"
|
|
|
|
import { useState } from "react"
|
2024-06-28 04:00:29 +02:00
|
|
|
import { FlatList, StyleSheet, Text, TouchableHighlight, View } from "react-native"
|
|
|
|
import {ProductAttributeEditor} from "./ProductAttributeEditor";
|
|
|
|
import React from "react";
|
2024-06-27 23:31:59 +02:00
|
|
|
|
2024-06-28 04:00:29 +02:00
|
|
|
export type ProductUpdatedFunc = (product_id: string, product: Product) => any;
|
|
|
|
export type ProductDeletedFunc = (product_id: string) => any;
|
2024-06-27 23:31:59 +02:00
|
|
|
|
|
|
|
export type ProductEditorItemProps = {
|
2024-06-28 04:00:29 +02:00
|
|
|
product: Product,
|
2024-06-27 23:31:59 +02:00
|
|
|
onProductUpdated?: ProductUpdatedFunc,
|
|
|
|
onProductDeleted?: ProductDeletedFunc,
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ProductEditorItem = ({ product, onProductUpdated, onProductDeleted }: ProductEditorItemProps) => {
|
|
|
|
|
2024-06-28 04:00:29 +02:00
|
|
|
const [showAttributes, setShowAttributes] = useState(false);
|
2024-06-27 23:31:59 +02:00
|
|
|
|
|
|
|
function onAttributeChanged(product_id: string, key: string, newValue: string) {
|
|
|
|
product.attributes[key] = newValue;
|
|
|
|
onProductUpdated && onProductUpdated(product_id, product);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function onAttributeDelete(product_id: string, key: string) {
|
|
|
|
product.removeAttribute(key);
|
2024-06-28 04:00:29 +02:00
|
|
|
onProductDeleted && onProductDeleted(product_id);
|
2024-06-27 23:31:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-06-28 04:00:29 +02:00
|
|
|
<View>
|
|
|
|
<TouchableHighlight
|
|
|
|
onPress={() => setShowAttributes(!showAttributes)}
|
|
|
|
aria-label="Product Item"
|
|
|
|
>
|
|
|
|
<Text style={styles.product}>{product.attributes.name || `Product ${product.id}`} </Text>
|
|
|
|
</TouchableHighlight>
|
2024-06-27 23:31:59 +02:00
|
|
|
{showAttributes &&
|
|
|
|
(
|
|
|
|
<FlatList
|
|
|
|
data={product.attributesAsList}
|
|
|
|
renderItem={({ item }) => (
|
|
|
|
<ProductAttributeEditor
|
|
|
|
product={product}
|
2024-06-28 04:00:29 +02:00
|
|
|
attributeKey={item.key || "some key"}
|
|
|
|
attributeValue={item.value}
|
2024-06-27 23:31:59 +02:00
|
|
|
onChange={onAttributeChanged}
|
|
|
|
onDelete={onAttributeDelete}
|
|
|
|
/>
|
|
|
|
)}
|
2024-06-28 04:00:29 +02:00
|
|
|
keyExtractor={(item) => `${product.id}-${item.key}`}
|
2024-06-27 23:31:59 +02:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2024-06-28 04:00:29 +02:00
|
|
|
</View>
|
2024-06-27 23:31:59 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
product: {},
|
|
|
|
})
|