46 lines
1.6 KiB
JavaScript
46 lines
1.6 KiB
JavaScript
/**
|
|
* @file articleHistoryAutomat.js
|
|
* @description Virtualized grid automat (`articleHistoryGridAutomat`) for
|
|
* infinite-scrolling article history via `react-virtual-aggridautomat`.
|
|
*
|
|
* Upstream Triggers:
|
|
* - `authAutomat`: Subscribes to auth state changes — calls `reload()` when
|
|
* status reaches `'authed'` with a `selectedCompanyId`.
|
|
* - AG Grid: Triggers `fetchRows()` on viewport scroll, sort, or filter changes.
|
|
*/
|
|
|
|
import { createGridAutomat } from 'react-virtual-aggridautomat/createGridAutomat';
|
|
import { article } from '../api';
|
|
import authAutomat from './authAutomat';
|
|
|
|
export const articleHistoryGridAutomat = createGridAutomat({
|
|
name: 'articleHistory',
|
|
blockSize: 50,
|
|
fetchRows: async ({ startRow, endRow, sortModel, filterModel }) => {
|
|
try {
|
|
const res = await article.getHistory({
|
|
startRow,
|
|
endRow,
|
|
sortModel,
|
|
filterModel,
|
|
});
|
|
const rows = res?.rows ?? res?.history ?? res?.entries ?? (Array.isArray(res) ? res : []);
|
|
const total = res?.total ?? res?.totalRows ?? res?.totalCount;
|
|
const lastRow = total != null ? total : (rows.length < (endRow - startRow) ? startRow + rows.length : undefined);
|
|
return { rows, lastRow };
|
|
} catch (err) {
|
|
console.warn('Failed to fetch article history (backend route pending):', err);
|
|
return { rows: [], lastRow: 0 };
|
|
}
|
|
},
|
|
});
|
|
|
|
// Reload on company switch or auth change
|
|
authAutomat.subscribe((auth) => {
|
|
if (auth.status === 'authed' && auth.user?.selectedCompanyId) {
|
|
articleHistoryGridAutomat.actions.reload();
|
|
}
|
|
});
|
|
|
|
export default articleHistoryGridAutomat;
|