Map inventory CSV and recover a lost acknowledgement
Read a file or paste CSV, map typed fields and review one atomic batch. A local receipt fixture demonstrates retry without creating duplicate records.
Keyboard
- Use Tab through source and mapping controls. Open choices with Enter; Escape dismisses.
- Row-error links move to the original CSV line in the paged preview.
- Review and confirm the batch before Apply. If acknowledgement is lost, Check import outcome reads the stored receipt; Retry reuses the same request ID.
States covered
- quoted CSV
- typed column mapping
- required fields
- duplicate policies
- paginated preview
- atomic apply callback
- lost acknowledgement
- idempotent retry
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-import-create-reviewed-batch.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import {useRef,useState} from 'react';
import {Checkbox,ImportWizard,UIProvider,type ImportField,type ImportRequest,type ImportApplyResult} from '@bld8/ui';
const fields:ImportField[]=[{key:'id',label:'ID',type:'text',required:true,maxLength:40},{key:'name',label:'Name',type:'text',required:true,maxLength:120},{key:'quantity',label:'Quantity',type:'number',required:true,min:0,max:10000,validate:value=>Number.isInteger(value)?undefined:'Use a whole quantity.'},{key:'restock',label:'Restock',type:'date'},{key:'approved',label:'Approved',type:'boolean'}];
export default function CreateImportExample(){
const rows=useRef(new Map<string,Record<string,unknown>>()),receipts=useRef(new Map<string,{signature:string;count:number}>());const [lost,setLost]=useState(false),[reject,setReject]=useState(false),[saved,setSaved]=useState<string[]>([]),[attempts,setAttempts]=useState<string[]>([]);
async function apply(request:ImportRequest,{signal}:{signal:AbortSignal}):Promise<ImportApplyResult>{setAttempts(current=>[...current,request.requestId]);const signature=JSON.stringify(request.rows),receipt=receipts.current.get(request.requestId);if(receipt)return receipt.signature===signature?{ok:true,requestId:request.requestId,count:receipt.count}:{ok:false,message:'This request ID has different content.'};await new Promise(resolve=>setTimeout(resolve,250));if(signal.aborted)throw new DOMException('Aborted','AbortError');if(reject){setReject(false);return {ok:false,message:'The service rejected this batch. No rows were stored.',issues:[{sourceRow:request.rows[0].sourceRow,field:'quantity',message:'Review the quantity against your current stock policy.'}]};}for(const row of request.rows)if(rows.current.has(String(row.values.id)))return {ok:false,message:'An ID already exists. This create import was not applied.',issues:[{sourceRow:row.sourceRow,field:'id',message:'Use a new identity or an update import.'}]};const next=new Map(rows.current);for(const row of request.rows)next.set(String(row.values.id),{...row.values});rows.current=next;receipts.current.set(request.requestId,{signature,count:request.rows.length});setSaved([...next.keys()]);if(lost){setLost(false);throw Error('Illustrative acknowledgement loss');}return {ok:true,requestId:request.requestId,count:request.rows.length};}
async function reconcile(request:ImportRequest):Promise<ImportApplyResult>{const receipt=receipts.current.get(request.requestId);return receipt&&receipt.signature===JSON.stringify(request.rows)?{ok:true,requestId:request.requestId,count:receipt.count}:{ok:false,message:'No matching receipt exists in this local fixture. No import was stored.'};}
return <UIProvider><div style={{display:'grid',gap:24}}><div style={{display:'flex',flexWrap:'wrap',gap:20}}><Checkbox label="Lose next acknowledgement" checked={lost} onCheckedChange={setLost}/><Checkbox label="Reject next batch" checked={reject} onCheckedChange={setReject}/></div><ImportWizard label="Import inventory" fields={fields} identityField="id" onApply={apply} onReconcile={reconcile}/><output aria-live="polite">Saved IDs: {saved.join(', ')||'none'}</output><p data-import-attempts style={{overflowWrap:'anywhere',fontSize:12}}>Request attempts: {attempts.join(' · ')||'none'}</p><p style={{fontSize:12}}>Local in-memory records and receipts demonstrate the contract. They reset on reload. A real service owns authorization, durable idempotency, identity checks and atomic persistence.</p></div></UIProvider>;
}