This commit is contained in:
seb
2026-10-15 21:04:37 +02:00
parent beed294cb0
commit fafeec93b6
5 changed files with 538 additions and 348 deletions

View File

@@ -47,6 +47,7 @@ import orderAutomat from './orderAutomat';
* @property {string} filter
* @property {string} stateFilter
* @property {number} seekMonths
* @property {number} seekYears
* @property {'idle'|'loading'|'ready'|'failed'} status
* @property {Error|string|null} error
*/
@@ -58,6 +59,7 @@ const initialState = {
filter: '',
stateFilter: '',
seekMonths: 0,
seekYears: 0,
status: 'idle',
error: null,
};
@@ -69,7 +71,6 @@ let debounceTimer = null;
// #region fetch helper
async function fetchShipments(state) {
const params = {
offset: 0,
limit: 100,
};
if (state.filter && state.filter.trim()) {
@@ -78,8 +79,9 @@ async function fetchShipments(state) {
if (state.stateFilter) {
params.state = state.stateFilter;
}
if (state.seekMonths) {
params.seekMonths = state.seekMonths;
const totalMonths = (state.seekMonths || 0) + (state.seekYears || 0) * 12;
if (totalMonths > 0) {
params.seekMonths = totalMonths;
}
const res = await shipmentApi.list(params);
@@ -164,7 +166,22 @@ const actions = {
},
setSeekMonths(months) {
automat.setState({ seekMonths: months });
automat.setState({ seekMonths: months, seekYears: 0 });
void automat.actions.load().catch(() => {});
},
addSeekMonth() {
automat.setState((s) => ({ seekMonths: (s.seekMonths ?? 0) + 1 }));
void automat.actions.load().catch(() => {});
},
addSeekYear() {
automat.setState((s) => ({ seekYears: (s.seekYears ?? 0) + 1 }));
void automat.actions.load().catch(() => {});
},
resetNegativeSeek() {
automat.setState({ seekMonths: 0, seekYears: 0 });
void automat.actions.load().catch(() => {});
},
// #endregion
@@ -205,16 +222,31 @@ const actions = {
void orderAutomat.actions.reload().catch(() => {});
},
reset() {
if (debounceTimer) clearTimeout(debounceTimer);
inflightLoad = null;
automat.setState({ ...initialState });
},
// #endregion
};
reset() {
if (debounceTimer) clearTimeout(debounceTimer);
inflightLoad = null;
automat.setState({ ...initialState });
},
// #endregion
};
automat = new Automat(initialState, actions, { name: 'shipment' });
// MARK: filter key & items helpers
export function getFilterKey(state = automat?.state || initialState) {
return `${state.filter || ''}|${state.stateFilter || ''}|${state.seekMonths || 0}|${state.seekYears || 0}`;
}
export function getItems() {
if (automat.state.status === 'idle') {
void automat.actions.load().catch(() => {});
}
return automat.state.items;
}
automat.getFilterKey = getFilterKey;
automat.getItems = getItems;
// #region authAutomat synchronization
let lastCompanyId = null;
authAutomat.subscribe((auth) => {