@apptimate/ui 2.8.0 → 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import React, { useState, useEffect, useCallback, useMemo } from "react";
|
|
3
|
+
import React, { useState, useEffect, useCallback, useMemo, useRef } from "react";
|
|
4
4
|
import { cn } from "@apptimate/core-lib";
|
|
5
5
|
import { CreditCard, Banknote, Building, Trash2, Loader2, Split, Check, AlertCircle } from "lucide-react";
|
|
6
6
|
|
|
@@ -70,6 +70,12 @@ export function PaymentSection({ totalAmount, payments, onPaymentsChange, compac
|
|
|
70
70
|
const [bankAccounts, setBankAccounts] = useState<BankAccount[]>([]);
|
|
71
71
|
const [isLoading, setIsLoading] = useState(false);
|
|
72
72
|
const [selectionMode, setSelectionMode] = useState<"single" | "split">("single");
|
|
73
|
+
const paymentsRef = useRef(payments);
|
|
74
|
+
|
|
75
|
+
// Keep ref in sync
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
paymentsRef.current = payments;
|
|
78
|
+
}, [payments]);
|
|
73
79
|
|
|
74
80
|
// ── Load payment modes on mount ──
|
|
75
81
|
useEffect(() => {
|
|
@@ -84,7 +90,7 @@ export function PaymentSection({ totalAmount, payments, onPaymentsChange, compac
|
|
|
84
90
|
if (bankRes.is_success) setBankAccounts(bankRes.result || []);
|
|
85
91
|
|
|
86
92
|
// Auto-select cash if no payments yet
|
|
87
|
-
if (
|
|
93
|
+
if (paymentsRef.current.length === 0 && modes.length > 0) {
|
|
88
94
|
const cashMode = modes.find((m) => m.code === "cash" && m.is_active);
|
|
89
95
|
if (cashMode) {
|
|
90
96
|
onPaymentsChange([{
|
|
@@ -92,12 +98,21 @@ export function PaymentSection({ totalAmount, payments, onPaymentsChange, compac
|
|
|
92
98
|
amount: totalAmount, metadata: {},
|
|
93
99
|
}]);
|
|
94
100
|
}
|
|
101
|
+
} else if (paymentsRef.current.length > 1) {
|
|
102
|
+
setSelectionMode("split");
|
|
95
103
|
}
|
|
96
104
|
} finally { setIsLoading(false); }
|
|
97
105
|
};
|
|
98
106
|
load();
|
|
99
107
|
}, []);
|
|
100
108
|
|
|
109
|
+
// Also sync selectionMode if payments change from outside
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
if (payments.length > 1 && selectionMode === "single") {
|
|
112
|
+
setSelectionMode("split");
|
|
113
|
+
}
|
|
114
|
+
}, [payments.length, selectionMode]);
|
|
115
|
+
|
|
101
116
|
// ── Calculations ──
|
|
102
117
|
const isCreditPayment = useCallback((payment: PaymentEntry) => payment.mode_code === "credit", []);
|
|
103
118
|
const totalPaid = useMemo(() => payments.reduce((s, p) => s + (isCreditPayment(p) ? 0 : p.amount), 0), [payments, isCreditPayment]);
|