u
This commit is contained in:
@@ -30,7 +30,10 @@ import {
|
||||
parseNum,
|
||||
} from './manifestConfig';
|
||||
import { ensureDocumentNumber, getNextDocumentNumber } from '../../utils/documentNumber';
|
||||
import { getManifestDraftAutomat } from '../../automats/manifestDraftAutomat';
|
||||
import {
|
||||
getManifestDraftAutomat,
|
||||
getManifestEditAutomat,
|
||||
} from '../../automats/manifestDraftAutomat';
|
||||
|
||||
class ManifestForm extends PureComponent {
|
||||
constructor(props) {
|
||||
@@ -44,19 +47,29 @@ class ManifestForm extends PureComponent {
|
||||
: entityAutomat.state.status === 'ready';
|
||||
|
||||
this.draftAutomat = props.mode === 'create' ? getManifestDraftAutomat(props.kind) : null;
|
||||
this.editAutomat = props.mode === 'edit' ? getManifestEditAutomat(props.kind) : null;
|
||||
|
||||
const initialDraft = this.draftAutomat?.state;
|
||||
const initialEdit = (this.editAutomat && String(this.editAutomat.state?.id) === String(props.docId))
|
||||
? this.editAutomat.state
|
||||
: null;
|
||||
const initialDoc = initialDraft || initialEdit;
|
||||
|
||||
this.state = {
|
||||
status: props.mode === 'edit' ? 'loading' : (partiesReady ? 'ready' : 'loadingParties'),
|
||||
status: props.mode === 'edit' && !initialEdit ? 'loading' : (partiesReady ? 'ready' : 'loadingParties'),
|
||||
error: null,
|
||||
parties: initialParties,
|
||||
partiesLoading: !partiesReady,
|
||||
date: initialDraft?.date || '',
|
||||
partyId: initialDraft?.partyId ?? null,
|
||||
number: initialDraft?.number || '',
|
||||
externalNumber: initialDraft?.externalNumber || '',
|
||||
lines: initialDraft?.lines?.length > 0 ? initialDraft.lines : [emptyLine()],
|
||||
attachments: initialDraft?.attachments || [],
|
||||
date: initialDoc?.date || '',
|
||||
partyId: initialDoc?.partyId ?? null,
|
||||
number: initialDoc?.number || '',
|
||||
externalNumber: initialDoc?.externalNumber || '',
|
||||
lines: initialDoc?.lines?.length > 0 ? initialDoc.lines : [emptyLine()],
|
||||
attachments: initialDoc?.attachments || [],
|
||||
isDirty: Boolean(initialEdit?.isDirty),
|
||||
dirtyFields: initialEdit?.dirtyFields || null,
|
||||
dirtyLineKeys: initialEdit?.dirtyLineKeys || [],
|
||||
dirtySummary: initialEdit?.dirtySummary || [],
|
||||
saving: false,
|
||||
pdfUploading: false,
|
||||
pdfDialogOpen: false,
|
||||
@@ -67,6 +80,7 @@ class ManifestForm extends PureComponent {
|
||||
};
|
||||
this._cancelled = false;
|
||||
this.handleRetry = this.handleRetry.bind(this);
|
||||
this.handleRestore = this.handleRestore.bind(this);
|
||||
this.handlePartyChange = this.handlePartyChange.bind(this);
|
||||
this.handleDateChange = this.handleDateChange.bind(this);
|
||||
this.handleNumberChange = this.handleNumberChange.bind(this);
|
||||
@@ -145,6 +159,25 @@ class ManifestForm extends PureComponent {
|
||||
attachments: state.attachments || [],
|
||||
}));
|
||||
}
|
||||
|
||||
if (this.editAutomat) {
|
||||
this.unsubEdit = this.editAutomat.subscribe(this, (state) => {
|
||||
if (String(state.id) === String(this.props.docId)) {
|
||||
return {
|
||||
date: state.date || '',
|
||||
partyId: state.partyId,
|
||||
externalNumber: state.externalNumber || '',
|
||||
lines: state.lines?.length > 0 ? state.lines : [emptyLine()],
|
||||
attachments: state.attachments || [],
|
||||
isDirty: Boolean(state.isDirty),
|
||||
dirtyFields: state.dirtyFields,
|
||||
dirtyLineKeys: state.dirtyLineKeys,
|
||||
dirtySummary: state.dirtySummary,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps, prevState) {
|
||||
@@ -172,11 +205,42 @@ class ManifestForm extends PureComponent {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
this.props.mode === 'edit' &&
|
||||
this.editAutomat &&
|
||||
String(this.editAutomat.state.id) === String(this.props.docId)
|
||||
) {
|
||||
if (
|
||||
prevState.date !== this.state.date ||
|
||||
prevState.partyId !== this.state.partyId ||
|
||||
prevState.externalNumber !== this.state.externalNumber ||
|
||||
prevState.lines !== this.state.lines ||
|
||||
prevState.attachments !== this.state.attachments
|
||||
) {
|
||||
this.editAutomat.actions.update({
|
||||
date: this.state.date,
|
||||
partyId: this.state.partyId,
|
||||
externalNumber: this.state.externalNumber,
|
||||
lines: this.state.lines,
|
||||
attachments: this.state.attachments,
|
||||
});
|
||||
const { isDirty, dirtyFields, dirtyLineKeys, dirtySummary } = this.editAutomat.state;
|
||||
if (
|
||||
this.state.isDirty !== isDirty ||
|
||||
this.state.dirtyFields !== dirtyFields ||
|
||||
this.state.dirtySummary !== dirtySummary
|
||||
) {
|
||||
this.setState({ isDirty, dirtyFields, dirtyLineKeys, dirtySummary });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this._cancelled = true;
|
||||
this.unsubDraft?.();
|
||||
this.unsubEdit?.();
|
||||
this.unsubParties?.();
|
||||
this.unsubCompany?.();
|
||||
if (this._parseAbortController) {
|
||||
@@ -221,6 +285,29 @@ class ManifestForm extends PureComponent {
|
||||
return;
|
||||
}
|
||||
|
||||
// If editAutomat already holds this exact document in memory, preserve in-progress edits
|
||||
if (
|
||||
this.editAutomat &&
|
||||
String(this.editAutomat.state.id) === String(docId) &&
|
||||
this.editAutomat.state.baseline != null
|
||||
) {
|
||||
void this.loadPartiesOnly();
|
||||
this.setState({
|
||||
status: 'ready',
|
||||
isDirty: Boolean(this.editAutomat.state.isDirty),
|
||||
dirtyFields: this.editAutomat.state.dirtyFields,
|
||||
dirtyLineKeys: this.editAutomat.state.dirtyLineKeys,
|
||||
dirtySummary: this.editAutomat.state.dirtySummary,
|
||||
date: this.editAutomat.state.date,
|
||||
partyId: this.editAutomat.state.partyId,
|
||||
number: this.editAutomat.state.number,
|
||||
externalNumber: this.editAutomat.state.externalNumber,
|
||||
lines: this.editAutomat.state.lines,
|
||||
attachments: this.editAutomat.state.attachments,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this.setState({ status: 'loading', error: null, partiesLoading: true });
|
||||
try {
|
||||
const [docRes, parties] = await Promise.all([
|
||||
@@ -240,10 +327,7 @@ class ManifestForm extends PureComponent {
|
||||
const apiFiles = doc.files ?? [];
|
||||
const attachments = await restoreAttachmentsFromApi(apiFiles);
|
||||
|
||||
this.setState({
|
||||
status: 'ready',
|
||||
parties,
|
||||
partiesLoading: false,
|
||||
const loadedDoc = {
|
||||
date: dateInputValue(doc.date),
|
||||
partyId: doc[cfg.partyField],
|
||||
number: doc.number ?? '',
|
||||
@@ -251,6 +335,21 @@ class ManifestForm extends PureComponent {
|
||||
doc.externalNumber != null ? String(doc.externalNumber) : '',
|
||||
lines,
|
||||
attachments,
|
||||
};
|
||||
|
||||
if (this.editAutomat) {
|
||||
this.editAutomat.actions.loadDoc(docId, loadedDoc);
|
||||
}
|
||||
|
||||
this.setState({
|
||||
status: 'ready',
|
||||
parties,
|
||||
partiesLoading: false,
|
||||
...loadedDoc,
|
||||
isDirty: false,
|
||||
dirtyFields: null,
|
||||
dirtyLineKeys: [],
|
||||
dirtySummary: [],
|
||||
});
|
||||
} catch (err) {
|
||||
if (this._cancelled) return;
|
||||
@@ -267,6 +366,25 @@ class ManifestForm extends PureComponent {
|
||||
else void this.loadPartiesOnly();
|
||||
}
|
||||
|
||||
handleRestore() {
|
||||
if (!this.editAutomat) return;
|
||||
this.editAutomat.actions.restoreBaseline();
|
||||
const state = this.editAutomat.state;
|
||||
this.setState({
|
||||
date: state.date || '',
|
||||
partyId: state.partyId,
|
||||
number: state.number || '',
|
||||
externalNumber: state.externalNumber || '',
|
||||
lines: state.lines?.length > 0 ? state.lines : [emptyLine()],
|
||||
attachments: state.attachments || [],
|
||||
isDirty: false,
|
||||
dirtyFields: state.dirtyFields,
|
||||
dirtyLineKeys: state.dirtyLineKeys,
|
||||
dirtySummary: state.dirtySummary,
|
||||
});
|
||||
enqueueSnackbar('Änderungen verworfen und ursprünglicher Stand wiederhergestellt.', { variant: 'info' });
|
||||
}
|
||||
|
||||
handlePartyChange(partyId) {
|
||||
const { mode } = this.props;
|
||||
const { requirePartyBeforeLines } = this.cfg;
|
||||
@@ -736,7 +854,7 @@ class ManifestForm extends PureComponent {
|
||||
if (this._cancelled) return;
|
||||
const doc = data[cfg.docField];
|
||||
const apiLines = requireArray(doc.lines, `${cfg.docField}.lines`);
|
||||
this.setState({
|
||||
const savedDoc = {
|
||||
date: dateInputValue(doc.date),
|
||||
partyId: doc[cfg.partyField],
|
||||
number: doc.number ?? '',
|
||||
@@ -744,6 +862,19 @@ class ManifestForm extends PureComponent {
|
||||
doc.externalNumber != null ? String(doc.externalNumber) : '',
|
||||
lines:
|
||||
apiLines.length > 0 ? apiLines.map(lineFromApi) : [emptyLine()],
|
||||
attachments: this.state.attachments,
|
||||
};
|
||||
|
||||
if (this.editAutomat && String(this.editAutomat.state.id) === String(docId)) {
|
||||
this.editAutomat.actions.commitSaved(savedDoc);
|
||||
}
|
||||
|
||||
this.setState({
|
||||
...savedDoc,
|
||||
isDirty: false,
|
||||
dirtyFields: null,
|
||||
dirtyLineKeys: [],
|
||||
dirtySummary: [],
|
||||
saving: false,
|
||||
});
|
||||
enqueueSnackbar(cfg.savedMsg, { variant: 'success' });
|
||||
@@ -756,7 +887,7 @@ class ManifestForm extends PureComponent {
|
||||
|
||||
renderArticles() {
|
||||
const cfg = this.cfg;
|
||||
const { lines, saving } = this.state;
|
||||
const { lines, saving, dirtyFields, dirtyLineKeys } = this.state;
|
||||
|
||||
return (
|
||||
<ManifestLines
|
||||
@@ -766,6 +897,8 @@ class ManifestForm extends PureComponent {
|
||||
saving={saving}
|
||||
wide={4}
|
||||
linesColor={cfg.linesColor}
|
||||
isLinesDirty={Boolean(dirtyFields?.lines)}
|
||||
dirtyLineKeys={dirtyLineKeys || []}
|
||||
onLineChange={this.handleLineChange}
|
||||
onSkuBlur={this.handleSkuBlur}
|
||||
onAddLine={this.handleAddLine}
|
||||
@@ -827,6 +960,10 @@ class ManifestForm extends PureComponent {
|
||||
saving={saving}
|
||||
pdfUploading={pdfUploading}
|
||||
lines={lines}
|
||||
isDirty={this.state.isDirty}
|
||||
dirtyFields={this.state.dirtyFields}
|
||||
dirtySummary={this.state.dirtySummary}
|
||||
onRestore={this.props.mode === 'edit' && this.state.isDirty ? this.handleRestore : undefined}
|
||||
onPdfUpload={kind === 'delivery' ? this.handlePdfUpload : undefined}
|
||||
onSave={this.handleSave}
|
||||
onPartyChange={this.handlePartyChange}
|
||||
|
||||
Reference in New Issue
Block a user