import { Image, StyleSheet, Platform, ImageBackground, View, Text, Button, TextInputKeyPressEventData } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { useAppSelector } from '../store'; import { selectProducts } from '@/features/product/productSlice'; import { Product, dimensions_t } from '@/lib/product'; import { ProductTile } from '@/components/ProductTile'; import { useEffect, useState } from 'react'; import { TextInput, TouchableHighlight } from 'react-native-gesture-handler'; export default function HomeScreen() { const products = useAppSelector(selectProducts); const [activeProduct, setActiveProduct] = useState(null as Product | null); const [price, setPrice] = useState("0.00"); const [length, setLength] = useState("0"); const [width, setWidth] = useState("0"); const [units, setUnits] = useState("in" as "ft" | "in"); function calculatePrice() { if (!activeProduct) { setPrice("0.00"); return; } const l = Number.parseInt(length); const w = Number.parseInt(width); console.log("l=%d, w=%d", l, w); const u = units; const d : dimensions_t = activeProduct.area ? {l, w, u} : {l, u}; const p = activeProduct.priceFor(d); console.log("set price %s", p); const s = p.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2, }) setPrice(s); } const onProductSelected = (product: Product) => { setActiveProduct(product); calculatePrice(); } const onUnitPressed = (u : "ft" | "in") => { setUnits(u); calculatePrice(); } const onLengthChanged = (value : string) => { setLength(value); calculatePrice(); } const onWidthChanged = (width : string) => { setWidth(width); calculatePrice(); } return ( $ { price } {activeProduct ? ( {units} {activeProduct.area && ( calculatePrice()} onChangeText={setWidth} inputMode='decimal' style={styles.widthInput} /> {units} ) } ) : (Please choose a product)}