Jelajahi Sumber

view payement section

Bernt Christian Egeland 7 bulan lalu
induk
melakukan
ba3338feb4

+ 24 - 14
src/features/vehicles/Components/service-detail/PaymentsSection.tsx

@@ -1,6 +1,6 @@
 "use client";
 "use client";
 
 
-import { useState } from "react";
+import { useRef, useState } from "react";
 import { Badge } from "@/components/ui/badge";
 import { Badge } from "@/components/ui/badge";
 import { Button } from "@/components/ui/button";
 import { Button } from "@/components/ui/button";
 import { Input } from "@/components/ui/input";
 import { Input } from "@/components/ui/input";
@@ -48,18 +48,26 @@ export function PaymentsSection({
   deletingPayment,
   deletingPayment,
 }: PaymentsSectionProps) {
 }: PaymentsSectionProps) {
   const [showForm, setShowForm] = useState(false);
   const [showForm, setShowForm] = useState(false);
+  const [paymentMethod, setPaymentMethod] = useState("other");
   const { formatDate } = useFormatDate();
   const { formatDate } = useFormatDate();
+  const formRef = useRef<HTMLDivElement>(null);
 
 
-  const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
-    e.preventDefault();
-    const formData = new FormData(e.currentTarget);
+  const handleSubmit = async () => {
+    if (!formRef.current) return;
+    const amount = formRef.current.querySelector<HTMLInputElement>('[name="paymentAmount"]');
+    const date = formRef.current.querySelector<HTMLInputElement>('[name="paymentDate"]');
+    const note = formRef.current.querySelector<HTMLInputElement>('[name="paymentNote"]');
+    if (!amount?.value || Number(amount.value) <= 0) return;
     const success = await onCreatePayment({
     const success = await onCreatePayment({
-      amount: Number(formData.get("paymentAmount")),
-      date: (formData.get("paymentDate") as string) || new Date().toISOString(),
-      method: formData.get("paymentMethod") as string,
-      note: (formData.get("paymentNote") as string) || undefined,
+      amount: Number(amount.value),
+      date: date?.value || new Date().toISOString(),
+      method: paymentMethod,
+      note: note?.value || undefined,
     });
     });
-    if (success) setShowForm(false);
+    if (success) {
+      setShowForm(false);
+      setPaymentMethod("other");
+    }
   };
   };
 
 
   return (
   return (
@@ -76,6 +84,7 @@ export function PaymentsSection({
         </div>
         </div>
         <div className="flex items-center gap-2">
         <div className="flex items-center gap-2">
           <Button
           <Button
+            type="button"
             variant="outline"
             variant="outline"
             size="sm"
             size="sm"
             className="h-7 text-xs"
             className="h-7 text-xs"
@@ -91,7 +100,7 @@ export function PaymentsSection({
             )}
             )}
             {manuallyPaid ? "Mark as Unpaid" : "Mark as Paid"}
             {manuallyPaid ? "Mark as Unpaid" : "Mark as Paid"}
           </Button>
           </Button>
-          <Button variant="outline" size="sm" className="h-7 text-xs" onClick={() => setShowForm(!showForm)}>
+          <Button type="button" variant="outline" size="sm" className="h-7 text-xs" onClick={() => setShowForm(!showForm)}>
             <Plus className="mr-1 h-3 w-3" />
             <Plus className="mr-1 h-3 w-3" />
             Record Payment
             Record Payment
           </Button>
           </Button>
@@ -107,7 +116,7 @@ export function PaymentsSection({
         </div>
         </div>
 
 
         {showForm && (
         {showForm && (
-          <form onSubmit={handleSubmit} className="space-y-3 rounded-lg border p-3">
+          <div ref={formRef} className="space-y-3 rounded-lg border p-3">
             <div className="grid gap-3 sm:grid-cols-2">
             <div className="grid gap-3 sm:grid-cols-2">
               <div className="space-y-1">
               <div className="space-y-1">
                 <Label htmlFor="paymentAmount" className="text-xs">Amount</Label>
                 <Label htmlFor="paymentAmount" className="text-xs">Amount</Label>
@@ -125,7 +134,7 @@ export function PaymentsSection({
               </div>
               </div>
               <div className="space-y-1">
               <div className="space-y-1">
                 <Label className="text-xs">Method</Label>
                 <Label className="text-xs">Method</Label>
-                <Select name="paymentMethod" defaultValue="other">
+                <Select value={paymentMethod} onValueChange={setPaymentMethod}>
                   <SelectTrigger><SelectValue /></SelectTrigger>
                   <SelectTrigger><SelectValue /></SelectTrigger>
                   <SelectContent>
                   <SelectContent>
                     <SelectItem value="cash">Cash</SelectItem>
                     <SelectItem value="cash">Cash</SelectItem>
@@ -141,7 +150,7 @@ export function PaymentsSection({
               </div>
               </div>
             </div>
             </div>
             <div className="flex gap-2">
             <div className="flex gap-2">
-              <Button type="submit" size="sm" disabled={paymentLoading}>
+              <Button type="button" size="sm" disabled={paymentLoading} onClick={handleSubmit}>
                 {paymentLoading && <Loader2 className="mr-1 h-3.5 w-3.5 animate-spin" />}
                 {paymentLoading && <Loader2 className="mr-1 h-3.5 w-3.5 animate-spin" />}
                 Save Payment
                 Save Payment
               </Button>
               </Button>
@@ -149,7 +158,7 @@ export function PaymentsSection({
                 Cancel
                 Cancel
               </Button>
               </Button>
             </div>
             </div>
-          </form>
+          </div>
         )}
         )}
 
 
         {payments.length > 0 && (
         {payments.length > 0 && (
@@ -175,6 +184,7 @@ export function PaymentsSection({
                     <td className="py-1.5 text-muted-foreground">{payment.note || "-"}</td>
                     <td className="py-1.5 text-muted-foreground">{payment.note || "-"}</td>
                     <td className="py-1.5">
                     <td className="py-1.5">
                       <Button
                       <Button
+                        type="button"
                         variant="ghost" size="icon"
                         variant="ghost" size="icon"
                         className="h-6 w-6 text-muted-foreground hover:text-destructive"
                         className="h-6 w-6 text-muted-foreground hover:text-destructive"
                         disabled={deletingPayment === payment.id}
                         disabled={deletingPayment === payment.id}