From 3000d7d45c9a560bcc95647655f1534fe30c30a7 Mon Sep 17 00:00:00 2001 From: seb Date: Wed, 5 Aug 2026 07:29:36 +0200 Subject: [PATCH] u --- src/components/CompanySettingsDialog.jsx | 311 +++++++++++------------ 1 file changed, 155 insertions(+), 156 deletions(-) diff --git a/src/components/CompanySettingsDialog.jsx b/src/components/CompanySettingsDialog.jsx index 69c7322..5a443ee 100644 --- a/src/components/CompanySettingsDialog.jsx +++ b/src/components/CompanySettingsDialog.jsx @@ -3,7 +3,6 @@ import { connect } from 'react-redux'; import Alert from '@mui/material/Alert'; import Box from '@mui/material/Box'; import Button from '@mui/material/Button'; -import CircularProgress from '@mui/material/CircularProgress'; import Dialog from '@mui/material/Dialog'; import DialogActions from '@mui/material/DialogActions'; import DialogContent from '@mui/material/DialogContent'; @@ -28,18 +27,15 @@ import { enqueueSnackbar } from 'notistack'; import BackendError from './BackendError'; import SingleLineTextEdit from './SingleLineTextEdit'; import { StorageManagerContext } from '../api/storageManager'; -import { - loadCurrentCompany, - selectCompanyStatus, - selectCurrentCompany, - updateCompany as updateCompanyThunk, -} from '../store/companySlice'; +import { selectAuthUser } from '../store/authSlice'; +import { updateCompany as updateCompanyThunk } from '../store/companySlice'; import { createPosterminal, deletePosterminal, enableAllTerminalCategories, } from '../store/posterminalSlice'; import { loadCategoryTree } from '../store/categorySlice'; +import { requireDefined } from '../utils/apiShape'; import { ISO639_OPTIONS, parseTaxRate } from '../utils/companyForm'; import { backendErrorFrom, isBackendFailure } from '../utils/backendError'; import { notifyError } from '../utils/notify'; @@ -65,34 +61,45 @@ class CompanySettingsDialog extends Component { this.syncFormFromCompany = this.syncFormFromCompany.bind(this); this.handleCreateTerminal = this.handleCreateTerminal.bind(this); this.handleDeleteTerminal = this.handleDeleteTerminal.bind(this); + this.loadCompany = this.loadCompany.bind(this); this.loadTerminals = this.loadTerminals.bind(this); } componentDidUpdate(prevProps) { - const { open, company, companyStatus } = this.props; + const { open } = this.props; if (open && !prevProps.open) { this.setState({ error: null }); - if (company) { - this.syncFormFromCompany(company); - } else if (companyStatus !== 'loading') { - void this.props.loadCurrentCompany(); - } + void this.loadCompany(); void this.loadTerminals(); } - if (open && company && company !== prevProps.company) { - this.syncFormFromCompany(company); - } } syncFormFromCompany(company) { this.setState({ - name: company.name ?? '', + name: company.name, iso639: company.iso639, taxrate: company.taxrate != null ? String(company.taxrate) : '', reducedtaxrate: company.reducedtaxrate != null ? String(company.reducedtaxrate) : '', }); } + async loadCompany() { + try { + const companyId = requireDefined( + this.props.selectedCompanyId, + 'selectedCompanyId', + ); + const companies = await this.context.companyPromise; + const company = requireDefined( + companies.find((c) => c.id === companyId), + 'company', + ); + this.syncFormFromCompany(company); + } catch (err) { + notifyError(err); + } + } + async loadTerminals() { try { const terminals = await this.context.posterminalPromise; @@ -156,13 +163,14 @@ class CompanySettingsDialog extends Component { this.setState({ busy: true, error: null }); try { await this.props - .updateCompany({ + .saveCompany({ name: name.trim(), iso639, taxrate: parsedTaxrate, reducedtaxrate: parsedReducedtaxrate, }) .unwrap(); + await this.context.updateCompany(); enqueueSnackbar('Unternehmen gespeichert.', { variant: 'success' }); onClose(); } catch (err) { @@ -173,9 +181,8 @@ class CompanySettingsDialog extends Component { } render() { - const { open, onClose, companyStatus } = this.props; + const { open, onClose } = this.props; const { busy, error, name, iso639, taxrate, reducedtaxrate } = this.state; - const loading = companyStatus === 'loading' && !busy; const parsedTaxrate = parseTaxRate(taxrate); const parsedReducedtaxrate = parseTaxRate(reducedtaxrate); const canSave = @@ -199,148 +206,142 @@ class CompanySettingsDialog extends Component { ) ) : null} - {loading ? ( - - - - ) : ( - - + this.setState({ name: nextName })} + autoComplete="off" + autoFocus + /> + + + Sprache + this.setState({ name: nextName })} - autoComplete="off" - autoFocus + onChange={(_event, value) => { + if (value) this.setState({ iso639: value }); + }} + > + Deutsch + English + + + + + this.setState({ taxrate: e.target.value })} + error={taxrate !== '' && parsedTaxrate == null} + slotProps={{ + input: { + endAdornment: %, + }, + htmlInput: { min: 0, max: 100, step: 0.01, required: true }, + }} + /> + this.setState({ reducedtaxrate: e.target.value })} + error={reducedtaxrate !== '' && parsedReducedtaxrate == null} + slotProps={{ + input: { + endAdornment: %, + }, + htmlInput: { min: 0, max: 100, step: 0.01, required: true }, + }} /> - - - Sprache - { - if (value) this.setState({ iso639: value }); - }} - > - Deutsch - English - - - - - this.setState({ taxrate: e.target.value })} - error={taxrate !== '' && parsedTaxrate == null} - slotProps={{ - input: { - endAdornment: %, - }, - htmlInput: { min: 0, max: 100, step: 0.01, required: true }, - }} - /> - this.setState({ reducedtaxrate: e.target.value })} - error={reducedtaxrate !== '' && parsedReducedtaxrate == null} - slotProps={{ - input: { - endAdornment: %, - }, - htmlInput: { min: 0, max: 100, step: 0.01, required: true }, - }} - /> - - - - - - - {this.state.terminals.map((terminal) => ( - - - - - this.handleDeleteTerminal(terminal)} - disabled={busy || this.state.creatingTerminal} - > - - - - - ))} - - - - this.setState({ newTerminalName: e.target.value })} - disabled={busy || this.state.creatingTerminal} - autoComplete="off" - onKeyDown={(e) => { - if (e.key === 'Enter') { - e.preventDefault(); - this.handleCreateTerminal(); - } - }} - sx={{ flex: 1 }} - /> - - - this.setState({ enableAllForNew: e.target.checked })} - disabled={busy || this.state.creatingTerminal} - /> - - - - - )} + + + + + + {this.state.terminals.map((terminal) => ( + + + + + this.handleDeleteTerminal(terminal)} + disabled={busy || this.state.creatingTerminal} + > + + + + + ))} + + + + this.setState({ newTerminalName: e.target.value })} + disabled={busy || this.state.creatingTerminal} + autoComplete="off" + onKeyDown={(e) => { + if (e.key === 'Enter') { + e.preventDefault(); + this.handleCreateTerminal(); + } + }} + sx={{ flex: 1 }} + /> + + + this.setState({ enableAllForNew: e.target.checked })} + disabled={busy || this.state.creatingTerminal} + /> + + + + + - @@ -351,13 +352,11 @@ class CompanySettingsDialog extends Component { } const mapStateToProps = (state) => ({ - company: selectCurrentCompany(state), - companyStatus: selectCompanyStatus(state), + selectedCompanyId: selectAuthUser(state)?.selectedCompanyId, }); const mapDispatchToProps = (dispatch) => ({ - loadCurrentCompany: () => dispatch(loadCurrentCompany()), - updateCompany: (payload) => dispatch(updateCompanyThunk(payload)), + saveCompany: (payload) => dispatch(updateCompanyThunk(payload)), createPosterminal: (payload) => dispatch(createPosterminal(payload)), deletePosterminal: (payload) => dispatch(deletePosterminal(payload)), enableAllTerminalCategories: (payload) => dispatch(enableAllTerminalCategories(payload)),