local-weather/components/LocationSearch.tsx
ChrQR 7b97a0eb65 Fixed some things.
Need to fix google places call. It is refreshing the page when receiving the data, thus resetting the state.

It's a job for tomorrow.
2024-05-06 23:22:57 +02:00

39 lines
1.2 KiB
TypeScript

"use client";
import { getLocation } from "@/app/actions";
import { useState } from "react";
import { useFormStatus } from "react-dom";
export default function LocationSearch(props: any) {
const [searchLocation, setSearchLocation] = useState("");
const setGeoLocation = props.setGeoLocation;
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchLocation(e.target.value);
};
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
const coordinates = getLocation(searchLocation);
setGeoLocation(coordinates);
};
const { pending } = useFormStatus();
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>
<p>{searchLocation}</p>
</>
);
}