import React, { useState } from "react"; import { TouchableOpacity, Text, Image, View, ScrollView, TextInput, Appearance, Alert, } from "react-native"; import { Stack, useLocalSearchParams, router } from "expo-router"; import { styled } from "nativewind"; declare function isNaN(x: string | number): boolean; export default function () { const [itemName, onitemName] = useState(""); const [itemVolume, onitemVolume] = useState(""); const [itemPrice, onitemPrice] = useState(""); const { storeKey } = useLocalSearchParams(); const StyledText = styled(Text); const handleSubmit = (price: string, vol: string, name: string) => { if (price === "" || vol === "" || name === "") { Alert.alert( "Insufficient information!", "Please double-check to ensure all fields have been filled" ); } else { // send data to flask fetch( `${process.env.EXPO_PUBLIC_BACKEND_URL}/?` + new URLSearchParams({ storeKey: storeKey.toString(), itemName: name, itemPrice: price, itemVolume: vol, }), { method: "POST", mode: "cors", redirect: "follow", } ).then((res) => { if (res.ok) { Alert.alert( "Success!", "Item has been successfully added. Thank you!", [ { text: "OK", onPress: () => { router.back(); }, }, ] ); } else { switch (res.status) { case 400: Alert.alert( "Error!", "Please double-check all fields are filled in." ); break; case 409: Alert.alert( "Error!", "Duplicate of existing item. Please go to that item to edit its price.", [ { text: "OK", onPress: () => { router.push("/"); }, }, ] ); break; case 500: Alert.alert( "Error!", "Backend server error. Please report to ak95@riseup.net" ); break; default: Alert.alert( "Error!", "Unspecified error. Please report to ak95@riseup.net" ); break; } } }); } }; return ( { const split = text.split(""); let numeric = true; split.forEach((char: string | number) => { if (isNaN(char)) { numeric = false; } }); if (numeric) onitemVolume(text); }} placeholder="Item Volume (fl. oz.)" placeholderTextColor={ Appearance.getColorScheme() === "dark" ? "#fff" : "#000" } inputMode="numeric" value={itemVolume} /> { text = text.slice(2); if (text.includes(".")) { const split = text.split("."); if (split.length < 3) { const righthand = split[1]; if (righthand.length <= 2) { onitemPrice(text); } } } else onitemPrice(text); }} placeholder="Item Price" inputMode="decimal" placeholderTextColor={ Appearance.getColorScheme() === "dark" ? "#fff" : "#000" } value={`$ ${itemPrice}`} /> handleSubmit(itemPrice, itemVolume, itemName)} > Submit ); }