-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathPVForecastForm.tsx
More file actions
112 lines (105 loc) · 3.13 KB
/
Copy pathPVForecastForm.tsx
File metadata and controls
112 lines (105 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { Button } from "@/components/ui/button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
const formSchema = z.object({
latitude: z.coerce
.number({ required_error: "Site latitude is required." })
.min(-90, {
message: "Latitude must be greater than or equal to -90.",
})
.max(90, {
message: "Latitude must be smaller than or equal to 90",
}),
longitude: z.coerce
.number({ required_error: "Site longitude is required." })
.min(-180, {
message: "Longitude must be greater than or equal to -180",
})
.max(180, {
message: "Longitude must be smaller than or equal to 180",
}),
capacity_kwp: z.coerce
.number({ required_error: "Site capacity is required." })
.gt(0, { message: "Site capacity must be greather than 0." }),
});
export function PVForecastForm({ updatePredictions }) {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
mode: "onChange",
defaultValues: {
latitude: 51.75,
longitude: -1.25,
capacity_kwp: 1.25,
},
});
async function onSubmit(values: z.infer<typeof formSchema>) {
const response = await fetch(`http://localhost:8000/forecast`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({site:values}),
});
const data = await response.json();
updatePredictions(data.predictions.power_kw);
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="latitude"
render={({ field }) => (
<FormItem>
<FormLabel>Latitude</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormDescription>The latitude of the site.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="longitude"
render={({ field }) => (
<FormItem>
<FormLabel>Longitude</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormDescription>The longitude of the site.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="capacity_kwp"
render={({ field }) => (
<FormItem>
<FormLabel>Capacity</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormDescription>The capacity (kwp) of the site.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Predict</Button>
</form>
</Form>
);
}