u
This commit is contained in:
@@ -68,28 +68,27 @@ export default class ArticleUnitEditorDialog extends Component {
|
||||
super(props);
|
||||
this.state = {
|
||||
busy: false,
|
||||
units: [],
|
||||
unitsLoading: false,
|
||||
units: companyAutomat.state.units,
|
||||
unitsLoading: companyAutomat.state.unitsStatus === 'loading',
|
||||
form: emptyForm(),
|
||||
selectedSi: null,
|
||||
};
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.unsub = companyAutomat.subscribe(this, (state) => ({
|
||||
units: state.units,
|
||||
unitsLoading: state.unitsStatus === 'loading',
|
||||
}));
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.unsub?.();
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
if (this.props.open && !prevProps.open) {
|
||||
this.setState({ form: emptyForm(), selectedSi: null });
|
||||
void this.loadUnits();
|
||||
}
|
||||
}
|
||||
|
||||
async loadUnits() {
|
||||
this.setState({ unitsLoading: true });
|
||||
try {
|
||||
const units = await companyAutomat.actions.loadUnits();
|
||||
this.setState({ units, unitsLoading: false });
|
||||
} catch (err) {
|
||||
this.setState({ unitsLoading: false });
|
||||
notifyError(err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,23 +115,22 @@ export default class ArticleUnitEditorDialog extends Component {
|
||||
if (!formValid(form)) return;
|
||||
this.setState({ busy: true });
|
||||
try {
|
||||
const data = await unitApi.upsert({
|
||||
const payload = {
|
||||
si: String(form.si ?? '').trim(),
|
||||
basesi: String(form.basesi ?? '').trim(),
|
||||
basesifactor: parseFactor(form.basesifactor),
|
||||
symbol: String(form.symbol ?? '').trim(),
|
||||
name: String(form.name ?? '').trim(),
|
||||
});
|
||||
requireObject(data, 'PUT /api/unit');
|
||||
requireObject(data.unit, 'unit');
|
||||
const units = await companyAutomat.actions.loadUnits();
|
||||
};
|
||||
const units = await companyAutomat.actions.upsertUnit(payload);
|
||||
const saved = units.find((u) => u.si === payload.si) ?? payload;
|
||||
this.setState({
|
||||
busy: false,
|
||||
units,
|
||||
form: formFromUnit(data.unit),
|
||||
selectedSi: data.unit.si,
|
||||
form: formFromUnit(saved),
|
||||
selectedSi: saved.si,
|
||||
});
|
||||
this.props.onUnitUpserted?.(data.unit);
|
||||
this.props.onUnitUpserted?.(saved);
|
||||
} catch (err) {
|
||||
this.setState({ busy: false });
|
||||
notifyError(err);
|
||||
@@ -144,9 +142,13 @@ export default class ArticleUnitEditorDialog extends Component {
|
||||
if (!selectedSi || selectedSi === DEFAULT_UNIT_SI) return;
|
||||
this.setState({ busy: true });
|
||||
try {
|
||||
await unitApi.delete(selectedSi);
|
||||
const units = await companyAutomat.actions.loadUnits();
|
||||
this.setState({ busy: false, units, form: emptyForm(), selectedSi: null });
|
||||
await companyAutomat.actions.deleteUnit(selectedSi);
|
||||
this.setState({
|
||||
busy: false,
|
||||
units: companyAutomat.state.units,
|
||||
form: emptyForm(),
|
||||
selectedSi: null,
|
||||
});
|
||||
this.props.onUnitDeleted?.(selectedSi);
|
||||
} catch (err) {
|
||||
this.setState({ busy: false });
|
||||
|
||||
Reference in New Issue
Block a user