-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #809 from interval/remote-pagination-v2
Progressive table data fetching
- Loading branch information
Showing
9 changed files
with
576 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { z } from 'zod' | ||
import Logger from '../classes/Logger' | ||
import { tableRow, T_IO_PROPS, menuItem, T_IO_STATE } from '../ioSchema' | ||
import { TableColumn } from '../types' | ||
import { | ||
columnsBuilder, | ||
tableRowSerializer, | ||
filterRows, | ||
sortRows, | ||
TABLE_DATA_BUFFER_SIZE, | ||
missingColumnMessage, | ||
} from '../utils/table' | ||
|
||
type PublicProps<Row> = Omit< | ||
T_IO_PROPS<'DISPLAY_TABLE'>, | ||
'data' | 'columns' | 'totalRecords' | ||
> & { | ||
data: Row[] | ||
columns?: (TableColumn<Row> | string)[] | ||
rowMenuItems?: (row: Row) => z.infer<typeof menuItem>[] | ||
} | ||
|
||
export default function displayTable(logger: Logger) { | ||
return function displayTable<Row extends z.input<typeof tableRow> = any>( | ||
props: PublicProps<Row> | ||
) { | ||
const columns = columnsBuilder(props, column => | ||
logger.error(missingColumnMessage('io.display.table')(column)) | ||
) | ||
|
||
// Rendering all rows on initialization is necessary for filtering and sorting | ||
const data = props.data.map((row, index) => | ||
tableRowSerializer({ | ||
index, | ||
row, | ||
columns, | ||
menuBuilder: props.rowMenuItems, | ||
}) | ||
) | ||
|
||
return { | ||
props: { | ||
...props, | ||
data: data.slice(0, TABLE_DATA_BUFFER_SIZE), | ||
totalRecords: data.length, | ||
columns, | ||
} as T_IO_PROPS<'DISPLAY_TABLE'>, | ||
async onStateChange(newState: T_IO_STATE<'DISPLAY_TABLE'>) { | ||
const filtered = filterRows({ | ||
queryTerm: newState.queryTerm, | ||
data, | ||
}) | ||
|
||
const sorted = sortRows({ | ||
data: filtered, | ||
column: newState.sortColumn ?? null, | ||
direction: newState.sortDirection ?? null, | ||
}) | ||
|
||
return { | ||
...props, | ||
data: sorted.slice( | ||
newState.offset, | ||
newState.offset + TABLE_DATA_BUFFER_SIZE | ||
), | ||
totalRecords: sorted.length, | ||
columns: columns.map(c => ({ label: c.label })), | ||
} | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { z } from 'zod' | ||
import Logger from '../classes/Logger' | ||
import { | ||
tableRow, | ||
T_IO_PROPS, | ||
menuItem, | ||
T_IO_RETURNS, | ||
T_IO_STATE, | ||
} from '../ioSchema' | ||
import { TableColumn } from '../types' | ||
import { | ||
columnsBuilder, | ||
filterRows, | ||
tableRowSerializer, | ||
sortRows, | ||
TABLE_DATA_BUFFER_SIZE, | ||
missingColumnMessage, | ||
} from '../utils/table' | ||
|
||
type PublicProps<Row> = Omit<T_IO_PROPS<'SELECT_TABLE'>, 'data' | 'columns'> & { | ||
data: Row[] | ||
columns?: (TableColumn<Row> | string)[] | ||
rowMenuItems?: (row: Row) => z.infer<typeof menuItem>[] | ||
} | ||
|
||
export default function selectTable(logger: Logger) { | ||
return function <Row extends z.input<typeof tableRow> = any>( | ||
props: PublicProps<Row> | ||
) { | ||
type DataList = typeof props['data'] | ||
|
||
const columns = columnsBuilder(props, column => | ||
logger.error(missingColumnMessage('io.select.table')(column)) | ||
) | ||
|
||
// Rendering all rows on initialization is necessary for filtering and sorting | ||
const data = props.data.map((row, index) => | ||
tableRowSerializer({ | ||
index, | ||
row, | ||
columns, | ||
menuBuilder: props.rowMenuItems, | ||
}) | ||
) | ||
|
||
return { | ||
props: { | ||
...props, | ||
data: data.slice(0, TABLE_DATA_BUFFER_SIZE), | ||
totalRecords: data.length, | ||
columns, | ||
}, | ||
getValue(response: T_IO_RETURNS<'SELECT_TABLE'>) { | ||
const indices = response.map(({ key }) => Number(key)) | ||
const rows = props.data.filter((_, idx) => indices.includes(idx)) | ||
return rows as DataList | ||
}, | ||
async onStateChange(newState: T_IO_STATE<'SELECT_TABLE'>) { | ||
const filtered = filterRows({ queryTerm: newState.queryTerm, data }) | ||
|
||
const sorted = sortRows({ | ||
data: filtered, | ||
column: newState.sortColumn ?? null, | ||
direction: newState.sortDirection ?? null, | ||
}) | ||
|
||
let selectedKeys: string[] = [] | ||
|
||
if (newState.isSelectAll) { | ||
selectedKeys = sorted.map(({ key }) => key) | ||
} | ||
|
||
return { | ||
...props, | ||
data: sorted.slice( | ||
newState.offset, | ||
newState.offset + TABLE_DATA_BUFFER_SIZE | ||
), | ||
totalRecords: sorted.length, | ||
selectedKeys, | ||
columns, | ||
} | ||
}, | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.