Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { IDatasource, SortModelItem } from 'ag-grid-community';
- import { defaultSort, quickSearchFields } from 'constants/tool';
- import { loadTools } from 'ducks/Tool/operations';
- import { useDispatch, useSelector } from 'react-redux';
- import { CONTEXT } from 'ducks/Tool';
- interface ICustomDatasourceParams {
- pageSize: number;
- }
- export default class CustomDatasource implements IDatasource {
- private params: ICustomDatasourceParams;
- private dispatch = useDispatch();
- private toolImages = useSelector((state: any) => state.toolImages);
- constructor(params: ICustomDatasourceParams) {
- this.params = params;
- }
- getFilterConditions = (filterModel: any) => {
- const filterKeys = Object.keys(filterModel);
- return filterKeys.includes('-')
- ? [{ field: quickSearchFields, contains: filterModel['-'].filter }]
- : filterKeys.map((filter: string) => ({
- field: [`thing.${filter}`],
- contains: filterModel[filter].filter,
- }));
- };
- getOrderColumns = (sortModel: SortModelItem[]) =>
- sortModel.length > 0
- ? sortModel.map(({ sort, colId }) => ({
- id: colId.replace('thing_seen.', ''),
- column: colId === 'auditLastSeenBy' ? 'auditorName' : colId,
- direction: sort,
- }))
- : [defaultSort];
- getRows(params: any) {
- const { endRow, filterModel, sortModel } = params;
- const orderColumns = this.getOrderColumns(sortModel);
- const filterConditions = this.getFilterConditions(filterModel);
- const searchBody = {
- pageNumber: Math.round(
- (endRow || this.params.pageSize) / this.params.pageSize
- ),
- pageSize: this.params.pageSize,
- orderColumns,
- filterConditions: filterConditions || [],
- };
- return this.dispatch(loadTools(CONTEXT)(searchBody) as any).then(
- (response: any) => {
- const rowData = response.payload.data.data.things;
- const rowCount = response.payload.data.data.metadata.searchTotalTools;
- params.successCallback(rowData, rowCount);
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement