2024-06-29 15:09:22 +02:00
|
|
|
import { Image, StyleSheet, Platform, ImageBackground, View, Text, Button, TextInputKeyPressEventData, TextInput, FlatList } from 'react-native';
|
2024-06-26 04:20:08 +02:00
|
|
|
|
2024-06-27 16:06:39 +02:00
|
|
|
import { SafeAreaView } from 'react-native-safe-area-context';
|
2024-06-29 02:04:30 +02:00
|
|
|
import { useAppSelector } from '../store';
|
2024-06-27 23:31:59 +02:00
|
|
|
import { selectProducts } from '@/features/product/productSlice';
|
2024-06-29 02:04:30 +02:00
|
|
|
import { Product, dimensions_t } from '@/lib/product';
|
|
|
|
import { useEffect, useState } from 'react';
|
2024-06-29 15:09:22 +02:00
|
|
|
import { TouchableHighlight } from 'react-native-gesture-handler';
|
|
|
|
|
|
|
|
const fallbackImage = require("@/assets/images/board-stock-lightened-blurred.png");
|
2024-06-26 04:20:08 +02:00
|
|
|
|
|
|
|
export default function HomeScreen() {
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-06-29 02:04:30 +02:00
|
|
|
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");
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-06-29 15:09:22 +02:00
|
|
|
useEffect(function () {
|
|
|
|
const iv = setInterval(function () {
|
|
|
|
if (!activeProduct) return;
|
|
|
|
const l = Number.parseInt(length);
|
|
|
|
const w = Number.parseInt(width);
|
2024-06-30 18:37:27 +02:00
|
|
|
// console.log("l=%d, w=%d", l, w);
|
2024-06-29 15:09:22 +02:00
|
|
|
const u = units;
|
|
|
|
const d: dimensions_t = activeProduct.area ? { l, w, u } : { l, u };
|
2024-06-30 18:37:27 +02:00
|
|
|
try {
|
|
|
|
const p = activeProduct.priceFor(d);
|
|
|
|
console.log("set price %s", p);
|
|
|
|
const s = p.toLocaleString(undefined, {
|
|
|
|
minimumFractionDigits: 2,
|
|
|
|
maximumFractionDigits: 2,
|
|
|
|
})
|
|
|
|
setPrice(s == "NaN" ? "0.00" : s);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(activeProduct);
|
|
|
|
console.error(err)
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}, 50);
|
2024-06-29 15:09:22 +02:00
|
|
|
return function () {
|
|
|
|
clearInterval(iv);
|
2024-06-29 02:04:30 +02:00
|
|
|
}
|
2024-06-29 15:09:22 +02:00
|
|
|
}, [activeProduct, length, width]);
|
2024-06-27 23:31:59 +02:00
|
|
|
|
2024-06-26 04:20:08 +02:00
|
|
|
return (
|
2024-06-29 02:04:30 +02:00
|
|
|
<SafeAreaView style={styles.wrapper}>
|
|
|
|
<View style={styles.bigPriceWrapper}>
|
2024-06-29 15:09:22 +02:00
|
|
|
<Text style={styles.bigPrice}>$ {price}</Text>
|
2024-06-29 02:04:30 +02:00
|
|
|
</View>
|
|
|
|
<View style={styles.inputAndUnitWrapper}>
|
|
|
|
<View style={styles.inputWrapper}>
|
|
|
|
{activeProduct ? (
|
|
|
|
<View>
|
2024-06-29 15:09:22 +02:00
|
|
|
<TextInput
|
|
|
|
clearTextOnFocus={true}
|
|
|
|
onChangeText={setLength}
|
|
|
|
inputMode='decimal'
|
|
|
|
style={styles.lengthInput}
|
|
|
|
/>
|
|
|
|
<Text style={styles.unitHints}>{units}</Text>
|
|
|
|
{activeProduct.area && (
|
|
|
|
<View>
|
|
|
|
<TextInput
|
|
|
|
clearTextOnFocus={true}
|
|
|
|
defaultValue={width}
|
|
|
|
onChangeText={setWidth}
|
|
|
|
inputMode='decimal'
|
|
|
|
style={styles.widthInput}
|
|
|
|
/>
|
|
|
|
<Text style={styles.unitHints}>{units}</Text>
|
|
|
|
</View>)
|
|
|
|
}
|
2024-06-30 18:37:27 +02:00
|
|
|
<View style={styles.unitSelector}>
|
|
|
|
<Button title="in" onPress={() => setUnits("in")} color={units === "in" ? "gray" : "blue"} />
|
|
|
|
<Button title="ft" onPress={() => setUnits("ft")} color={units === "ft" ? "gray" : "blue"} />
|
|
|
|
</View>
|
2024-06-29 02:04:30 +02:00
|
|
|
</View>
|
|
|
|
) : (<Text>Please choose a product</Text>)}
|
2024-06-29 15:09:22 +02:00
|
|
|
</View>
|
2024-06-29 02:04:30 +02:00
|
|
|
</View>
|
2024-06-29 15:09:22 +02:00
|
|
|
<FlatList
|
|
|
|
data={products}
|
|
|
|
style={styles.productSelectorFlatList}
|
|
|
|
renderItem={({ item }) => {
|
|
|
|
return (
|
|
|
|
<TouchableHighlight
|
|
|
|
style={item === activeProduct ? styles.productTileTouchableActive : styles.productTileTouchable}
|
|
|
|
onPress={() => setActiveProduct(item)}>
|
|
|
|
<Text style={item === activeProduct ? styles.productTileTextActive : styles.productTileText}>{item.attributes.name || `Product ${item.id}`}</Text>
|
|
|
|
</TouchableHighlight>
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
2024-06-27 16:06:39 +02:00
|
|
|
</SafeAreaView>
|
2024-06-26 04:20:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2024-06-29 02:04:30 +02:00
|
|
|
wrapper: {
|
|
|
|
},
|
|
|
|
bigPriceWrapper: {
|
|
|
|
alignContent: "center",
|
|
|
|
},
|
|
|
|
bigPrice: {
|
|
|
|
alignSelf: "center",
|
|
|
|
fontSize: 40,
|
|
|
|
marginTop: 100,
|
|
|
|
marginBottom: 100,
|
|
|
|
},
|
|
|
|
inputWrapper: {
|
|
|
|
flexDirection: "row",
|
2024-06-29 15:09:22 +02:00
|
|
|
alignItems: "flex-start",
|
2024-06-29 02:04:30 +02:00
|
|
|
},
|
|
|
|
unitSelector: {
|
|
|
|
},
|
|
|
|
inputAndUnitWrapper: {
|
|
|
|
flexDirection: "row",
|
2024-06-29 15:09:22 +02:00
|
|
|
alignSelf: "center",
|
2024-06-29 02:04:30 +02:00
|
|
|
},
|
|
|
|
unitHints: {
|
|
|
|
fontSize: 30,
|
|
|
|
padding: 10,
|
|
|
|
},
|
|
|
|
lengthInput: {
|
|
|
|
borderWidth: 1,
|
|
|
|
borderRadius: 4,
|
|
|
|
borderColor: "grey",
|
|
|
|
padding: 4,
|
|
|
|
margin: 4,
|
|
|
|
fontSize: 30,
|
|
|
|
width: 200,
|
|
|
|
},
|
|
|
|
widthInput: {
|
|
|
|
width: 200,
|
|
|
|
borderWidth: 1,
|
|
|
|
borderRadius: 4,
|
|
|
|
borderColor: "grey",
|
|
|
|
padding: 4,
|
|
|
|
margin: 4,
|
|
|
|
fontSize: 30,
|
|
|
|
},
|
|
|
|
activeProduct: {
|
|
|
|
borderWidth: 2,
|
|
|
|
borderColor: "black",
|
|
|
|
borderStyle: "solid",
|
|
|
|
},
|
|
|
|
inactiveProduct: {
|
|
|
|
|
|
|
|
},
|
2024-06-26 04:20:08 +02:00
|
|
|
titleContainer: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
gap: 8,
|
|
|
|
},
|
|
|
|
stepContainer: {
|
|
|
|
gap: 8,
|
|
|
|
marginBottom: 8,
|
|
|
|
},
|
|
|
|
reactLogo: {
|
|
|
|
height: 178,
|
|
|
|
width: 290,
|
|
|
|
bottom: 0,
|
|
|
|
left: 0,
|
|
|
|
},
|
2024-06-29 15:09:22 +02:00
|
|
|
|
|
|
|
productSelectorFlatList: {
|
|
|
|
padding: 10,
|
|
|
|
margin: 10,
|
|
|
|
},
|
|
|
|
|
|
|
|
productTileTouchable: {
|
|
|
|
margin: 10,
|
|
|
|
padding: 20,
|
|
|
|
backgroundColor: "grey",
|
|
|
|
},
|
|
|
|
|
|
|
|
productTileTouchableActive: {
|
|
|
|
borderWidth: 2,
|
|
|
|
borderStyle: "solid",
|
|
|
|
borderColor: "black",
|
|
|
|
margin: 10,
|
|
|
|
padding: 20,
|
|
|
|
},
|
|
|
|
|
|
|
|
productTileText: {
|
|
|
|
textAlign: "center",
|
|
|
|
color: "white",
|
|
|
|
},
|
|
|
|
|
|
|
|
productTileTextActive: {
|
|
|
|
textAlign: "center",
|
|
|
|
color: "black",
|
|
|
|
},
|
|
|
|
|
|
|
|
productTileCover: {
|
|
|
|
padding: 4,
|
|
|
|
},
|
|
|
|
|
2024-06-26 04:20:08 +02:00
|
|
|
});
|