import { useEffect, useState } from '@wordpress/element'; import { vfAPI } from '../../lib/vfAPI'; import { Button, Flex, Modal, Spinner } from '@wordpress/components'; import { Breadcrumb } from '../../components/ui/Breadcrumb'; import { Notices } from '../../components/ui/Notices'; interface Batch { id: number; filename: string; status: 'pending'|'confirmed'|'processing'|'processed'; total_records: number; processed_records?: number; uploaded_at: string; started_at: string; finished_at: string; failures: { data: Failure[]; }; } interface Failure { id: number; customer_batch_id: number; row: number; failures: string[]; value: { [name:string]: string; } } export const CustomerUploadShow = () => { const initialBreadcrumb = [ { name: 'Customers', hash: '#!/customers' }, { name: 'Accounts', hash: '#!/customers/accounts' }, { name: 'Uploads', hash: '#!/customers/uploads' }, ]; const [ batch, setBatch ] = useState(); const [ breadcrumbs, setBreadcrumbs ] = useState( initialBreadcrumb ); const [ failureDetails, setFailureDetails ] = useState(); const [ id, setId ] = useState(); const [ isBusy, setBusy ] = useState( false ); const [ isOpen, setOpen ] = useState( false ); const [ notices, setNotices ] = useState( [] ); const checkId = () => { const lastHash = location.hash.split( '/' ).pop(); if ( lastHash !== 'new' ) { setId( Number( lastHash ) ); } }; const closeModal = () => setOpen( false ); const showCustomerUpload = () => { setBusy( true ); const url = `${ localized.apiURL }/admin/customers/batches/${ id }`; vfAPI.get( url ) .then( ( response ) => { setNotices( response.data.notices ); if ( response.data.batch ) { setBatch( response.data.batch ); } setBusy( false ); } ); }; const handleDetails = ( failure: Failure ) => { setFailureDetails( failure ); openModal(); }; const openModal = () => setOpen( true ); useEffect( () => { checkId(); }, [] ); useEffect( () => { if ( id ) { setBreadcrumbs( [ ...initialBreadcrumb, { name: id.toString(), hash: `#!/customers/uploads/${ id }`, } ] ); showCustomerUpload(); } }, [ id ] ); useEffect( () => { if ( batch ) { setBreadcrumbs( [ ...initialBreadcrumb, { name: batch.filename, hash: `#!/customers/uploads/${ id }`, } ] ); } }, [ batch ] ); return <>

View upload

{ id && <> { isBusy && ! batch && } { batch && <>
ID Filename Uploaded Completed Status Total records Processed records
{ batch.id } { batch.filename } { new Date( batch.uploaded_at ).toLocaleString() } { batch.finished_at ? new Date( batch.finished_at ).toLocaleString() : <>— } { batch.status } { batch.total_records.toLocaleString() } { batch.processed_records.toLocaleString() }
{ batch.failures?.data.length > 0 && { batch.failures.data.map( ( failure ) => , ) }
Failures
Row Failures Details
{ failure.row } { failure.failures.map( ( msg: string ) => <>{ msg } ) }
} { isOpen && ( { failureDetails &&
Customer batch ID { failureDetails.customer_batch_id }
Failures { failureDetails.failures.join() }
ID { failureDetails.id }
Row { failureDetails.row }
Value { Object.entries( failureDetails.value ).map( ( item, i ) => <>{ item[ 0 ] && } , ) }
{ item[ 0 ] } { item[ 1 ] }
}
) } } { ! isBusy && ! batch && } } ; };