diff --git a/src/admin/pages/InvoiceDetail.tsx b/src/admin/pages/InvoiceDetail.tsx index 6bad19f..c6ef87d 100644 --- a/src/admin/pages/InvoiceDetail.tsx +++ b/src/admin/pages/InvoiceDetail.tsx @@ -49,7 +49,7 @@ import useDocumentDraft, { readDraft } from "../hooks/useDocumentDraft"; import { DRAFT_KEYS } from "../lib/draftKeys"; import { companySettingsOptions } from "../lib/queries/settings"; import { invoiceDetailOptions } from "../lib/queries/invoices"; -import { offerCustomersOptions, type Customer } from "../lib/queries/offers"; +import { offerCustomersOptions } from "../lib/queries/offers"; import { bankAccountsOptions } from "../lib/queries/common"; import { jsonQuery } from "../lib/apiAdapter"; import { formatCurrency, formatDate, todayLocalStr } from "../utils/formatters"; @@ -67,6 +67,7 @@ import { LoadingState, PageEnter, RichTextView, + CustomerPicker, headerActionsSx, } from "../ui"; import { @@ -606,9 +607,6 @@ export default function InvoiceDetail() { const [invoiceNumber, setInvoiceNumber] = useState(""); const initialSnapshotRef = useRef(null); - const [customerSearch, setCustomerSearch] = useState(""); - const [showCustomerDropdown, setShowCustomerDropdown] = useState(false); - const companySettings = useQuery(companySettingsOptions()).data; useEffect(() => { @@ -900,26 +898,6 @@ export default function InvoiceDetail() { return addDaysLocalStr(dueDays, form.issue_date); }, [form.issue_date, dueDays]); - // ─── Create mode: customer filtering ─── - const filteredCustomers = useMemo(() => { - if (!customerSearch) return customers; - const q = customerSearch.toLowerCase(); - return customers.filter( - (c) => - (c.name || "").toLowerCase().includes(q) || - (c.company_id || "").includes(customerSearch) || - (c.city || "").toLowerCase().includes(q), - ); - }, [customers, customerSearch]); - - useEffect(() => { - const handleClickOutside = () => setShowCustomerDropdown(false); - if (showCustomerDropdown) { - document.addEventListener("click", handleClickOutside); - return () => document.removeEventListener("click", handleClickOutside); - } - }, [showCustomerDropdown]); - const selectBankAccount = (accountId: string) => { const acc = bankAccounts.find((a) => a.id === Number(accountId)); if (acc) { @@ -943,15 +921,14 @@ export default function InvoiceDetail() { } }; - const selectCustomer = (customer: Customer) => { + const selectCustomer = (id: number | null) => { + const customer = id != null ? customers.find((c) => c.id === id) : null; setForm((prev) => ({ ...prev, - customer_id: customer.id, - customer_name: customer.name, + customer_id: id, + customer_name: customer?.name ?? "", })); setErrors((prev) => ({ ...prev, customer_id: "" })); - setCustomerSearch(""); - setShowCustomerDropdown(false); }; // ─── Create mode: items management ─── @@ -1823,115 +1800,13 @@ export default function InvoiceDetail() { /> - {form.customer_id ? ( - - - {form.customer_name} - - - setForm((prev) => ({ - ...prev, - customer_id: null, - customer_name: "", - })) - } - title="Odebrat zákazníka" - aria-label="Odebrat zákazníka" - > - - - - - - - ) : ( - e.stopPropagation()} - > - { - setCustomerSearch(e.target.value); - setShowCustomerDropdown(true); - }} - onFocus={() => setShowCustomerDropdown(true)} - placeholder="Hledat zákazníka (název, IČ, město)..." - autoComplete="off" - /> - {showCustomerDropdown && ( - - {filteredCustomers.length === 0 ? ( - - Žádní zákazníci - - ) : ( - filteredCustomers.slice(0, 10).map((c) => ( - selectCustomer(c)} - sx={{ - px: 1.5, - py: 1, - cursor: "pointer", - "&:hover": { bgcolor: "action.hover" }, - }} - > - {c.name} - {(c.company_id || c.city) && ( - - {c.company_id && `IČ: ${c.company_id}`} - {c.city && ` · ${c.city}`} - - )} - - )) - )} - - )} - - )} + { - const cid = val ? Number(val) : null; - const c = customers.find((x: Customer) => x.id === cid); + const selectCustomer = (id: number | null) => { + const c = id != null ? customers.find((x: Customer) => x.id === id) : null; setForm((prev) => ({ ...prev, - customer_id: cid, + customer_id: id, customer_name: c?.name || "", })); setErrors((prev) => ({ ...prev, customer_id: "" })); @@ -956,19 +956,13 @@ export default function IssuedOrderDetail() { }} > -