import { useEffect, useState } from "react"; import { View, Text, StyleSheet } from "react-native"; import { 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 convert, { Length } from "convert"; import ProductList from "@/components/ProductList"; import { HelpfulMeasurementUnitInput } from "./HelpfulMeasurementInput"; import { ScrollView } from "react-native-gesture-handler"; const DEFAULT_DIAMETER_UNIT: Length = "in"; const DEFAULT_LENGTH_UNIT: Length = "ft"; export const CarpetRollCalculator = () => { const products = useAppSelector(selectProducts); const [width, setWidth] = useState(0); const [outerDiameter, setOuterDiameter] = useState({ l: 0, u: DEFAULT_DIAMETER_UNIT, }); const [innerDiameter, setInnerDiameter] = useState({ l: 0, u: DEFAULT_DIAMETER_UNIT, }); const [numRings, setNumRings] = useState(0); const [rugDimensions, setRugDimensions] = useState({ u: DEFAULT_LENGTH_UNIT, w: 0, l: 0, }); const [selectedProduct, setSelectedProduct] = useState(null); const [units, setUnits] = useState(DEFAULT_LENGTH_UNIT); useEffect(() => { // convert the "diameter" units to the length unit. const outerD2Value = convert(outerDiameter.l, outerDiameter.u).to(units); const innerD2Value = convert(innerDiameter.l, innerDiameter.u).to(units); const innerD2 = { l: innerD2Value, u: units, }; const outerD2 = { l: outerD2Value, u: units, }; const l = diameterToLength(outerD2, innerD2, numRings).l; const dimens = { l, 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 ? ( ) : ( Please Select a Product )} setInnerDiameter({ ...innerDiameter, u })} defaultValue={innerDiameter.l} defaultUnit={innerDiameter.u} unitChoices={["ft", "in"]} onValueSet={(l) => setInnerDiameter({ ...innerDiameter, l })} /> setOuterDiameter({ ...outerDiameter, u })} defaultValue={innerDiameter.l} defaultUnit={innerDiameter.u} unitChoices={["ft", "in"]} onValueSet={(l) => setOuterDiameter({ ...outerDiameter, l })} /> ); }; const styles = StyleSheet.create({ container: { flexGrow: 1, flex: 1, }, placeholder: { alignContent: "center", alignSelf: "center", paddingTop: 50, paddingBottom: 50, fontSize: 30, }, inputFieldWrapper: { padding: 10, }, inputFields: {}, label: { flex: 1, flexDirection: "row", }, numberInput: { flexDirection: "row", borderStyle: "solid", borderColor: "black", borderWidth: 1, }, }); export default CarpetRollCalculator;