import { useEffect, useState } from "react";
import {
TouchableOpacity,
Image,
Text,
View,
ScrollView,
Alert,
} from "react-native";
import { useNavigation, Stack, useLocalSearchParams } from "expo-router";
import * as FileSystem from "expo-file-system";
import { styled } from "nativewind";
import StoreItem from "../../components/StoreItem";
import handleAddItem from "./handleAddItem";
import toUTM from "./toUTM";
import uploadImage from "./uploadImage";
declare function isNaN(x: string | number): boolean;
interface storeItemInterface {
key: String;
lastUpdated: String;
name: String;
perFloz: Number;
price: Number;
store: String;
volumeFloz: Number;
}
export default function () {
const [storeKey, setStoreKey] = useState("");
const [storeName, setStoreName] = useState("");
const [storeItems, setStoreItems] = useState([]);
const [image, setImage] = useState(null);
const navigation = useNavigation();
const StyledText = styled(Text);
const coords = useLocalSearchParams().coords.toString().split("%2B");
const lon = coords[0];
const lat = coords[1];
const utmCoords = toUTM(lat, lon);
const getStore = async () => {
const res = await fetch(
`${process.env.EXPO_PUBLIC_BACKEND_URL}/?` +
new URLSearchParams({
easting: utmCoords.Easting,
northing: utmCoords.Northing,
zone: utmCoords.ZoneNumber,
})
);
if (!res.ok)
return Alert.alert(
"Error!",
"Server error. Please report to ak95@riseup.net"
);
const json = await res.json();
setStoreName(json.name);
setStoreKey(json.key.toString());
const imageKey = json.key.toString();
const items = json.items.map((item: storeItemInterface, index: number) => {
const date = new Date(item.lastUpdated.replace(" ", "T") + "Z");
return (
);
});
setStoreItems(items);
const imageURL = `${process.env.EXPO_PUBLIC_BACKEND_URL}/img?imageKey=${imageKey}`;
await FileSystem.downloadAsync(
imageURL,
FileSystem.documentDirectory + imageKey
)
.then(({ uri }) => {
setImage(uri);
})
.catch(() => {});
console.log(image);
};
useEffect(() => {
return navigation.addListener("focus", () => getStore());
}, [navigation]);
return (
{
uploadImage(storeKey).then((uri) => {
setImage(uri);
});
}}
>
{image ? (
) : (
)}
{storeName}
{storeItems}
handleAddItem(storeKey)}
>
Add
);
}