Advertisement
minafaw3

CustomDatasource

Mar 28th, 2023
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import { IDatasource, SortModelItem } from 'ag-grid-community';
  2. import { defaultSort, quickSearchFields } from 'constants/tool';
  3. import { loadTools } from 'ducks/Tool/operations';
  4. import { useDispatch, useSelector } from 'react-redux';
  5. import { CONTEXT } from 'ducks/Tool';
  6.  
  7. interface ICustomDatasourceParams {
  8. pageSize: number;
  9. }
  10.  
  11. export default class CustomDatasource implements IDatasource {
  12. private params: ICustomDatasourceParams;
  13.  
  14. private dispatch = useDispatch();
  15.  
  16. private toolImages = useSelector((state: any) => state.toolImages);
  17.  
  18. constructor(params: ICustomDatasourceParams) {
  19. this.params = params;
  20. }
  21.  
  22. getFilterConditions = (filterModel: any) => {
  23. const filterKeys = Object.keys(filterModel);
  24. return filterKeys.includes('-')
  25. ? [{ field: quickSearchFields, contains: filterModel['-'].filter }]
  26. : filterKeys.map((filter: string) => ({
  27. field: [`thing.${filter}`],
  28. contains: filterModel[filter].filter,
  29. }));
  30. };
  31.  
  32. getOrderColumns = (sortModel: SortModelItem[]) =>
  33. sortModel.length > 0
  34. ? sortModel.map(({ sort, colId }) => ({
  35. id: colId.replace('thing_seen.', ''),
  36. column: colId === 'auditLastSeenBy' ? 'auditorName' : colId,
  37. direction: sort,
  38. }))
  39. : [defaultSort];
  40.  
  41. getRows(params: any) {
  42. const { endRow, filterModel, sortModel } = params;
  43. const orderColumns = this.getOrderColumns(sortModel);
  44. const filterConditions = this.getFilterConditions(filterModel);
  45.  
  46. const searchBody = {
  47. pageNumber: Math.round(
  48. (endRow || this.params.pageSize) / this.params.pageSize
  49. ),
  50. pageSize: this.params.pageSize,
  51. orderColumns,
  52. filterConditions: filterConditions || [],
  53. };
  54.  
  55. return this.dispatch(loadTools(CONTEXT)(searchBody) as any).then(
  56. (response: any) => {
  57. const rowData = response.payload.data.data.things;
  58. const rowCount = response.payload.data.data.metadata.searchTotalTools;
  59. params.successCallback(rowData, rowCount);
  60. }
  61. );
  62. }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement