u
This commit is contained in:
@@ -31,16 +31,17 @@ import { notifyError } from '../../utils/notify';
|
||||
import { formatDate } from './manifestConfig';
|
||||
import orderAutomat from '../../automats/orderAutomat';
|
||||
import shipmentAutomat from '../../automats/shipmentAutomat';
|
||||
import carrierAutomat from '../../automats/carrierAutomat';
|
||||
import { SURFACE_RAISED, WHITE } from '../../colors';
|
||||
|
||||
const CARRIERS = ['DHL', 'UPS', 'DPD', 'GLS', 'Hermes', 'Spedition', 'Selbstabholung'];
|
||||
|
||||
// MARK: <AddPackageDialog/>
|
||||
class AddPackageDialog extends PureComponent {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
const carriers = carrierAutomat.state.carriers || [];
|
||||
this.state = {
|
||||
carrier: 'DHL',
|
||||
carriers,
|
||||
carrierId: carriers[0]?.id ?? '',
|
||||
trackingCode: '',
|
||||
type: 'parcel',
|
||||
state: 'draft',
|
||||
@@ -49,15 +50,34 @@ class AddPackageDialog extends PureComponent {
|
||||
this.handleSubmit = this.handleSubmit.bind(this);
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.unsub = carrierAutomat.subscribe(this, (state) => {
|
||||
const carriers = state.carriers || [];
|
||||
const currentId = this.state.carrierId;
|
||||
const valid = carriers.some((c) => c.id === currentId);
|
||||
return {
|
||||
carriers,
|
||||
carrierId: valid ? currentId : (carriers[0]?.id ?? ''),
|
||||
};
|
||||
});
|
||||
if (carrierAutomat.state.status !== 'ready') {
|
||||
void carrierAutomat.actions.load().catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.unsub?.();
|
||||
}
|
||||
|
||||
async handleSubmit() {
|
||||
const { shipment, onClose, onSuccess } = this.props;
|
||||
const { carrier, trackingCode, type, state } = this.state;
|
||||
if (!shipment?.id) return;
|
||||
const { carrierId, trackingCode, type, state } = this.state;
|
||||
if (!shipment?.id || !carrierId) return;
|
||||
|
||||
this.setState({ saving: true });
|
||||
try {
|
||||
await shippingApi.create(shipment.id, {
|
||||
carrier: carrier.trim() || undefined,
|
||||
carrierId: Number(carrierId),
|
||||
trackingCode: trackingCode.trim() || undefined,
|
||||
type,
|
||||
state,
|
||||
@@ -77,7 +97,8 @@ class AddPackageDialog extends PureComponent {
|
||||
|
||||
render() {
|
||||
const { open, onClose, shipment } = this.props;
|
||||
const { carrier, trackingCode, type, state, saving } = this.state;
|
||||
const { carriers, carrierId, trackingCode, type, state, saving } = this.state;
|
||||
const canSubmit = Boolean(carrierId) && !saving;
|
||||
|
||||
return (
|
||||
<AppDialog open={open} onClose={onClose} maxWidth="xs" fullWidth>
|
||||
@@ -97,13 +118,21 @@ class AddPackageDialog extends PureComponent {
|
||||
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2, mb: 3 }}>
|
||||
<TextField
|
||||
select
|
||||
label="Versanddienstleister (Carrier)"
|
||||
label="Lieferart (Carrier)"
|
||||
size="small"
|
||||
value={carrier}
|
||||
onChange={(e) => this.setState({ carrier: e.target.value })}
|
||||
value={carrierId}
|
||||
onChange={(e) => this.setState({ carrierId: e.target.value })}
|
||||
error={carriers.length === 0}
|
||||
helperText={
|
||||
carriers.length === 0
|
||||
? 'Keine Lieferarten eingerichtet (unter Auftrag → Lieferarten anlegen)'
|
||||
: undefined
|
||||
}
|
||||
>
|
||||
{CARRIERS.map((c) => (
|
||||
<MenuItem key={c} value={c}>{c}</MenuItem>
|
||||
{carriers.map((c) => (
|
||||
<MenuItem key={c.id} value={c.id}>
|
||||
{c.name}
|
||||
</MenuItem>
|
||||
))}
|
||||
</TextField>
|
||||
|
||||
@@ -145,7 +174,7 @@ class AddPackageDialog extends PureComponent {
|
||||
<Button variant="outlined" onClick={onClose} disabled={saving}>
|
||||
Abbrechen
|
||||
</Button>
|
||||
<Button variant="contained" onClick={this.handleSubmit} disabled={saving}>
|
||||
<Button variant="contained" onClick={this.handleSubmit} disabled={!canSubmit}>
|
||||
{saving ? 'Wird gespeichert…' : 'Paket anlegen'}
|
||||
</Button>
|
||||
</Box>
|
||||
|
||||
Reference in New Issue
Block a user