Skip to content

Commit

Permalink
Get initial location from local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
timmo001 committed Sep 2, 2024
1 parent d86f735 commit 3bdf92d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/app/_components/location-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ import {
import { Input } from "~/components/ui/input";
import { LocationSchema } from "~/lib/schema";

type Location = z.infer<typeof LocationSchema>;

export function LocationForm() {
const form = useForm<z.infer<typeof LocationSchema>>({
const form = useForm<Location>({
resolver: zodResolver(LocationSchema),
defaultValues: {
latitude: 32,
longitude: 104.9,
defaultValues: async () => {
const data = localStorage.getItem("location");
if (data) {
try {
return JSON.parse(data);
} catch (e) {
console.error("Error parsing location data:", e);
}
}
return {
latitude: 32,
longitude: 104.9,
};
},
});

Expand All @@ -39,14 +51,18 @@ export function LocationForm() {
}

function onSetLocation() {
const data: z.infer<typeof LocationSchema> = {
const data: Location = {
latitude: Number(form.getValues("latitude")),
longitude: Number(form.getValues("longitude")),
};
console.log("Update location:", data);
localStorage.setItem("location", JSON.stringify(data));
}

if (form.formState.isLoading) {
return <span>Loading...</span>;
}

return (
<Form {...form}>
<form className="flex flex-row flex-wrap gap-4">
Expand Down

0 comments on commit 3bdf92d

Please sign in to comment.