import { PureComponent } from 'react'; import Box from '@mui/material/Box'; import Chip from '@mui/material/Chip'; import IconButton from '@mui/material/IconButton'; import ListItem from '@mui/material/ListItem'; import DeleteOutlinedIcon from '@mui/icons-material/DeleteOutlined'; import DragIndicatorIcon from '@mui/icons-material/DragIndicator'; import SingleLineTextEdit from 'dashgui/SingleLineTextEdit'; import { enqueueSnackbar } from 'notistack'; import companyAutomat from '../../automats/companyAutomat'; import { notifyError } from '../../utils/notify'; class ArtikelMerkmalWert extends PureComponent { constructor(props) { super(props); const item = props.item ?? props.value; this.state = { draft: item?.value ?? '', busy: false, }; } componentDidUpdate(prevProps) { const prevItem = prevProps.item ?? prevProps.value; const item = this.props.item ?? this.props.value; if (prevItem?.value !== item?.value) { this.setState({ draft: item?.value ?? '' }); } else if (prevProps.disabled && !this.props.disabled) { if (this.state.draft !== item?.value) { this.setState({ draft: item?.value ?? '' }); } } } handleChange = (next) => { this.setState({ draft: next }); const item = this.props.item ?? this.props.value; this.props.onChange?.(next, item); }; handleBlur = () => { const item = this.props.item ?? this.props.value; if (!item) return; const draft = this.state.draft.trim(); if (!draft) { this.setState({ draft: item.value ?? '' }); return; } if (draft !== item.value) { void this.renameValue(item, draft); } }; async renameValue(item, val) { this.setState({ busy: true }); try { await companyAutomat.actions.updateValue(item.id, { value: val }); enqueueSnackbar('Wert gespeichert.', { variant: 'success' }); } catch (err) { this.setState({ draft: item.value ?? '' }); notifyError(err); } finally { this.setState({ busy: false }); } } handleKeyDown = (event) => { const item = this.props.item ?? this.props.value; if (event.key === 'Enter') { event.preventDefault(); event.target.blur(); } if (event.key === 'Escape') { event.preventDefault(); this.setState({ draft: item?.value ?? '' }); event.target.blur(); } }; handleDelete = () => { const item = this.props.item ?? this.props.value; if (!item || this.props.disabled || this.state.busy) return; if (!window.confirm(`Wert „${item.value}“ wirklich löschen?`)) { return; } this.setState({ busy: true }); void (async () => { try { await companyAutomat.actions.deleteValue(item.id); enqueueSnackbar('Wert gelöscht.', { variant: 'success' }); } catch (err) { notifyError(err); this.setState({ busy: false }); } })(); }; render() { const { disabled, sx } = this.props; const item = this.props.item ?? this.props.value; const { draft, busy } = this.state; const valText = item?.value ?? ''; const isDisabled = disabled || busy; const articleCount = item?.articleCount ?? 0; return ( 0 ? 'text.primary' : 'text.disabled', flexShrink: 0, '& .MuiChip-label': { px: 0.5, textAlign: 'center', width: '100%', }, }} /> ); } } export default ArtikelMerkmalWert;