Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions apps/web/app/(dashboard)/dashboard/invoices/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useForm } from '@tanstack/react-form';
import { ArrowLeftIcon } from 'lucide-react';
import Link from 'next/link';
import { useParams, useRouter } from 'next/navigation';
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import { toast } from 'sonner';
import { InvoiceGeneralSection } from '@/components/invoice-general-section';
import { InvoiceItemsEditor } from '@/components/invoice-items-editor';
Expand All @@ -20,8 +20,15 @@ export default function EditInvoicePage() {
const { id } = useParams<{ id: string }>();
const router = useRouter();
const { data: invoice, isLoading } = useInvoice(id);
const { data: clientsData } = useClients();
const clientList = clientsData?.data ?? [];
const { data: clientsData, isLoading: clientsLoading } = useClients({
limit: 100,
});
const clientList = useMemo(() => {
const list = clientsData?.data ?? [];
if (!invoice?.client) return list;
if (list.some((c) => c.id === invoice.clientId)) return list;
return [invoice.client, ...list];
}, [clientsData?.data, invoice?.client, invoice?.clientId]);
const updateInvoice = useUpdateInvoice();

const [items, setItems] = useState<InvoiceLineItem[]>([emptyItem()]);
Expand Down Expand Up @@ -69,7 +76,7 @@ export default function EditInvoicePage() {
unitPrice: i.unitPrice,
})),
);
}, [invoice, form]);
}, [invoice, form.setFieldValue]);

return (
<>
Expand All @@ -86,7 +93,7 @@ export default function EditInvoicePage() {
/>

<div className="flex flex-1 flex-col p-4 md:p-6">
{isLoading ? (
{isLoading || clientsLoading ? (
<EditSkeleton />
) : (
<form
Expand Down
12 changes: 11 additions & 1 deletion apps/web/components/invoice-general-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export function InvoiceGeneralSection({
onIssueDateChange,
onDueDateChange,
}: InvoiceGeneralSectionProps) {
// Radix Select ne met pas à jour l'affichage si la valeur est définie
// avant que l'option correspondante soit rendue — on remonte le composant
// une fois le client présent dans la liste.
const clientInList =
!clientId || clients?.some((c) => c.id === clientId) === true;

return (
<section className="rounded-xl border border-border bg-card p-5 flex flex-col gap-5">
<Text size="sm" weight="semibold">
Expand All @@ -59,7 +65,11 @@ export function InvoiceGeneralSection({
</Text>
</div>
) : (
<Select value={clientId} onValueChange={onClientChange}>
<Select
key={clientInList ? clientId || 'empty' : 'pending'}
value={clientInList && clientId ? clientId : undefined}
onValueChange={onClientChange}
>
<SelectTrigger
className={`w-full max-w-48 ${clientIdError ? 'border-destructive' : ''}`}
>
Expand Down
Loading