diff --git a/src/automats/manifestDraftAutomat.js b/src/automats/manifestDraftAutomat.js index 09c9d76..4308845 100644 --- a/src/automats/manifestDraftAutomat.js +++ b/src/automats/manifestDraftAutomat.js @@ -67,7 +67,7 @@ export function createManifestDraftAutomat({ kind, name }) { /** @param {Array} lines */ setLines(lines) { - const list = Array.isArray(lines) && lines.length > 0 ? lines : [emptyLine()]; + const list = Array.isArray(lines) ? lines : []; automat.setState({ lines: list }); }, @@ -100,11 +100,11 @@ export function createManifestDraftAutomat({ kind, name }) { removeLine(key) { const current = automat.state.lines || []; const next = current.filter((l) => l.key !== key); - automat.setState({ lines: next.length > 0 ? next : [emptyLine()] }); + automat.setState({ lines: next }); }, removeAllLines() { - automat.setState({ lines: [emptyLine()] }); + automat.setState({ lines: [] }); }, /** @param {Array} attachments */ @@ -423,7 +423,7 @@ export function createManifestEditAutomat({ kind, name }) { const partyId = doc.partyId ?? null; const number = doc.number ?? ''; const externalNumber = doc.externalNumber != null ? String(doc.externalNumber) : ''; - const lines = Array.isArray(doc.lines) && doc.lines.length > 0 ? doc.lines : [emptyLine()]; + const lines = Array.isArray(doc.lines) ? doc.lines : []; const attachments = Array.isArray(doc.attachments) ? doc.attachments : []; const baseline = { @@ -508,7 +508,7 @@ export function createManifestEditAutomat({ kind, name }) { const partyId = savedDoc.partyId ?? null; const number = savedDoc.number ?? automat.state.number; const externalNumber = savedDoc.externalNumber != null ? String(savedDoc.externalNumber) : ''; - const lines = Array.isArray(savedDoc.lines) && savedDoc.lines.length > 0 ? savedDoc.lines : [emptyLine()]; + const lines = Array.isArray(savedDoc.lines) ? savedDoc.lines : []; const attachments = Array.isArray(savedDoc.attachments) ? savedDoc.attachments : automat.state.attachments; const baseline = { diff --git a/src/components/manifest/ManifestForm.jsx b/src/components/manifest/ManifestForm.jsx index a908e34..7bfbb4a 100644 --- a/src/components/manifest/ManifestForm.jsx +++ b/src/components/manifest/ManifestForm.jsx @@ -82,7 +82,7 @@ class ManifestForm extends PureComponent { partyId: initialDoc?.partyId ?? null, number: initialDoc?.number || '', externalNumber: initialDoc?.externalNumber || '', - lines: initialDoc?.lines?.length > 0 ? initialDoc.lines : [emptyLine()], + lines: Array.isArray(initialDoc?.lines) ? initialDoc.lines : (props.mode === 'create' ? [emptyLine()] : []), attachments: initialDoc?.attachments || [], shipments: initialEdit?.shipments || [], payments: initialEdit?.payments || [], @@ -197,7 +197,7 @@ class ManifestForm extends PureComponent { date: state.date || '', partyId: state.partyId, externalNumber: state.externalNumber || '', - lines: state.lines?.length > 0 ? state.lines : [emptyLine()], + lines: state.lines || [], attachments: state.attachments || [], })); } @@ -209,7 +209,7 @@ class ManifestForm extends PureComponent { date: state.date || '', partyId: state.partyId, externalNumber: state.externalNumber || '', - lines: state.lines?.length > 0 ? state.lines : [emptyLine()], + lines: state.lines || [], attachments: state.attachments || [], shipments: state.shipments || [], payments: state.payments || [], @@ -371,8 +371,7 @@ class ManifestForm extends PureComponent { requireObject(docRes[cfg.docField], cfg.docField); const doc = docRes[cfg.docField]; const apiLines = requireArray(doc.lines, `${cfg.docField}.lines`); - const lines = - apiLines.length > 0 ? apiLines.map(lineFromApi) : [emptyLine()]; + const lines = apiLines.map(lineFromApi); // Restore attachments if available const apiFiles = doc.files ?? []; @@ -456,7 +455,7 @@ class ManifestForm extends PureComponent { partyId: state.partyId, number: state.number || '', externalNumber: state.externalNumber || '', - lines: state.lines?.length > 0 ? state.lines : [emptyLine()], + lines: state.lines || [], attachments: state.attachments || [], isDirty: false, dirtyFields: state.dirtyFields, @@ -787,7 +786,7 @@ class ManifestForm extends PureComponent { } } const next = prev.lines.filter((line) => line.key !== key); - return { lines: next.length > 0 ? next : [emptyLine()], isDirty: true }; + return { lines: next, isDirty: true }; }); } @@ -823,10 +822,10 @@ class ManifestForm extends PureComponent { return null; } enqueueSnackbar('Nicht ausgelieferte Positionen wurden entfernt.', { variant: 'info' }); - return { lines: remaining.length > 0 ? remaining : [emptyLine()], isDirty: true }; + return { lines: remaining, isDirty: true }; } } - return { lines: [emptyLine()], isDirty: true }; + return { lines: [], isDirty: true }; }); } @@ -1097,7 +1096,7 @@ class ManifestForm extends PureComponent { if (this.state.saving) return false; if (mode === 'edit' && docId == null) return false; - const requireAtLeastOne = mode === 'create'; + const isEdit = mode === 'edit'; let body; try { body = await buildManifestBody( @@ -1112,8 +1111,10 @@ class ManifestForm extends PureComponent { { kind: cfg.kind, partyField: cfg.partyField, - requireAtLeastOne, + requireAtLeastOne: true, externalNumber: cfg.externalNumber, + isUpdate: isEdit, + mode, }, ); } catch (err) { @@ -1151,8 +1152,7 @@ class ManifestForm extends PureComponent { number: doc.number ?? '', externalNumber: doc.externalNumber != null ? String(doc.externalNumber) : '', - lines: - apiLines.length > 0 ? apiLines.map(lineFromApi) : [emptyLine()], + lines: apiLines.map(lineFromApi), attachments: this.state.attachments, }; diff --git a/src/components/manifest/ManifestLines.jsx b/src/components/manifest/ManifestLines.jsx index f4b3d78..acd1086 100644 --- a/src/components/manifest/ManifestLines.jsx +++ b/src/components/manifest/ManifestLines.jsx @@ -114,8 +114,21 @@ class ManifestLines extends PureComponent { } } - return Array.isArray(lines) && - lines.map((line, index) => { + if (!lines || lines.length === 0) { + return ( + + + Keine Positionen vorhanden + + + ); + } + + return lines.map((line, index) => { const lineKey = line.key ?? index; const rawSku = line.sku != null ? String(line.sku).trim() : ''; const isOnShipment = kind === 'order' && Boolean(rawSku && shippedSkus.has(rawSku.toLowerCase())); diff --git a/src/components/manifest/manifestConfig.js b/src/components/manifest/manifestConfig.js index 07b0f70..623ee35 100644 --- a/src/components/manifest/manifestConfig.js +++ b/src/components/manifest/manifestConfig.js @@ -454,12 +454,14 @@ export function collectFilesForUpload(attachments = [], { onlyNew = false } = {} */ export async function buildManifestBody( form, - { partyField, requireAtLeastOne, externalNumber: useExternalNumber, kind }, + { partyField, requireAtLeastOne, externalNumber: useExternalNumber, kind, isUpdate, mode }, ) { const date = form.date; if (!date) throw new Error('Datum fehlt.'); const externalNumber = String(form.externalNumber ?? '').trim(); + const isEdit = isUpdate != null ? Boolean(isUpdate) : (mode === 'edit' || !requireAtLeastOne); + const shouldRequireLine = requireAtLeastOne !== false; let body; if (kind === 'delivery') { @@ -468,7 +470,7 @@ export async function buildManifestBody( body = { externalNumber: externalNumber || null, date, - lines: buildLines(form.lines, { requireAtLeastOne }), + lines: buildLines(form.lines, { requireAtLeastOne: shouldRequireLine }), }; } else { let number = String(form.number ?? '').trim(); @@ -479,7 +481,7 @@ export async function buildManifestBody( body = { number, date, - lines: buildLines(form.lines, { requireAtLeastOne }), + lines: buildLines(form.lines, { requireAtLeastOne: shouldRequireLine }), }; if (useExternalNumber || form.externalNumber != null) { body.externalNumber = externalNumber || null; @@ -490,14 +492,13 @@ export async function buildManifestBody( if (!Number.isNaN(pid)) { body[partyField] = pid; } - } else if (!requireAtLeastOne) { + } else if (isEdit) { body[partyField] = null; } // Add files if attachments exist if (form.attachments && form.attachments.length > 0) { - const isUpdate = !requireAtLeastOne; - const filesToUpload = collectFilesForUpload(form.attachments, { onlyNew: isUpdate }); + const filesToUpload = collectFilesForUpload(form.attachments, { onlyNew: isEdit }); if (filesToUpload.length > 0) { const formData = new FormData(); body.filesMeta = filesToUpload.map((item) => ({