u
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { Component } from 'react';
|
||||
import { connect } from 'react-redux';
|
||||
import Alert from '@mui/material/Alert';
|
||||
import Box from '@mui/material/Box';
|
||||
import Button from '@mui/material/Button';
|
||||
@@ -27,20 +26,12 @@ import { enqueueSnackbar } from 'notistack';
|
||||
import BackendError from './BackendError';
|
||||
import SingleLineTextEdit from './SingleLineTextEdit';
|
||||
import { StorageManagerContext } from '../api/storageManager';
|
||||
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 { requireDefined, requireObject } from '../utils/apiShape';
|
||||
import { ISO639_OPTIONS, parseTaxRate } from '../utils/companyForm';
|
||||
import { backendErrorFrom, isBackendFailure } from '../utils/backendError';
|
||||
import { notifyError } from '../utils/notify';
|
||||
|
||||
class CompanySettingsDialog extends Component {
|
||||
export default class CompanySettingsDialog extends Component {
|
||||
static contextType = StorageManagerContext;
|
||||
|
||||
constructor(props) {
|
||||
@@ -74,27 +65,25 @@ class CompanySettingsDialog extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
syncFormFromCompany(company) {
|
||||
syncFormFromCompany(companyRow) {
|
||||
this.setState({
|
||||
name: company.name,
|
||||
iso639: company.iso639,
|
||||
taxrate: company.taxrate != null ? String(company.taxrate) : '',
|
||||
reducedtaxrate: company.reducedtaxrate != null ? String(company.reducedtaxrate) : '',
|
||||
name: companyRow.name,
|
||||
iso639: companyRow.iso639,
|
||||
taxrate: companyRow.taxrate != null ? String(companyRow.taxrate) : '',
|
||||
reducedtaxrate: companyRow.reducedtaxrate != null ? String(companyRow.reducedtaxrate) : '',
|
||||
});
|
||||
}
|
||||
|
||||
async loadCompany() {
|
||||
try {
|
||||
const companyId = requireDefined(
|
||||
this.props.selectedCompanyId,
|
||||
'selectedCompanyId',
|
||||
);
|
||||
const session = await this.context.authMePromise;
|
||||
const companyId = requireDefined(session.user?.selectedCompanyId, 'selectedCompanyId');
|
||||
const companies = await this.context.companyPromise;
|
||||
const company = requireDefined(
|
||||
const companyRow = requireDefined(
|
||||
companies.find((c) => c.id === companyId),
|
||||
'company',
|
||||
);
|
||||
this.syncFormFromCompany(company);
|
||||
this.syncFormFromCompany(companyRow);
|
||||
} catch (err) {
|
||||
notifyError(err);
|
||||
}
|
||||
@@ -115,13 +104,14 @@ class CompanySettingsDialog extends Component {
|
||||
|
||||
this.setState({ creatingTerminal: true });
|
||||
try {
|
||||
const terminal = await this.props.createPosterminal({ name: trimmed }).unwrap();
|
||||
const data = await this.context.posterminal.upsert({ name: trimmed });
|
||||
requireObject(data, 'PUT /api/posterminal');
|
||||
requireObject(data.posterminal, 'posterminal');
|
||||
requireDefined(data.posterminal.id, 'posterminal.id');
|
||||
|
||||
// If "enable all" toggle is on, enable all categories for this terminal
|
||||
if (enableAllForNew && terminal.id) {
|
||||
await this.props.enableAllTerminalCategories({ terminalId: terminal.id }).unwrap();
|
||||
// Reload category tree so tiles reflect the updated posterminalIds
|
||||
this.props.loadCategoryTree();
|
||||
if (enableAllForNew) {
|
||||
await this.context.posterminal.setOnAll(data.posterminal.id);
|
||||
await this.context.updateCategoryTree();
|
||||
}
|
||||
|
||||
const terminals = await this.context.updatePosterminal();
|
||||
@@ -135,7 +125,7 @@ class CompanySettingsDialog extends Component {
|
||||
|
||||
async handleDeleteTerminal(terminal) {
|
||||
try {
|
||||
await this.props.deletePosterminal({ terminalId: terminal.id }).unwrap();
|
||||
await this.context.posterminal.delete(terminal.id);
|
||||
const terminals = await this.context.updatePosterminal();
|
||||
this.setState({ terminals });
|
||||
} catch (err) {
|
||||
@@ -162,14 +152,14 @@ class CompanySettingsDialog extends Component {
|
||||
|
||||
this.setState({ busy: true, error: null });
|
||||
try {
|
||||
await this.props
|
||||
.saveCompany({
|
||||
name: name.trim(),
|
||||
iso639,
|
||||
taxrate: parsedTaxrate,
|
||||
reducedtaxrate: parsedReducedtaxrate,
|
||||
})
|
||||
.unwrap();
|
||||
const session = await this.context.authMePromise;
|
||||
const companyId = requireDefined(session.user?.selectedCompanyId, 'selectedCompanyId');
|
||||
await this.context.company.update(companyId, {
|
||||
name: name.trim(),
|
||||
iso639,
|
||||
taxrate: parsedTaxrate,
|
||||
reducedtaxrate: parsedReducedtaxrate,
|
||||
});
|
||||
await this.context.updateCompany();
|
||||
enqueueSnackbar('Unternehmen gespeichert.', { variant: 'success' });
|
||||
onClose();
|
||||
@@ -350,17 +340,3 @@ class CompanySettingsDialog extends Component {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const mapStateToProps = (state) => ({
|
||||
selectedCompanyId: selectAuthUser(state)?.selectedCompanyId,
|
||||
});
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
saveCompany: (payload) => dispatch(updateCompanyThunk(payload)),
|
||||
createPosterminal: (payload) => dispatch(createPosterminal(payload)),
|
||||
deletePosterminal: (payload) => dispatch(deletePosterminal(payload)),
|
||||
enableAllTerminalCategories: (payload) => dispatch(enableAllTerminalCategories(payload)),
|
||||
loadCategoryTree: () => dispatch(loadCategoryTree()),
|
||||
});
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(CompanySettingsDialog);
|
||||
|
||||
Reference in New Issue
Block a user