u
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
import { Automat } from 'react-automat';
|
||||
import { order as orderApi } from '../api';
|
||||
import authAutomat from './authAutomat';
|
||||
import entityAutomat from './entityAutomat';
|
||||
|
||||
/**
|
||||
* @typedef {Object} OrderLine
|
||||
@@ -50,11 +51,18 @@ const PREFETCH_CHUNKS = 2;
|
||||
const initialState = {
|
||||
items: null,
|
||||
total: 0,
|
||||
filter: '',
|
||||
onlyUnpaid: false,
|
||||
onlyUndelivered: false,
|
||||
seekMonths: 0,
|
||||
seekYears: 0,
|
||||
negativeSeekMonths: 0,
|
||||
status: 'idle',
|
||||
error: null,
|
||||
};
|
||||
|
||||
let automat;
|
||||
let rawItems = null;
|
||||
const loadedChunks = new Set();
|
||||
const inflightChunks = new Map();
|
||||
|
||||
@@ -147,7 +155,8 @@ export function filterOrders(items, filterOrOpts, nameOf) {
|
||||
}
|
||||
}
|
||||
if (query) {
|
||||
const matches = [o.number, o.externalNumber, nameOf?.(o)]
|
||||
const customer = nameOf ? nameOf(o) : getCustomerName(o);
|
||||
const matches = [o.number, o.externalNumber, customer]
|
||||
.some((v) => String(v ?? '').toLowerCase().includes(query));
|
||||
if (!matches) return false;
|
||||
}
|
||||
@@ -155,6 +164,50 @@ export function filterOrders(items, filterOrOpts, nameOf) {
|
||||
});
|
||||
}
|
||||
|
||||
function getCustomerName(order) {
|
||||
if (!order?.entityId) return '';
|
||||
const entities = entityAutomat.state?.items;
|
||||
if (!Array.isArray(entities)) return '';
|
||||
const entity = entities.find((e) => e.id === order.entityId);
|
||||
if (!entity) return '';
|
||||
return [entity.firstName, entity.name].filter(Boolean).join(' ');
|
||||
}
|
||||
|
||||
function hasActiveFilters(state = automat.state) {
|
||||
return Boolean(
|
||||
state.filter ||
|
||||
state.onlyUnpaid ||
|
||||
state.onlyUndelivered ||
|
||||
state.negativeSeekMonths < 0
|
||||
);
|
||||
}
|
||||
|
||||
function updatePublishedItems() {
|
||||
if (rawItems === null) {
|
||||
automat.setState({ items: null });
|
||||
return;
|
||||
}
|
||||
const state = automat.state;
|
||||
if (!hasActiveFilters(state)) {
|
||||
automat.setState({
|
||||
items: rawItems ? [...rawItems] : null,
|
||||
total: rawItems ? rawItems.length : 0,
|
||||
});
|
||||
return;
|
||||
}
|
||||
const loadedOrders = rawItems.filter(Boolean);
|
||||
const filtered = filterOrders(loadedOrders, {
|
||||
filter: state.filter,
|
||||
onlyUnpaid: state.onlyUnpaid,
|
||||
onlyUndelivered: state.onlyUndelivered,
|
||||
negativeSeekMonths: state.negativeSeekMonths,
|
||||
});
|
||||
automat.setState({
|
||||
items: filtered,
|
||||
total: filtered.length,
|
||||
});
|
||||
}
|
||||
|
||||
const actions = {
|
||||
// MARK: returns current items and triggers chunk 0 if not loaded
|
||||
getItems() {
|
||||
@@ -170,7 +223,7 @@ const actions = {
|
||||
const start = Math.max(0, Number(chunkIndex) || 0);
|
||||
const p = actions.loadChunk(start);
|
||||
for (let c = start + 1; c <= start + PREFETCH_CHUNKS; c++) {
|
||||
const total = automat.state.total;
|
||||
const total = rawItems ? rawItems.length : (automat.state.total || 0);
|
||||
if (total > 0 && c * CHUNK_SIZE >= total) {
|
||||
break;
|
||||
}
|
||||
@@ -191,46 +244,41 @@ const actions = {
|
||||
|
||||
const offset = index * CHUNK_SIZE;
|
||||
const limit = CHUNK_SIZE;
|
||||
if (automat.state.items === null && automat.state.status === 'idle') {
|
||||
if (rawItems === null && automat.state.status === 'idle') {
|
||||
automat.setState({ status: 'loading', error: null });
|
||||
}
|
||||
|
||||
const promise = fetchOrdersPage(offset, limit)
|
||||
.then(({ orders, total }) => {
|
||||
let items = automat.state.items;
|
||||
const currentTotal = total > 0 ? total : (orders.length + offset);
|
||||
|
||||
if (items === null) {
|
||||
// Pre-fill items array with that many empty (null) items
|
||||
items = new Array(currentTotal).fill(null);
|
||||
} else if (items.length !== currentTotal) {
|
||||
if (rawItems === null) {
|
||||
// Pre-fill rawItems array with that many empty (null) items
|
||||
rawItems = new Array(currentTotal).fill(null);
|
||||
} else if (rawItems.length !== currentTotal) {
|
||||
const next = new Array(currentTotal).fill(null);
|
||||
const copyLen = Math.min(items.length, currentTotal);
|
||||
const copyLen = Math.min(rawItems.length, currentTotal);
|
||||
for (let i = 0; i < copyLen; i++) {
|
||||
next[i] = items[i];
|
||||
next[i] = rawItems[i];
|
||||
}
|
||||
items = next;
|
||||
} else {
|
||||
// Shallow copy to notify subscribers and PureComponents
|
||||
items = [...items];
|
||||
rawItems = next;
|
||||
}
|
||||
|
||||
// Fill in real received items
|
||||
for (let i = 0; i < orders.length; i++) {
|
||||
const idx = offset + i;
|
||||
if (idx < items.length) {
|
||||
items[idx] = orders[i];
|
||||
if (idx < rawItems.length) {
|
||||
rawItems[idx] = orders[i];
|
||||
}
|
||||
}
|
||||
|
||||
loadedChunks.add(index);
|
||||
|
||||
automat.setState({
|
||||
items,
|
||||
total: currentTotal,
|
||||
status: 'ready',
|
||||
error: null,
|
||||
});
|
||||
updatePublishedItems();
|
||||
|
||||
// Trigger lookahead prefetching if first chunk just arrived
|
||||
if (index === 0) {
|
||||
@@ -241,11 +289,11 @@ const actions = {
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
return automat.state.items;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`[orderAutomat] loadChunk(${index}) error:`, err);
|
||||
if (automat.state.items === null) {
|
||||
if (rawItems === null) {
|
||||
automat.setState({ status: 'failed', error: err });
|
||||
}
|
||||
throw err;
|
||||
@@ -258,6 +306,98 @@ const actions = {
|
||||
return promise;
|
||||
},
|
||||
|
||||
// MARK: loads all remaining chunks in the background when filtering
|
||||
loadAllRemainingChunks() {
|
||||
if (!rawItems) return;
|
||||
const total = rawItems.length;
|
||||
const totalChunks = Math.ceil(total / CHUNK_SIZE);
|
||||
for (let c = 0; c < totalChunks; c++) {
|
||||
if (!loadedChunks.has(c) && !inflightChunks.has(c)) {
|
||||
void actions.loadChunk(c);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// MARK: filter actions
|
||||
filter(query) {
|
||||
automat.setState({ filter: String(query ?? '') });
|
||||
updatePublishedItems();
|
||||
if (hasActiveFilters()) {
|
||||
actions.loadAllRemainingChunks();
|
||||
}
|
||||
},
|
||||
|
||||
setOnlyUnpaid(onlyUnpaid) {
|
||||
automat.setState({ onlyUnpaid: Boolean(onlyUnpaid) });
|
||||
updatePublishedItems();
|
||||
if (hasActiveFilters()) {
|
||||
actions.loadAllRemainingChunks();
|
||||
}
|
||||
},
|
||||
|
||||
setOnlyUndelivered(onlyUndelivered) {
|
||||
automat.setState({ onlyUndelivered: Boolean(onlyUndelivered) });
|
||||
updatePublishedItems();
|
||||
if (hasActiveFilters()) {
|
||||
actions.loadAllRemainingChunks();
|
||||
}
|
||||
},
|
||||
|
||||
addSeekMonth() {
|
||||
const seekMonths = (automat.state.seekMonths ?? 0) + 1;
|
||||
const seekYears = automat.state.seekYears ?? 0;
|
||||
const totalMonths = -(seekMonths + seekYears * 12);
|
||||
automat.setState({ seekMonths, negativeSeekMonths: totalMonths });
|
||||
updatePublishedItems();
|
||||
if (hasActiveFilters()) {
|
||||
actions.loadAllRemainingChunks();
|
||||
}
|
||||
},
|
||||
|
||||
addSeekYear() {
|
||||
const seekMonths = automat.state.seekMonths ?? 0;
|
||||
const seekYears = (automat.state.seekYears ?? 0) + 1;
|
||||
const totalMonths = -(seekMonths + seekYears * 12);
|
||||
automat.setState({ seekYears, negativeSeekMonths: totalMonths });
|
||||
updatePublishedItems();
|
||||
if (hasActiveFilters()) {
|
||||
actions.loadAllRemainingChunks();
|
||||
}
|
||||
},
|
||||
|
||||
addNegativeSeek(months) {
|
||||
if (months === -12) {
|
||||
actions.addSeekYear();
|
||||
} else if (months === -1) {
|
||||
actions.addSeekMonth();
|
||||
} else {
|
||||
const current = Math.min(0, Number(automat.state.negativeSeekMonths ?? 0));
|
||||
const next = current + months;
|
||||
automat.setState({ negativeSeekMonths: Math.min(0, next) });
|
||||
updatePublishedItems();
|
||||
if (hasActiveFilters()) {
|
||||
actions.loadAllRemainingChunks();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
resetNegativeSeek() {
|
||||
automat.setState({ seekMonths: 0, seekYears: 0, negativeSeekMonths: 0 });
|
||||
updatePublishedItems();
|
||||
},
|
||||
|
||||
resetFilters() {
|
||||
automat.setState({
|
||||
filter: '',
|
||||
onlyUnpaid: false,
|
||||
onlyUndelivered: false,
|
||||
seekMonths: 0,
|
||||
seekYears: 0,
|
||||
negativeSeekMonths: 0,
|
||||
});
|
||||
updatePublishedItems();
|
||||
},
|
||||
|
||||
// MARK: initial load
|
||||
load() {
|
||||
return actions.loadChunk(0);
|
||||
@@ -276,12 +416,13 @@ const actions = {
|
||||
const chunksToReload = Array.from(loadedChunks);
|
||||
loadedChunks.clear();
|
||||
inflightChunks.clear();
|
||||
rawItems = null;
|
||||
if (chunksToReload.length === 0) {
|
||||
return actions.loadChunk(0);
|
||||
}
|
||||
await actions.loadChunk(0);
|
||||
for (const c of chunksToReload) {
|
||||
if (c !== 0 && c * CHUNK_SIZE < automat.state.total) {
|
||||
if (c !== 0 && rawItems && c * CHUNK_SIZE < rawItems.length) {
|
||||
void actions.loadChunk(c);
|
||||
}
|
||||
}
|
||||
@@ -292,37 +433,38 @@ const actions = {
|
||||
replace(items) {
|
||||
loadedChunks.clear();
|
||||
inflightChunks.clear();
|
||||
const count = Array.isArray(items) ? items.length : 0;
|
||||
rawItems = Array.isArray(items) ? [...items] : [];
|
||||
updatePublishedItems();
|
||||
automat.setState({
|
||||
items,
|
||||
total: count,
|
||||
status: 'ready',
|
||||
error: null,
|
||||
});
|
||||
return items;
|
||||
return automat.state.items;
|
||||
},
|
||||
|
||||
// MARK: patches an order locally in the automat items cache
|
||||
patchOrder(id, patch) {
|
||||
if (!Array.isArray(automat.state.items)) return automat.state.items;
|
||||
let changed = false;
|
||||
const items = automat.state.items.map((o) => {
|
||||
if (o && o.id === id) {
|
||||
changed = true;
|
||||
return { ...o, ...patch };
|
||||
}
|
||||
return o;
|
||||
});
|
||||
if (changed) {
|
||||
automat.setState({ items });
|
||||
if (Array.isArray(rawItems)) {
|
||||
rawItems = rawItems.map((o) => {
|
||||
if (o && o.id === id) {
|
||||
changed = true;
|
||||
return { ...o, ...patch };
|
||||
}
|
||||
return o;
|
||||
});
|
||||
}
|
||||
return items;
|
||||
if (changed) {
|
||||
updatePublishedItems();
|
||||
}
|
||||
return automat.state.items;
|
||||
},
|
||||
|
||||
// MARK: resets state
|
||||
reset() {
|
||||
loadedChunks.clear();
|
||||
inflightChunks.clear();
|
||||
rawItems = null;
|
||||
automat.setState(initialState);
|
||||
},
|
||||
};
|
||||
@@ -372,17 +514,27 @@ export function selectOrderTotal(state = automat.state) {
|
||||
|
||||
/** Selects unpaid filter */
|
||||
export function selectOnlyUnpaid(state = automat.state) {
|
||||
return false;
|
||||
return state.onlyUnpaid;
|
||||
}
|
||||
|
||||
/** Selects undelivered filter */
|
||||
export function selectOnlyUndelivered(state = automat.state) {
|
||||
return false;
|
||||
return state.onlyUndelivered;
|
||||
}
|
||||
|
||||
/** Selects negative seek offset in months */
|
||||
export function selectNegativeSeekMonths(state = automat.state) {
|
||||
return 0;
|
||||
return state.negativeSeekMonths;
|
||||
}
|
||||
|
||||
/** Selects seek month count */
|
||||
export function selectSeekMonths(state = automat.state) {
|
||||
return state.seekMonths ?? 0;
|
||||
}
|
||||
|
||||
/** Selects seek year count */
|
||||
export function selectSeekYears(state = automat.state) {
|
||||
return state.seekYears ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user