pogdark-app/components/Location.tsx

211 lines
8.7 KiB
TypeScript
Raw Normal View History

2025-04-25 05:01:04 +00:00
import React, { useState } from "react";
2025-04-27 14:09:04 +00:00
import {Dialog, TextInput, useTheme, Button, Text, List, Portal} from "react-native-paper";
2025-04-27 03:00:56 +00:00
import { View, FlatList } from "react-native";
2025-04-26 17:28:33 +00:00
import Slider from "@react-native-community/slider";
2025-04-25 04:44:06 +00:00
import axios from "axios";
import * as Location from "expo-location";
2025-04-27 03:00:56 +00:00
import log from "@/util/log";
2025-04-25 05:01:04 +00:00
2025-04-27 14:09:04 +00:00
interface LocationScreenProps {
visible: boolean;
setChanged: (dataChanged: boolean) => void;
setTheme: (theme: string) => void;
currentTheme: string;
onClose: () => void;
}
2025-04-25 05:01:04 +00:00
export const API_URL = process.env.EXPO_PUBLIC_API_URL;
2025-04-25 05:22:42 +00:00
const BUTTON_WIDTH = 260;
2025-04-27 03:00:56 +00:00
type Park = {
2025-04-27 14:32:53 +00:00
Id: string;
2025-04-27 03:00:56 +00:00
name: string;
address: string;
city: string;
state: string;
zip: string;
location: {
coordinates: [number, number];
};
};
2025-04-27 14:09:04 +00:00
const LocationScreen: React.FC<LocationScreenProps> = ({ visible, setTheme, currentTheme, setChanged, onClose }) => {
2025-04-25 04:44:06 +00:00
const theme = useTheme();
2025-04-25 05:01:04 +00:00
const [zip, setZip] = useState("");
2025-04-27 03:00:56 +00:00
const [distance, setDistance] = useState(25);
2025-04-25 05:01:04 +00:00
const [loading, setLoading] = useState(false);
const [locLoading, setLocLoading] = useState(false);
2025-04-27 03:00:56 +00:00
const [parks, setParks] = useState<Park[]>([]);
const [hasSearched, setHasSearched] = useState(false);
2025-04-25 05:01:04 +00:00
2025-04-26 17:20:02 +00:00
// Call the parks endpoint with coordinates
const fetchNearbyParks = async (longitude: number, latitude: number) => {
try {
2025-04-26 17:28:33 +00:00
const response = await axios.post(API_URL + "/parkLookup", { "Lon": longitude, "Lat": latitude, "Dist": distance });
2025-04-27 03:00:56 +00:00
log.error("Nearby Parks:", response.data);
setParks(response.data);
2025-04-26 17:20:02 +00:00
} catch (err) {
log.error("Nearby Parks Error:", err);
2025-04-27 03:00:56 +00:00
setParks([]);
} finally {
setHasSearched(true);
2025-04-26 17:20:02 +00:00
}
};
// Zip code handler
2025-04-25 05:01:04 +00:00
const handleSubmit = async () => {
if (!zip) return;
setLoading(true);
2025-04-25 05:01:04 +00:00
try {
const response = await axios.post(API_URL + "/zipLookup", { zip });
2025-04-26 17:20:02 +00:00
log.error("Zip Lookup Response:", response.data);
const longitude = response.data[0];
const latitude = response.data[1];
await fetchNearbyParks(longitude, latitude);
2025-04-25 05:01:04 +00:00
} catch (err) {
log.error(err);
}
setLoading(false);
};
2025-04-25 04:44:06 +00:00
2025-04-26 17:20:02 +00:00
// 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]);
2025-04-26 17:20:02 +00:00
await fetchNearbyParks(longitude, latitude);
} catch (err) {
log.error("Location Error:", err);
}
setLocLoading(false);
};
2025-04-27 03:00:56 +00:00
// Render each park item
const renderItem = ({ item }: { item: Park }) => (
<List.Item
title={item.name}
description={
`${item.address}\n${item.city}, ${item.state} ${item.zip}`
}
left={props => <List.Icon {...props} icon="paw" />}
style={{
backgroundColor: theme.colors.surface,
borderRadius: 8,
marginBottom: 8,
elevation: 1,
}}
titleStyle={{ fontWeight: "bold" }}
descriptionNumberOfLines={2}
/>
);
return (
2025-04-27 14:09:04 +00:00
<Portal>
<Dialog visible={visible}
onDismiss={() => {
onClose();
2025-04-25 05:22:42 +00:00
}}
2025-04-27 14:09:04 +00:00
style={{ backgroundColor: theme.colors.background }}>
<Dialog.Title style={{color: theme.colors.primary, textAlign: 'center'}}>Location</Dialog.Title>
<Dialog.Content style={{ maxHeight: 500, justifyContent: "center", alignItems: "center" }}>
<View style={{ alignItems: "center", width: "100%" }}>
<Button mode="outlined" onPress={handleGetLocation} loading={locLoading} disabled={locLoading}
style={{ marginBottom: 12, width: BUTTON_WIDTH }}>Use My Location</Button>
<Text style={{ marginBottom: 12, color: theme.colors.primary, fontWeight: "bold" }}>OR</Text>
<View style={{
flexDirection: "row",
alignItems: "center",
width: BUTTON_WIDTH,
justifyContent: "center",
marginBottom: 4,
}}
>
<TextInput
label="Enter Zip Code"
mode="outlined"
value={zip}
onChangeText={setZip}
style={{
flex: 3,
marginRight: 6,
fontFamily: "SpaceReg",
minWidth: 0,
}}
placeholderTextColor={theme.colors.primary}
textColor={theme.colors.primary}
theme={{ colors: { text: theme.colors.primary } }}
2025-04-27 03:00:56 +00:00
/>
2025-04-27 14:09:04 +00:00
<Button
mode="contained"
onPress={handleSubmit}
loading={loading}
disabled={!zip || loading}
style={{
flex: 2,
minWidth: 0,
height: 50,
}}
contentStyle={{ height: 50 }}
>
Submit
</Button>
</View>
<View style={{ width: BUTTON_WIDTH, marginTop: 10, alignItems: "center" }}>
<Text style={{ color: theme.colors.primary, marginBottom: 2 }}>
Distance: {distance} mile{distance !== 1 ? "s" : ""}
2025-04-27 03:00:56 +00:00
</Text>
2025-04-27 14:09:04 +00:00
<Slider
style={{ width: "100%", height: 36 }}
minimumValue={1}
maximumValue={40}
step={1}
value={distance}
onValueChange={setDistance}
minimumTrackTintColor={theme.colors.primary}
maximumTrackTintColor={theme.colors.onSurfaceDisabled || "#ccc"}
thumbTintColor={theme.colors.primary}
/>
</View>
{/* Parks List */}
{hasSearched && (
<View style={{ width: BUTTON_WIDTH, marginTop: 18, maxHeight: 200 }}>
<Text style={{
fontWeight: "bold",
fontSize: 18,
marginBottom: parks != null ? 8 : 2,
color: theme.colors.primary
}}>
Nearby Dogparks
</Text>
{parks != null ? (
<FlatList
data={parks}
keyExtractor={(_, idx) => String(idx)}
renderItem={renderItem}
showsVerticalScrollIndicator={false}
style={{ maxHeight: 170 }}
/>
) : (
<Text style={{ color: theme.colors.onSurface, textAlign: "center", marginTop: 12 }}>
No parks found in range.
</Text>
)}
</View>
2025-04-27 03:00:56 +00:00
)}
</View>
2025-04-27 14:09:04 +00:00
</Dialog.Content>
</Dialog>
</Portal>
2025-04-25 05:01:04 +00:00
);
};
2025-04-27 14:09:04 +00:00
export default LocationScreen;