Advertisement
ArCiGo

PoC

Feb 28th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Axios from 'axios';
  2. import * as Rx from 'rx';
  3. import { AggregateFunctions, ColumnDataType, CompareOperators } from './Column';
  4. import ColumnModel from './ColumnModel';
  5. import GridRequest from './GridRequest';
  6. import GridResponse from './GridResponse';
  7.  
  8. export default class RemoteDataSource implements IDataSource {
  9.  
  10.   public columns: ColumnModel[];
  11.   public dataStream: any;
  12.   public url: string;
  13.   public counter: number;
  14.  
  15.   constructor(url: string, columns: ColumnModel[]) {
  16.     this.url = url;
  17.     this.counter = 0;
  18.     this.dataStream = new Rx.BehaviorSubject({ Payload: [] });
  19.     this.columns = columns;
  20.   }
  21.  
  22.   public connect(rowsPerPage: number, page: number, searchText: string) {
  23.     this.getAllRecords(rowsPerPage, page, searchText);
  24.     return this.dataStream;
  25.   }
  26.  
  27.   public getAllRecords = (rowsPerPage: number, page: number, searchText: string): Promise<object> =>
  28.   new Promise((resolve, reject) => {
  29.     const request = new GridRequest({
  30.       Columns: this.columns,
  31.       Count: this.counter++,
  32.       Search: { Text: searchText ? searchText : '', Operator: 'Auto' },
  33.       Skip: page * rowsPerPage,
  34.       Take: rowsPerPage,
  35.       TimezoneOffset: 360
  36.     });
  37.  
  38.     Axios.post(this.url, request).then((response) => {
  39.       if (response.data === undefined || !this.isValidResponse(response.data)) {
  40.         throw new Error('It\'s not a valid Tubular response object');
  41.       }
  42.  
  43.       const data = response.data.Payload;
  44.       const rows = data.map((row: any) => {
  45.         const obj: any = {};
  46.  
  47.         this.columns.forEach((column: any, key: any) => {
  48.           obj[column.Name] = row[key] || row[column.Name];
  49.         });
  50.  
  51.         return obj;
  52.       });
  53.  
  54.       resolve(this.dataStream.onNext(
  55.         (new GridResponse({
  56.             Aggregate: response.data.AggregationPayload,
  57.             FilteredRecordCount: response.data.FilteredRecordCount,
  58.             Payload: rows,
  59.             RowsPerPage: rowsPerPage,
  60.             SearchText: searchText,
  61.             TotalRecordCount: response.data.TotalRecordCount
  62.           })
  63.       )));
  64.     }).catch((error) => {
  65.       reject(error);
  66.     });
  67.   })
  68.  
  69.   public isValidResponse(response: object) {
  70.     const expectedStructure: any = {
  71.       AggregationPayload: null,
  72.       Counter: null,
  73.       CurrentPage: null,
  74.       FilteredRecordCount: null,
  75.       Payload: null,
  76.       TotalPages: null,
  77.       TotalRecordCount: null
  78.     };
  79.  
  80.     const expectedStructureKeys = Object.keys(expectedStructure).sort();
  81.     const responseKeys = Object.keys(response).sort();
  82.  
  83.     return JSON.stringify(expectedStructureKeys) === JSON.stringify(responseKeys);
  84.   }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement