2024-08-15 23:07:19 +02:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { View, Text, StyleSheet } from "react-native";
|
|
|
|
import { Product } from "@/lib/product";
|
2024-08-10 19:06:25 +02:00
|
|
|
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";
|
2024-08-15 23:07:19 +02:00
|
|
|
import convert, { Length } from "convert";
|
2024-08-10 19:06:25 +02:00
|
|
|
import ProductList from "@/components/ProductList";
|
2024-08-15 23:07:19 +02:00
|
|
|
import { HelpfulMeasurementUnitInput } from "./HelpfulMeasurementInput";
|
|
|
|
import { ScrollView } from "react-native-gesture-handler";
|
2024-08-10 19:06:25 +02:00
|
|
|
|
2024-08-15 23:07:19 +02:00
|
|
|
const DEFAULT_DIAMETER_UNIT: Length = "in";
|
|
|
|
const DEFAULT_LENGTH_UNIT: Length = "ft";
|
2024-08-10 19:06:25 +02:00
|
|
|
|
|
|
|
export const CarpetRollCalculator = () => {
|
|
|
|
const products = useAppSelector(selectProducts);
|
|
|
|
|
|
|
|
const [width, setWidth] = useState(0);
|
|
|
|
const [outerDiameter, setOuterDiameter] = useState<length_t>({
|
|
|
|
l: 0,
|
2024-08-15 23:07:19 +02:00
|
|
|
u: DEFAULT_DIAMETER_UNIT,
|
2024-08-10 19:06:25 +02:00
|
|
|
});
|
|
|
|
const [innerDiameter, setInnerDiameter] = useState<length_t>({
|
|
|
|
l: 0,
|
2024-08-15 23:07:19 +02:00
|
|
|
u: DEFAULT_DIAMETER_UNIT,
|
2024-08-10 19:06:25 +02:00
|
|
|
});
|
|
|
|
const [numRings, setNumRings] = useState(0);
|
|
|
|
const [rugDimensions, setRugDimensions] = useState<area_t>({
|
2024-08-15 23:07:19 +02:00
|
|
|
u: DEFAULT_LENGTH_UNIT,
|
2024-08-10 19:06:25 +02:00
|
|
|
w: 0,
|
|
|
|
l: 0,
|
|
|
|
});
|
|
|
|
const [selectedProduct, setSelectedProduct] = useState<Product | null>(null);
|
2024-08-15 23:07:19 +02:00
|
|
|
const [units, setUnits] = useState<Length>(DEFAULT_LENGTH_UNIT);
|
2024-08-10 19:06:25 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-08-15 23:07:19 +02:00
|
|
|
// 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;
|
|
|
|
|
2024-08-10 19:06:25 +02:00
|
|
|
const dimens = {
|
2024-08-15 23:07:19 +02:00
|
|
|
l,
|
2024-08-10 19:06:25 +02:00
|
|
|
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 (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<View>
|
2024-08-15 23:07:19 +02:00
|
|
|
{selectedProduct ? (
|
|
|
|
<AreaRugTag dimensions={rugDimensions} product={selectedProduct} />
|
|
|
|
) : (
|
|
|
|
<Text style={styles.placeholder}>Please Select a Product</Text>
|
|
|
|
)}
|
2024-08-10 19:06:25 +02:00
|
|
|
</View>
|
2024-08-15 23:07:19 +02:00
|
|
|
<View style={{ flex: 1, }}>
|
|
|
|
<ScrollView>
|
|
|
|
<View style={styles.inputFields}>
|
|
|
|
<View style={styles.inputFieldWrapper}>
|
|
|
|
<HelpfulMeasurementUnitInput
|
|
|
|
label="Length"
|
|
|
|
svgUri="/assets/images/icons/carpet-roll-length-raw.svg"
|
|
|
|
onUnitSet={setUnits}
|
|
|
|
onValueSet={setWidth}
|
|
|
|
defaultValue={width}
|
|
|
|
defaultUnit={units}
|
|
|
|
unitChoices={["ft", "in"]}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<View style={styles.inputFieldWrapper}>
|
|
|
|
<HelpfulMeasurementUnitInput
|
|
|
|
label="Inner diameter"
|
|
|
|
svgUri="/assets/images/icons/carpet-roll-length-inner-diameter-raw.svg"
|
|
|
|
onUnitSet={(u) => setInnerDiameter({ ...innerDiameter, u })}
|
|
|
|
defaultValue={innerDiameter.l}
|
|
|
|
defaultUnit={innerDiameter.u}
|
|
|
|
unitChoices={["ft", "in"]}
|
|
|
|
onValueSet={(l) => setInnerDiameter({ ...innerDiameter, l })}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<View style={styles.inputFieldWrapper}>
|
|
|
|
<HelpfulMeasurementUnitInput
|
|
|
|
label="Outer diameter"
|
|
|
|
svgUri="/assets/images/icons/carpet-roll-length-outer-diameter-raw.svg"
|
|
|
|
onUnitSet={(u) => setOuterDiameter({ ...outerDiameter, u })}
|
|
|
|
defaultValue={innerDiameter.l}
|
|
|
|
defaultUnit={innerDiameter.u}
|
|
|
|
unitChoices={["ft", "in"]}
|
|
|
|
onValueSet={(l) => setOuterDiameter({ ...outerDiameter, l })}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<View style={styles.inputFieldWrapper}>
|
|
|
|
<HelpfulMeasurementUnitInput
|
|
|
|
label="Number of rings"
|
|
|
|
svgUri="/assets/images/icons/carpet-roll-length-number-of-rings-raw.svg"
|
|
|
|
defaultValue={0}
|
|
|
|
onValueSet={setNumRings}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
<View style={styles.container}>
|
|
|
|
<ProductList
|
|
|
|
onProductSelected={setSelectedProduct}
|
|
|
|
productType="area_rug"
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</ScrollView>
|
2024-08-10 19:06:25 +02:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
2024-08-15 23:07:19 +02:00
|
|
|
flexGrow: 1,
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
placeholder: {
|
|
|
|
alignContent: "center",
|
|
|
|
alignSelf: "center",
|
|
|
|
paddingTop: 50,
|
|
|
|
paddingBottom: 50,
|
|
|
|
fontSize: 30,
|
|
|
|
},
|
|
|
|
inputFieldWrapper: {
|
|
|
|
padding: 10,
|
|
|
|
},
|
|
|
|
inputFields: {},
|
|
|
|
label: {
|
2024-08-10 19:06:25 +02:00
|
|
|
flex: 1,
|
2024-08-15 23:07:19 +02:00
|
|
|
flexDirection: "row",
|
|
|
|
},
|
|
|
|
numberInput: {
|
|
|
|
flexDirection: "row",
|
|
|
|
borderStyle: "solid",
|
|
|
|
borderColor: "black",
|
|
|
|
borderWidth: 1,
|
2024-08-10 19:06:25 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default CarpetRollCalculator;
|