diff --git a/components/GasFillupForm.tsx b/components/GasFillupForm.tsx index 6a10bd1..5e9688c 100644 --- a/components/GasFillupForm.tsx +++ b/components/GasFillupForm.tsx @@ -8,7 +8,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@ 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" +import { FuelIcon as GasPump, Trash2 } from "lucide-react" interface Vehicle { id: string @@ -17,14 +17,21 @@ interface Vehicle { export default function GasFillupForm() { const { toast } = useToast() + + // Form state variables const [vehicle, setVehicle] = useState("") const [mileage, setMileage] = useState("") const [fuelAmount, setFuelAmount] = useState("") const [fuelCost, setFuelCost] = useState("") + const [fuelReceipt, setFuelReceipt] = useState(null) // Image file state + const [previewURL, setPreviewURL] = useState(null) // Image preview state + + // API-related state const [vehicles, setVehicles] = useState([]) const [isLoading, setIsLoading] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false) + // Fetch vehicles from API when the component loads useEffect(() => { async function loadVehicles() { setIsLoading(true) @@ -41,10 +48,37 @@ export default function GasFillupForm() { setIsLoading(false) } } - loadVehicles() }, [toast]) + // Allowed image formats for uploads (common phone formats) + const allowedFormats = ["image/png", "image/jpeg", "image/jpg", "image/heic", "image/heif", "image/webp"] + + // Handle file selection and validation + const handleFileChange = (e: React.ChangeEvent) => { + const file = e.target.files?.[0] || null + + if (file && !allowedFormats.includes(file.type)) { + toast({ + title: "Invalid file type", + description: "Please upload a valid image file (PNG, JPG, JPEG, HEIC, or WebP).", + variant: "destructive", + }) + setFuelReceipt(null) // Reset file selection + setPreviewURL(null) + return + } + + setFuelReceipt(file) + + // Create an object URL for preview + if (file) { + const url = URL.createObjectURL(file) + setPreviewURL(url) + } + } + + // Handle form submission const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsSubmitting(true) @@ -57,6 +91,7 @@ export default function GasFillupForm() { cost: Number(fuelCost), isFillToFull: true, missedFuelUp: false, + file: fuelReceipt, // Attach uploaded file }) toast({ @@ -64,11 +99,13 @@ export default function GasFillupForm() { description: "Gas fillup has been recorded successfully.", }) - // Reset form + // Reset form state after successful submission setVehicle("") setMileage("") setFuelAmount("") setFuelCost("") + setFuelReceipt(null) + setPreviewURL(null) } catch (error) { toast({ title: "Error", @@ -83,14 +120,18 @@ export default function GasFillupForm() { return (
+ {/* Form Header */}
Gas Fill-up Form
+ + {/* Form Content */}
+ {/* Vehicle Selection */}
- {/* Mileage, Fuel, Cost Inputs */} - + {/* Mileage Input */} +
+ + setMileage(e.target.value)} + required + min="0" + step="1" + className="border-mdc-blue focus:ring-secondary" + disabled={isSubmitting} + /> +
+ + {/* Fuel Amount Input */} +
+ + setFuelAmount(e.target.value)} + required + min="0" + step="0.001" + className="border-mdc-blue focus:ring-secondary" + disabled={isSubmitting} + /> +
+ + {/* Fuel Cost Input */} +
+ + setFuelCost(e.target.value)} + required + min="0" + step="0.01" + className="border-mdc-blue focus:ring-secondary" + disabled={isSubmitting} + /> +
+ + {/* File Upload Input */} +
+ + + {/* Show image preview if a file is selected */} + {previewURL && ( +
+ Receipt preview + +
+ )} +
+ + {/* Submit Button */}