u
This commit is contained in:
@@ -25,7 +25,9 @@ import {
|
||||
company as companyApi,
|
||||
number as numberApi,
|
||||
posterminal as posterminalApi,
|
||||
printer as printerApi,
|
||||
printeragent as printeragentApi,
|
||||
agentprinter as agentprinterApi,
|
||||
supplier as supplierApi,
|
||||
unit as unitApi,
|
||||
warehouse as warehouseApi,
|
||||
@@ -178,6 +180,15 @@ const sortPrinterAgents = (agents) => {
|
||||
});
|
||||
};
|
||||
|
||||
const sortAgentPrinters = (printers) => {
|
||||
if (!Array.isArray(printers)) return [];
|
||||
return [...printers].sort((a, b) => {
|
||||
const sDiff = (Number(a.sort) || 0) - (Number(b.sort) || 0);
|
||||
if (sDiff !== 0) return sDiff;
|
||||
return (Number(a.id) || 0) - (Number(b.id) || 0);
|
||||
});
|
||||
};
|
||||
|
||||
const sortAccounts = (items) =>
|
||||
[...items].sort((a, b) => String(a.name ?? '').localeCompare(String(b.name ?? ''), 'de'));
|
||||
|
||||
@@ -247,12 +258,19 @@ const initialState = {
|
||||
terminalsStatus: 'idle',
|
||||
terminalsError: null,
|
||||
|
||||
// Printer agents
|
||||
// Printer agents & agent printers
|
||||
printeragents: [],
|
||||
printeragentsCompanyId: null,
|
||||
printeragentsStatus: 'idle',
|
||||
printeragentsError: null,
|
||||
|
||||
agentprinters: [],
|
||||
agentprintersCompanyId: null,
|
||||
agentprintersStatus: 'idle',
|
||||
agentprintersError: null,
|
||||
|
||||
livePrinters: [], // LivePrinter[] from /api/printer/state or SSE printer/printerstate
|
||||
|
||||
// 4. Suppliers
|
||||
suppliers: [],
|
||||
articlesBySupplier: {},
|
||||
@@ -320,6 +338,8 @@ let inflightCompany = null;
|
||||
let inflightNumbers = null;
|
||||
let inflightTerminals = null;
|
||||
let inflightPrinterAgents = null;
|
||||
let inflightAgentPrinters = null;
|
||||
let inflightLivePrinters = null;
|
||||
let inflightSuppliers = null;
|
||||
let inflightUnits = null;
|
||||
let inflightWarehouses = null;
|
||||
@@ -514,6 +534,8 @@ const actions = {
|
||||
actions.loadNumbers(),
|
||||
actions.loadTerminals(),
|
||||
actions.loadPrinterAgents(),
|
||||
actions.loadAgentPrinters(),
|
||||
actions.loadLivePrinters(),
|
||||
actions.loadSuppliers(),
|
||||
actions.loadUnits(),
|
||||
actions.loadWarehouses(),
|
||||
@@ -531,6 +553,8 @@ const actions = {
|
||||
inflightNumbers = null;
|
||||
inflightTerminals = null;
|
||||
inflightPrinterAgents = null;
|
||||
inflightAgentPrinters = null;
|
||||
inflightLivePrinters = null;
|
||||
inflightSuppliers = null;
|
||||
inflightUnits = null;
|
||||
inflightWarehouses = null;
|
||||
@@ -779,6 +803,102 @@ const actions = {
|
||||
await printeragentApi.delete(agentId);
|
||||
companyAutomat.setState({
|
||||
printeragents: companyAutomat.state.printeragents.filter((a) => a.id !== agentId),
|
||||
agentprinters: companyAutomat.state.agentprinters.filter((p) => p.printeragentId !== agentId),
|
||||
});
|
||||
},
|
||||
// #endregion
|
||||
|
||||
// #region Agent printers (agentprinter) & live telemetry
|
||||
// MARK: calls agentprinter.list from ../api/index.js
|
||||
async loadAgentPrinters(printeragentId) {
|
||||
const companyId =
|
||||
authAutomat.state.user?.selectedCompanyId ?? companyAutomat.state.current?.id;
|
||||
if (!companyId) return [];
|
||||
if (inflightAgentPrinters) return inflightAgentPrinters;
|
||||
companyAutomat.setState({ agentprintersStatus: 'loading', agentprintersError: null });
|
||||
inflightAgentPrinters = (async () => {
|
||||
try {
|
||||
const res = await agentprinterApi.list(printeragentId);
|
||||
const agentprinters = sortAgentPrinters(Array.isArray(res.agentprinters) ? res.agentprinters : []);
|
||||
companyAutomat.setState({
|
||||
agentprinters,
|
||||
agentprintersCompanyId: companyId,
|
||||
agentprintersStatus: 'succeeded',
|
||||
agentprintersError: null,
|
||||
});
|
||||
return agentprinters;
|
||||
} catch (err) {
|
||||
companyAutomat.setState({
|
||||
agentprintersStatus: 'failed',
|
||||
agentprintersError: err?.message ?? String(err),
|
||||
});
|
||||
throw err;
|
||||
} finally {
|
||||
inflightAgentPrinters = null;
|
||||
}
|
||||
})();
|
||||
return inflightAgentPrinters;
|
||||
},
|
||||
|
||||
reloadAgentPrinters(printeragentId) {
|
||||
return actions.loadAgentPrinters(printeragentId);
|
||||
},
|
||||
|
||||
async updateAgentPrinter(id, patch) {
|
||||
const res = await agentprinterApi.update(id, patch);
|
||||
const updated = res.agentprinter;
|
||||
companyAutomat.setState({
|
||||
agentprinters: sortAgentPrinters(
|
||||
companyAutomat.state.agentprinters.map((p) =>
|
||||
p.id === id ? { ...p, ...updated } : p,
|
||||
),
|
||||
),
|
||||
});
|
||||
return updated;
|
||||
},
|
||||
|
||||
async reorderAgentPrinters({ printeragentId, agentprinterIds }) {
|
||||
await agentprinterApi.order({ printeragentId, agentprinterIds });
|
||||
const idOrder = new Map(agentprinterIds.map((id, index) => [id, index]));
|
||||
companyAutomat.setState({
|
||||
agentprinters: sortAgentPrinters(
|
||||
companyAutomat.state.agentprinters.map((p) =>
|
||||
idOrder.has(p.id) ? { ...p, sort: idOrder.get(p.id) } : p,
|
||||
),
|
||||
),
|
||||
});
|
||||
},
|
||||
|
||||
async deleteAgentPrinter(id) {
|
||||
await agentprinterApi.delete(id);
|
||||
companyAutomat.setState({
|
||||
agentprinters: companyAutomat.state.agentprinters.filter((p) => p.id !== id),
|
||||
});
|
||||
},
|
||||
|
||||
async loadLivePrinters() {
|
||||
const companyId =
|
||||
authAutomat.state.user?.selectedCompanyId ?? companyAutomat.state.current?.id;
|
||||
if (!companyId) return [];
|
||||
if (inflightLivePrinters) return inflightLivePrinters;
|
||||
inflightLivePrinters = (async () => {
|
||||
try {
|
||||
const res = await printerApi.state();
|
||||
const livePrinters = Array.isArray(res.printers) ? res.printers : [];
|
||||
companyAutomat.setState({ livePrinters });
|
||||
return livePrinters;
|
||||
} catch (err) {
|
||||
return [];
|
||||
} finally {
|
||||
inflightLivePrinters = null;
|
||||
}
|
||||
})();
|
||||
return inflightLivePrinters;
|
||||
},
|
||||
|
||||
setLivePrinters(printers) {
|
||||
companyAutomat.setState({
|
||||
livePrinters: Array.isArray(printers) ? printers : [],
|
||||
});
|
||||
},
|
||||
// #endregion
|
||||
@@ -1641,6 +1761,27 @@ sseAutomat.actions.on('posterminal', () => {
|
||||
sseAutomat.actions.on('printeragent', () => {
|
||||
void companyAutomat.actions.loadPrinterAgents().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('agentprinter', () => {
|
||||
void companyAutomat.actions.loadAgentPrinters().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('printerstate', (ev) => {
|
||||
try {
|
||||
const payload = typeof ev?.data === 'string' ? JSON.parse(ev.data || '{}') : (ev?.data || {});
|
||||
if (Array.isArray(payload.printers)) {
|
||||
companyAutomat.actions.setLivePrinters(payload.printers);
|
||||
return;
|
||||
}
|
||||
} catch {}
|
||||
void companyAutomat.actions.loadLivePrinters().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('printer', (ev) => {
|
||||
try {
|
||||
const payload = typeof ev?.data === 'string' ? JSON.parse(ev.data || '{}') : (ev?.data || {});
|
||||
if (Array.isArray(payload.printers)) {
|
||||
companyAutomat.actions.setLivePrinters(payload.printers);
|
||||
}
|
||||
} catch {}
|
||||
});
|
||||
sseAutomat.actions.on('customergroup', () => {
|
||||
void companyAutomat.actions.reloadCustomergroups().catch(() => {});
|
||||
});
|
||||
@@ -1686,6 +1827,11 @@ export const selectPrinterAgents = () => companyAutomat.state.printeragents;
|
||||
export const selectPrinterAgentStatus = () => companyAutomat.state.printeragentsStatus;
|
||||
export const selectHasPrinterAgents = () => companyAutomat.state.printeragents.length > 0;
|
||||
|
||||
// Agent printers selectors
|
||||
export const selectAgentPrinters = () => companyAutomat.state.agentprinters;
|
||||
export const selectAgentPrinterStatus = () => companyAutomat.state.agentprintersStatus;
|
||||
export const selectLivePrinters = () => companyAutomat.state.livePrinters;
|
||||
|
||||
// Suppliers selectors
|
||||
export const selectSuppliers = () => companyAutomat.state.suppliers;
|
||||
export const selectSuppliersStatus = () => companyAutomat.state.suppliersStatus;
|
||||
|
||||
Reference in New Issue
Block a user