import { Button } from '@components/common/ui/Button.js'; import { Card, CardContent } from '@components/common/ui/Card.js'; import { Checkbox } from '@components/common/ui/Checkbox.js'; import { Label } from '@components/common/ui/Label.js'; import { Progress } from '@components/common/ui/Progress.js'; import { RadioGroup, RadioGroupItem } from '@components/common/ui/RadioGroup.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React, { useRef } from 'react'; import type { ImportEntity, ImportMode } from '../services/types.js'; import type { FileProblem, UploadChoice, UploadNotice } from '../ui/uploadChoice.js'; import type { JobView } from '../web/jobView.js'; import { ChoiceSelect } from './ChoiceSelect.js'; import { formatBytes, formatDateTime } from './format.js'; import { entityLabel, entityNoun, modeLabel, statusLabel } from './labels.js'; import { Notice, ToneIcon } from './Notice.js'; /** The order of the radio buttons (§ 8.2). */ export const ENTITY_CHOICES: readonly ImportEntity[] = [ 'product', 'category', 'customer', 'customerAddress' ]; const MODE_CHOICES: readonly ImportMode[] = ['upsert', 'create', 'update']; export interface NewImportViewProps { entity: ImportEntity; mode: ImportMode; checkImages: boolean; file: { name: string; size: number } | null; choice: UploadChoice; maxFileBytes: number | null; templateUrl: string; /** The time zone of the store: the times on the page are in it. */ timeZone?: string | null; /** The page of a job, for the link in the notice of an import at work. */ viewUrlOf(job: JobView): string; /** Set while the file is on its way. */ upload: { percent: number } | null; error: string | null; onEntity(entity: ImportEntity): void; onMode(mode: ImportMode): void; onCheckImages(value: boolean): void; onFile(file: File | null): void; onSubmit(): void; onAbort(): void; } const who = (job: JobView) => job.createdBy?.name ?? _('Someone'); /** The text of a notice, and of the question that goes with it when it needs a yes. */ export function noticeText( notice: UploadNotice, timeZone?: string | null ): string { const { job } = notice; const what = entityNoun(job.entity); switch (notice.kind) { case 'busy': return _( 'A ${what} import is running (${percent}%). You can upload a new file when it finishes, or cancel it.', { what, percent: `${notice.percent}` } ); case 'replace': return _( 'There is an unfinished ${what} import (${file}, ${state}, uploaded by ${name} at ${time}). A new upload replaces it.', { what, file: job.fileName, state: statusLabel(job.status).toLocaleLowerCase(), name: who(job), time: formatDateTime(job.createdAt, timeZone) } ); case 'close': return job.status === 'stopped' ? _( 'A ${what} import was stopped after ${rows} rows. Uploading closes it; the rows already written stay.', { what, rows: `${notice.rows}` } ) : _( 'A ${what} import was interrupted after ${rows} rows. Uploading closes it; the rows already written stay.', { what, rows: `${notice.rows}` } ); default: return ''; } } /** What the confirmation dialog says for a notice (D-32). */ export function confirmText( notice: UploadNotice, timeZone?: string | null ): { heading: string; content: string; } { const { job } = notice; if (notice.kind === 'replace') { return { heading: _('Replace the earlier upload?'), content: _( '${name} uploaded ${file} at ${time}. Uploading replaces it. Continue?', { name: who(job), file: job.fileName, time: formatDateTime(job.createdAt, timeZone) } ) }; } return { heading: _('Close the earlier import?'), content: `${noticeText(notice, timeZone)} ${_('Continue?')}` }; } function fileProblemText( problem: FileProblem, maxFileBytes: number | null ): string { return problem === 'empty' ? _('The file is empty.') : _('The file is larger than ${max}.', { max: formatBytes(maxFileBytes ?? 0) }); } // A notice that is about to be overwritten is a warning; one that only informs is not. const NOTICE_TONE = { busy: 'info', replace: 'warning', close: 'warning' } as const; /** * The page "New import" (§ 8.2): what to import, what to do with a record that * exists, the template, the file, and the notices about the type that was * chosen. It holds no state of its own except the file picker's element. */ export function NewImportView(props: NewImportViewProps) { const { entity, choice, upload } = props; const picker = useRef(null); const uploading = upload !== null; return (
{_('What do you want to import?')} props.onEntity(value as ImportEntity)} > {ENTITY_CHOICES.map((value) => (
))}
{choice.notices.map((notice) => ( {noticeText(notice, props.timeZone)} {notice.kind === 'busy' && ( <> {' '} {_('Open it')} )} ))} {choice.showMode && (
({ value, label: modeLabel(entity, value) }))} onChange={(value) => props.onMode(value as ImportMode)} />
)}
{_('Template')} {_('Download the ${what} template (CSV)', { what: entityNoun(entity) })}
{_('File')}
props.onFile(e.target.files?.[0] ?? null)} /> {props.file ? `${props.file.name} - ${formatBytes(props.file.size)}` : _('No file chosen')}
{choice.fileProblem && (

{fileProblemText(choice.fileProblem, props.maxFileBytes)}

)}
{choice.showCheckImages && (
props.onCheckImages(checked)} />
)} {props.error && {props.error}} {upload && (
{upload.percent}%

{upload.percent >= 100 ? _('Finishing the upload...') : _('The page must stay open until the upload ends.')}

)}
{upload ? ( ) : ( )}

{_( 'Tip: import in this order - categories, products, customers, addresses.' )}

); }