fix(po): pre-fill vendor from cart when all items share the same vendor

This commit is contained in:
Hardik 2026-05-16 00:09:15 +05:30
parent d769cae71e
commit 42c58d8c15
2 changed files with 14 additions and 3 deletions

View file

@ -20,13 +20,15 @@ interface Props {
accounts: Account[]; accounts: Account[];
vendors: Vendor[]; vendors: Vendor[];
initialLineItems?: LineItemInput[]; initialLineItems?: LineItemInput[];
initialVendorId?: string;
} }
export function NewPoForm({ vessels, accounts, vendors, initialLineItems }: Props) { export function NewPoForm({ vessels, accounts, vendors, initialLineItems, initialVendorId }: Props) {
const router = useRouter(); const router = useRouter();
const [lineItems, setLineItems] = useState<LineItemInput[]>( const [lineItems, setLineItems] = useState<LineItemInput[]>(
initialLineItems && initialLineItems.length > 0 ? initialLineItems : [EMPTY_LINE] initialLineItems && initialLineItems.length > 0 ? initialLineItems : [EMPTY_LINE]
); );
const [vendorId, setVendorId] = useState(initialVendorId ?? "");
const [files, setFiles] = useState<File[]>([]); const [files, setFiles] = useState<File[]>([]);
const [submitting, setSubmitting] = useState<"draft" | "submit" | null>(null); const [submitting, setSubmitting] = useState<"draft" | "submit" | null>(null);
const [error, setError] = useState(""); const [error, setError] = useState("");
@ -189,7 +191,12 @@ export function NewPoForm({ vessels, accounts, vendors, initialLineItems }: Prop
<label className="block text-sm font-medium text-neutral-700 mb-1.5"> <label className="block text-sm font-medium text-neutral-700 mb-1.5">
Vendor (optional can be added later) Vendor (optional can be added later)
</label> </label>
<select name="vendorId" className={INPUT_CLS}> <select
name="vendorId"
value={vendorId}
onChange={(e) => setVendorId(e.target.value)}
className={INPUT_CLS}
>
<option value="">No vendor selected</option> <option value="">No vendor selected</option>
{vendors.map((v) => ( {vendors.map((v) => (
<option key={v.id} value={v.id}> <option key={v.id} value={v.id}>

View file

@ -24,6 +24,7 @@ export default async function NewPoPage({ searchParams }: Props) {
const { cart } = await searchParams; const { cart } = await searchParams;
let initialLineItems: LineItemInput[] | undefined; let initialLineItems: LineItemInput[] | undefined;
let initialVendorId: string | undefined;
if (cart) { if (cart) {
try { try {
const cartItems: CartItem[] = JSON.parse(decodeURIComponent(cart)); const cartItems: CartItem[] = JSON.parse(decodeURIComponent(cart));
@ -38,6 +39,9 @@ export default async function NewPoPage({ searchParams }: Props) {
gstRate: 0.18, gstRate: 0.18,
productId: item.productId, productId: item.productId,
})); }));
// Pre-fill vendor only when all items share the same vendor
const vendorIds = [...new Set(cartItems.map((i) => i.vendorId).filter(Boolean))];
if (vendorIds.length === 1) initialVendorId = vendorIds[0];
} }
} catch { } catch {
// malformed cart param — ignore and start empty // malformed cart param — ignore and start empty
@ -58,7 +62,7 @@ export default async function NewPoPage({ searchParams }: Props) {
Fill in the details below. You can save as draft or submit directly for approval. Fill in the details below. You can save as draft or submit directly for approval.
</p> </p>
</div> </div>
<NewPoForm vessels={vessels} accounts={accounts} vendors={vendors} initialLineItems={initialLineItems} /> <NewPoForm vessels={vessels} accounts={accounts} vendors={vendors} initialLineItems={initialLineItems} initialVendorId={initialVendorId} />
</div> </div>
); );
} }