pogdark-app/components/Location.tsx

201 lines
7.5 KiB
TypeScript
Raw Normal View History

2025-04-25 05:01:04 +00:00
import React, { useState } from "react";
2025-04-27 03:00:56 +00:00
import { Dialog, TextInput, useTheme, Button, Text, List } from "react-native-paper";
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
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 = {
name: string;
address: string;
city: string;
state: string;
zip: string;
location: {
coordinates: [number, number];
};
};
const LocationComponent = () => {
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 03:00:56 +00:00
<Dialog.Content style={{ maxHeight: 500, justifyContent: "center", alignItems: "center" }}>
2025-04-25 05:06:43 +00:00
<View style={{ alignItems: "center", width: "100%" }}>
<Button
mode="outlined"
onPress={handleGetLocation}
loading={locLoading}
disabled={locLoading}
2025-04-25 05:22:42 +00:00
style={{ marginBottom: 12, width: BUTTON_WIDTH }}
>
Use My Location
</Button>
2025-04-25 05:06:43 +00:00
<Text style={{ marginBottom: 12, color: theme.colors.primary, fontWeight: "bold" }}>OR</Text>
2025-04-25 05:22:42 +00:00
<View
style={{
flexDirection: "row",
alignItems: "center",
width: BUTTON_WIDTH,
justifyContent: "center",
2025-04-26 17:28:33 +00:00
marginBottom: 4,
2025-04-25 05:22:42 +00:00
}}
>
2025-04-25 05:06:43 +00:00
<TextInput
label="Enter Zip Code"
mode="outlined"
value={zip}
onChangeText={setZip}
2025-04-25 05:22:42 +00:00
style={{
flex: 3,
marginRight: 6,
fontFamily: "SpaceReg",
minWidth: 0,
}}
2025-04-25 05:06:43 +00:00
placeholderTextColor={theme.colors.primary}
textColor={theme.colors.primary}
theme={{ colors: { text: theme.colors.primary } }}
/>
<Button
mode="contained"
onPress={handleSubmit}
loading={loading}
disabled={!zip || loading}
2025-04-25 05:22:42 +00:00
style={{
flex: 2,
minWidth: 0,
height: 50,
}}
contentStyle={{ height: 50 }}
2025-04-25 05:06:43 +00:00
>
Submit
</Button>
</View>
2025-04-26 17:28:33 +00:00
<View style={{ width: BUTTON_WIDTH, marginTop: 10, alignItems: "center" }}>
<Text style={{ color: theme.colors.primary, marginBottom: 2 }}>
Distance: {distance} mile{distance !== 1 ? "s" : ""}
</Text>
<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>
2025-04-27 03:00:56 +00:00
{/* 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-25 04:44:06 +00:00
</View>
</Dialog.Content>
2025-04-25 05:01:04 +00:00
);
};
2025-04-26 17:28:33 +00:00
export default LocationComponent;