"use client"; import { useState, useRef } from "react"; import Link from "next/link"; import { parseOrderCSV } from "@/lib/csv-parsers"; import { importOrdersBatch } from "@/actions/import-orders"; type PreviewRow = { customer_name: string; customer_email: string; customer_phone: string; stop_id: string; items: Array<{ product_id: string; quantity: number; fulfillment: string }>; _rowIndex: number; }; export default function SalesImportPage() { const [csvText, setCsvText] = useState(""); const [preview, setPreview] = useState(null); const [parseErrors, setParseErrors] = useState<{ row: number; error: string }[]>([]); const [importing, setImporting] = useState(false); const [result, setResult] = useState<{ imported: number; errors: { row: number; error: string }[] } | null>(null); const [brandId, setBrandId] = useState(""); const fileRef = useRef(null); const SAMPLE_CSV = `customer_name,customer_email,customer_phone,stop_id,product_id,quantity,fulfillment Jane Smith,jane@example.com,555-1234,{STOP_ID},{PRODUCT_ID},2,pickup John Doe,john@example.com,555-5678,{STOP_ID},{PRODUCT_ID},1,shipping `; function handleFile(e: React.ChangeEvent) { const file = e.target.files?.[0]; if (!file) return; const reader = new FileReader(); reader.onload = (ev) => { const text = ev.target?.result as string; setCsvText(text); handlePreview(text); }; reader.readAsText(file); } async function handlePreview(text?: string) { const toParse = text ?? csvText; if (!toParse.trim()) return; const parseResult = await parseOrderCSV(toParse); if (!parseResult.success) { setPreview(null); setParseErrors([{ row: 0, error: parseResult.error }]); return; } setPreview(parseResult.rows); setParseErrors(parseResult.errors); } async function handleImport() { if (!preview || !brandId) return; setImporting(true); const importResult = await importOrdersBatch( brandId, preview.map((r) => ({ customer_name: r.customer_name, customer_email: r.customer_email, customer_phone: r.customer_phone, stop_id: r.stop_id, items: r.items, })) ); setImporting(false); if (importResult.success) { setResult({ imported: importResult.imported, errors: importResult.errors }); } else { setResult({ imported: 0, errors: [{ row: 0, error: importResult.error }] }); } } return (
← Back to Orders

Import Orders

Upload a CSV to import orders from another platform. Uses existing stop and product IDs.

setBrandId(e.target.value)} placeholder="64294306-5f42-463d-a5e8-2ad6c81a96de" className="mt-1 w-full rounded-xl border border-stone-300 px-4 py-3 text-sm outline-none focus:border-blue-500 bg-white" />

Paste CSV content below: