pogdark-app/components/Location.tsx

120 lines
4.4 KiB
TypeScript
Raw Normal View History

2025-04-25 05:01:04 +00:00
import React, { useState } from "react";
2025-04-25 05:06:43 +00:00
import { Dialog, TextInput, useTheme, Button, Text } from "react-native-paper";
2025-04-25 05:01:04 +00:00
import { View } from "react-native";
2025-04-25 04:44:06 +00:00
import axios from "axios";
import * as Location from "expo-location";
2025-04-25 05:01:04 +00:00
import log from "@/util/log"
export const API_URL = process.env.EXPO_PUBLIC_API_URL;
2025-04-25 05:22:42 +00:00
const BUTTON_WIDTH = 260;
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("");
const [loading, setLoading] = useState(false);
const [locLoading, setLocLoading] = 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 {
const range = 25;
const response = await axios.post(API_URL + "/parkLookup", { "Lon": longitude, "Lat": latitude, "Dist": range });
log.error("Nearby Parks:", response.data); // handle parks as you need
} catch (err) {
log.error("Nearby Parks Error:", err);
}
};
// 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);
};
return (
2025-04-25 05:06:43 +00:00
<Dialog.Content style={{ maxHeight: 360, justifyContent: "center", alignItems: "center" }}>
<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-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-25 04:44:06 +00:00
</View>
</Dialog.Content>
2025-04-25 05:01:04 +00:00
);
};
2025-04-26 17:20:02 +00:00
export default LocationComponent;