import React, { useState } from "react"; import {Dialog, TextInput, useTheme, Button, Text, List, Portal} from "react-native-paper"; import { View, FlatList } from "react-native"; import Slider from "@react-native-community/slider"; import axios from "axios"; import * as Location from "expo-location"; import log from "@/util/log"; interface LocationScreenProps { visible: boolean; setChanged: (dataChanged: boolean) => void; setTheme: (theme: string) => void; currentTheme: string; onClose: () => void; } export const API_URL = process.env.EXPO_PUBLIC_API_URL; const BUTTON_WIDTH = 260; type Park = { name: string; address: string; city: string; state: string; zip: string; location: { coordinates: [number, number]; }; }; const LocationScreen: React.FC = ({ visible, setTheme, currentTheme, setChanged, onClose }) => { const theme = useTheme(); const [zip, setZip] = useState(""); const [distance, setDistance] = useState(25); const [loading, setLoading] = useState(false); const [locLoading, setLocLoading] = useState(false); const [parks, setParks] = useState([]); const [hasSearched, setHasSearched] = useState(false); // Call the parks endpoint with coordinates const fetchNearbyParks = async (longitude: number, latitude: number) => { try { const response = await axios.post(API_URL + "/parkLookup", { "Lon": longitude, "Lat": latitude, "Dist": distance }); log.error("Nearby Parks:", response.data); setParks(response.data); } catch (err) { log.error("Nearby Parks Error:", err); setParks([]); } finally { setHasSearched(true); } }; // Zip code handler const handleSubmit = async () => { if (!zip) return; setLoading(true); try { const response = await axios.post(API_URL + "/zipLookup", { zip }); log.error("Zip Lookup Response:", response.data); const longitude = response.data[0]; const latitude = response.data[1]; await fetchNearbyParks(longitude, latitude); } catch (err) { log.error(err); } setLoading(false); }; // Geolocation handler const handleGetLocation = async () => { setLocLoading(true); try { let { status } = await Location.requestForegroundPermissionsAsync(); if (status !== 'granted') { log.error("Permission to access location was denied"); setLocLoading(false); return; } let location = await Location.getCurrentPositionAsync({}); const { longitude, latitude } = location.coords; log.error([longitude, latitude]); await fetchNearbyParks(longitude, latitude); } catch (err) { log.error("Location Error:", err); } setLocLoading(false); }; // Render each park item const renderItem = ({ item }: { item: Park }) => ( } style={{ backgroundColor: theme.colors.surface, borderRadius: 8, marginBottom: 8, elevation: 1, }} titleStyle={{ fontWeight: "bold" }} descriptionNumberOfLines={2} /> ); return ( { onClose(); }} style={{ backgroundColor: theme.colors.background }}> Location OR Distance: {distance} mile{distance !== 1 ? "s" : ""} {/* Parks List */} {hasSearched && ( Nearby Dogparks {parks != null ? ( String(idx)} renderItem={renderItem} showsVerticalScrollIndicator={false} style={{ maxHeight: 170 }} /> ) : ( No parks found in range. )} )} ); }; export default LocationScreen;