u
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Component } from 'react';
|
||||
import { PureComponent } from 'react';
|
||||
import Alert from '@mui/material/Alert';
|
||||
import Box from '@mui/material/Box';
|
||||
import Button from '@mui/material/Button';
|
||||
@@ -36,8 +36,8 @@ import RequestQuoteIcon from '@mui/icons-material/RequestQuote';
|
||||
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
|
||||
import AssignmentReturnIcon from '@mui/icons-material/AssignmentReturn';
|
||||
import BackendError from './BackendError';
|
||||
import { company } from '../api';
|
||||
import authAutomat, { bootstrapAuthMe } from '../automats/authAutomat';
|
||||
import authAutomat from '../automats/authAutomat';
|
||||
import companyAutomat from '../automats/companyAutomat';
|
||||
import {
|
||||
defaultIso639,
|
||||
ISO639_OPTIONS,
|
||||
@@ -126,12 +126,18 @@ function formatNumberPreview(prefix, padding, next, offset = 0) {
|
||||
return `${prefix ?? ''}${numStr}`;
|
||||
}
|
||||
|
||||
class CompanySetupDialog extends Component {
|
||||
function mapCompanyState(state) {
|
||||
return {
|
||||
companies: Array.isArray(state?.companies) ? state.companies : [],
|
||||
loading: state?.status === 'idle' || state?.status === 'loading',
|
||||
};
|
||||
}
|
||||
|
||||
class CompanySetupDialog extends PureComponent {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
companies: [],
|
||||
loading: true,
|
||||
...mapCompanyState(companyAutomat.state),
|
||||
busy: false,
|
||||
error: null,
|
||||
name: '',
|
||||
@@ -150,26 +156,21 @@ class CompanySetupDialog extends Component {
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
void this.loadCompanies();
|
||||
this.unsubscribe = companyAutomat.subscribe(this, mapCompanyState);
|
||||
this.setState(mapCompanyState(companyAutomat.state));
|
||||
if (companyAutomat.state.status !== 'succeeded') {
|
||||
void companyAutomat.actions.load().catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
async loadCompanies() {
|
||||
this.setState({ loading: true });
|
||||
try {
|
||||
const data = await company.list();
|
||||
this.setState({ companies: data.companies ?? [] });
|
||||
} catch {
|
||||
this.setState({ companies: [] });
|
||||
} finally {
|
||||
this.setState({ loading: false });
|
||||
}
|
||||
componentWillUnmount() {
|
||||
this.unsubscribe?.();
|
||||
}
|
||||
|
||||
async handleSelect(companyId) {
|
||||
this.setState({ busy: true, error: null });
|
||||
try {
|
||||
await authAutomat.actions.selectCompany(companyId);
|
||||
await bootstrapAuthMe();
|
||||
} catch (err) {
|
||||
this.setState({ error: backendErrorFrom(err) });
|
||||
} finally {
|
||||
@@ -224,8 +225,6 @@ class CompanySetupDialog extends Component {
|
||||
reducedtaxrate: parsedReducedtaxrate,
|
||||
numbers,
|
||||
});
|
||||
|
||||
await bootstrapAuthMe();
|
||||
} catch (err) {
|
||||
this.setState({ error: backendErrorFrom(err) });
|
||||
} finally {
|
||||
@@ -236,7 +235,6 @@ class CompanySetupDialog extends Component {
|
||||
async handleLogout() {
|
||||
try {
|
||||
await authAutomat.actions.logout();
|
||||
await bootstrapAuthMe();
|
||||
} catch (err) {
|
||||
this.setState({ error: backendErrorFrom(err) });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user