local-weather/components/LocationSearch.tsx

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-05-05 20:55:51 +00:00
"use client";
import { getLocation } from "@/app/actions";
import { useState } from "react";
import { useFormStatus } from "react-dom";
2024-05-04 21:04:30 +00:00
2024-05-05 20:55:51 +00:00
export default function LocationSearch(props: any) {
const [searchLocation, setSearchLocation] = useState("");
const setGeoLocation = props.setGeoLocation;
2024-05-05 20:55:51 +00:00
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchLocation(e.target.value);
};
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
const coordinates = getLocation(searchLocation);
setGeoLocation(coordinates);
2024-05-05 20:55:51 +00:00
};
const { pending } = useFormStatus();
2024-05-04 21:04:30 +00:00
return (
<>
<form onSubmit={handleSubmit}>
<input
id="location"
name="locationSearch"
type="text"
placeholder="enter a location to get the weather"
value={searchLocation}
onChange={handleChange}
/>
<button type="submit" disabled={pending}>
{pending ? "Searching..." : "Search"}
</button>
<label htmlFor="location" className="hidden">
Enter location to get the weather!
</label>
</form>
2024-05-05 20:55:51 +00:00
<p>{searchLocation}</p>
</>
2024-05-04 21:04:30 +00:00
);
}