import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { vfAPI } from '../../lib/vfAPI'; import type { Batch, BatchFailure } from '../../types'; import { Button, Flex, Modal } from '@wordpress/components'; import { Breadcrumb } from '../../../../resources/js/Shared/Breadcrumb'; import { Spinner } from '../../../../resources/js/Shared/Spinner'; export const BatchUpload = (props) => { const [batch, setBatch] = useState(); const [breadcrumbs, setBreadcrumbs] = useState(props.breadcrumbs); const [failureDetails, setFailureDetails] = useState(); const [id, setId] = useState(); const [isBusy, setBusy] = useState(false); const [isOpen, setOpen] = useState(false); const checkId = () => { const lastHash = location.hash.split('/').pop(); if (lastHash !== 'new') { setId(Number(lastHash)); } }; const closeModal = () => setOpen(false); const handleClick = () => { show(); }; const show = () => { setBusy(true); const url = `${props.url}/batches/${id}`; vfAPI.get(url).then((response) => { if (response.data.batch) { setBatch(response.data.batch); } setBusy(false); }); }; const handleDetails = (failure: BatchFailure) => { setFailureDetails(failure); openModal(); }; const openModal = () => setOpen(true); useEffect(() => { checkId(); }, []); useEffect(() => { if (id) { setBreadcrumbs([ ...props.breadcrumbs, { label: id.toString(), href: `${props.linkToUploads}/${id}`, }, ]); show(); } }, [id]); useEffect(() => { if (batch) { setBreadcrumbs([ ...props.breadcrumbs, { label: batch.filename, href: `${props.linkToUploads}/${id}`, }, ]); } }, [batch]); return ( <>

View upload

{(batch?.status === 'uploaded' || batch?.status === 'processing') && ( )}
{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 && ( )} )} ); }; BatchUpload.propTypes = { breadcrumbs: PropTypes.array.isRequired, linkToUploads: PropTypes.string.isRequired, url: PropTypes.string.isRequired, };