u
This commit is contained in:
@@ -8,6 +8,7 @@ import brandAutomat from '../automats/brandAutomat';
|
||||
import unitAutomat from '../automats/unitAutomat';
|
||||
import authAutomat from '../automats/authAutomat';
|
||||
import categoryAutomat from '../automats/categoryAutomat';
|
||||
import posterminalAutomat from '../automats/posterminalAutomat';
|
||||
|
||||
|
||||
/**
|
||||
@@ -458,6 +459,22 @@ export class StorageManager extends ApiClient {
|
||||
/** Reload units from API. */
|
||||
updateUnit: { value: () => unitAutomat.actions.load() },
|
||||
|
||||
/** Posterminal list from posterminalAutomat. */
|
||||
posterminals: { get: () => posterminalAutomat.state.terminals },
|
||||
/** Promise that resolves when posterminals are first loaded. */
|
||||
posterminalPromise: {
|
||||
get: () => posterminalAutomat.state.status === 'succeeded'
|
||||
? Promise.resolve(posterminalAutomat.state.terminals)
|
||||
: new Promise((resolve, reject) => {
|
||||
const unsub = posterminalAutomat.subscribe((s) => {
|
||||
if (s.status === 'succeeded') { unsub(); resolve(s.terminals); }
|
||||
else if (s.status === 'failed') { unsub(); reject(new Error(s.error)); }
|
||||
});
|
||||
}),
|
||||
},
|
||||
/** Reload posterminals from API. */
|
||||
updatePosterminal: { value: () => posterminalAutomat.actions.load() },
|
||||
|
||||
/**
|
||||
* Current company object from companyAutomat, or null.
|
||||
* Previously: companies array from GET /api/company.
|
||||
|
||||
@@ -24,7 +24,7 @@ let posterminalAutomat;
|
||||
const actions = {
|
||||
/** Load all posterminals for the selected company. */
|
||||
async load() {
|
||||
const companyId = companyAutomat.state.current?.id;
|
||||
const companyId = authAutomat.state.user?.selectedCompanyId ?? companyAutomat.state.current?.id;
|
||||
if (!companyId) return [];
|
||||
posterminalAutomat.setState({ status: 'loading', error: null });
|
||||
try {
|
||||
|
||||
@@ -56,6 +56,14 @@ export default class CompanySettingsDialog extends Component {
|
||||
this.loadTerminals = this.loadTerminals.bind(this);
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
if (this.props.open) {
|
||||
this.setState({ error: null });
|
||||
void this.loadCompany();
|
||||
void this.loadTerminals();
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
const { open } = this.props;
|
||||
if (open && !prevProps.open) {
|
||||
@@ -78,9 +86,9 @@ export default class CompanySettingsDialog extends Component {
|
||||
try {
|
||||
const session = await this.context.authMePromise;
|
||||
const companyId = requireDefined(session.user?.selectedCompanyId, 'selectedCompanyId');
|
||||
const companies = await this.context.companyPromise;
|
||||
const companyData = await this.context.companyPromise;
|
||||
const companyRow = requireDefined(
|
||||
companies.find((c) => c.id === companyId),
|
||||
Array.isArray(companyData) ? companyData.find((c) => c.id === companyId) : companyData,
|
||||
'company',
|
||||
);
|
||||
this.syncFormFromCompany(companyRow);
|
||||
@@ -92,7 +100,7 @@ export default class CompanySettingsDialog extends Component {
|
||||
async loadTerminals() {
|
||||
try {
|
||||
const terminals = await this.context.posterminalPromise;
|
||||
this.setState({ terminals });
|
||||
this.setState({ terminals: Array.isArray(terminals) ? terminals : [] });
|
||||
} catch (err) {
|
||||
notifyError(err);
|
||||
}
|
||||
@@ -115,7 +123,7 @@ export default class CompanySettingsDialog extends Component {
|
||||
}
|
||||
|
||||
const terminals = await this.context.updatePosterminal();
|
||||
this.setState({ newTerminalName: '', terminals });
|
||||
this.setState({ newTerminalName: '', terminals: Array.isArray(terminals) ? terminals : [] });
|
||||
} catch (err) {
|
||||
notifyError(err);
|
||||
} finally {
|
||||
@@ -127,7 +135,7 @@ export default class CompanySettingsDialog extends Component {
|
||||
try {
|
||||
await this.context.posterminal.delete(terminal.id);
|
||||
const terminals = await this.context.updatePosterminal();
|
||||
this.setState({ terminals });
|
||||
this.setState({ terminals: Array.isArray(terminals) ? terminals : [] });
|
||||
} catch (err) {
|
||||
notifyError(err);
|
||||
}
|
||||
@@ -266,7 +274,7 @@ export default class CompanySettingsDialog extends Component {
|
||||
|
||||
<Box>
|
||||
<List dense sx={{ py: 0 }}>
|
||||
{this.state.terminals.map((terminal) => (
|
||||
{(this.state.terminals || []).map((terminal) => (
|
||||
<ListItem
|
||||
key={terminal.id}
|
||||
sx={{ px: 0, py: 0.5 }}
|
||||
|
||||
Reference in New Issue
Block a user