"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { useToast } from "@/components/ui/use-toast" import { getVehicles, submitGasFillup } from "@/app/actions" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { FuelIcon as GasPump } from "lucide-react" interface Vehicle { id: string name: string } export default function GasFillupForm() { const { toast } = useToast() const [name, setName] = useState("") const [vehicle, setVehicle] = useState("") const [mileage, setMileage] = useState("") const [fuelAmount, setFuelAmount] = useState("") const [vehicles, setVehicles] = useState([]) const [isLoading, setIsLoading] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false) useEffect(() => { async function loadVehicles() { setIsLoading(true) try { const vehicleData = await getVehicles() setVehicles(vehicleData) } catch (error) { toast({ title: "Error", description: "Failed to load vehicles. Please try again.", variant: "destructive", }) } finally { setIsLoading(false) } } loadVehicles() }, [toast]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsSubmitting(true) try { await submitGasFillup({ name, vehicleId: vehicle, mileage: Number(mileage), gallons: Number(fuelAmount), }) toast({ title: "Success", description: "Gas fillup has been recorded successfully.", }) // Reset form setName("") setVehicle("") setMileage("") setFuelAmount("") } catch (error) { toast({ title: "Error", description: "Failed to submit gas fillup. Please try again.", variant: "destructive", }) } finally { setIsSubmitting(false) } } return (
Gas Fill-up Form
setName(e.target.value)} required className="border-mdc-blue focus:ring-secondary" disabled={isSubmitting} />
setMileage(e.target.value)} required min="0" step="1" className="border-mdc-blue focus:ring-secondary" disabled={isSubmitting} />
setFuelAmount(e.target.value)} required min="0" step="0.001" className="border-mdc-blue focus:ring-secondary" disabled={isSubmitting} />
) }