import React, { useEffect, useState } from "react"; import { View, Text, TextInput, Button, StyleSheet } from "react-native"; import { productPriceFor, priceDisplay, pricePerUnitDisplay, Product, } from "@/lib/product"; import { selectProducts } from "@/features/product/productSlice"; import { area_t, diameterToLength, length_t } from "@/lib/dimensions"; import { useAppSelector } from "../app/store"; import { AreaRugTag } from "@/components/AreaRugTag"; import { Length } from "convert"; import ProductList from "@/components/ProductList"; const DEFAULT_UNIT: Length = "ft"; export const CarpetRollCalculator = () => { const products = useAppSelector(selectProducts); const [width, setWidth] = useState(0); const [outerDiameter, setOuterDiameter] = useState({ l: 0, u: DEFAULT_UNIT, }); const [innerDiameter, setInnerDiameter] = useState({ l: 0, u: DEFAULT_UNIT, }); const [numRings, setNumRings] = useState(0); const [price, setPrice] = useState(0); const [rugDimensions, setRugDimensions] = useState({ u: DEFAULT_UNIT, w: 0, l: 0, }); const [selectedProduct, setSelectedProduct] = useState(null); const [units, setUnits] = useState(DEFAULT_UNIT); useEffect(() => { console.log(`recalculating...`); const dimens = { l: diameterToLength(outerDiameter, innerDiameter, numRings).l || 0.0, w: width || selectedProduct?.dimensions.l || 0.0, u: units || selectedProduct?.dimensions.u || "ft", }; console.dir(dimens); setRugDimensions(dimens); }, [outerDiameter, innerDiameter, width, numRings, selectedProduct, units]); return ( {selectedProduct && ( )} Length Calculation Outer Diameter: setOuterDiameter({ l: Number(text), u: units }) } /> Inner Diameter: setInnerDiameter({ l: Number(text), u: units }) } /> Number of rings: setNumRings(Number(text))} /> Width: setWidth(Number(text))} /> Price: {priceDisplay(price)} {selectedProduct ? pricePerUnitDisplay(selectedProduct) : "0.00"} ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "center", padding: 20, }, }); export default CarpetRollCalculator;