summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-fetch-data-context.ts
blob: 7937d82e6f3e62cf2d04e7eb872b12c9bed09a4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { HttpParams } from '@angular/common/http';

import { PageInfo } from './cd-table-paging';

export class CdTableFetchDataContext {
  errorConfig = {
    resetData: true, // Force data table to show no data
    displayError: true // Show an error panel above the data table
  };

  /**
   * The function that should be called from within the error handler
   * of the 'fetchData' function to display the error panel and to
   * reset the data table to the correct state.
   */
  error: Function;
  pageInfo: PageInfo = new PageInfo();
  search = '';
  sort = '+name';

  constructor(error: () => void) {
    this.error = error;
  }

  toParams(): HttpParams {
    if (this.pageInfo.limit === null) {
      this.pageInfo.limit = 0;
    }
    if (!this.search) {
      this.search = '';
    }
    if (!this.sort || this.sort.length < 2) {
      this.sort = '+name';
    }
    return new HttpParams({
      fromObject: {
        offset: String(this.pageInfo.offset * this.pageInfo.limit),
        limit: String(this.pageInfo.limit),
        search: this.search,
        sort: this.sort
      }
    });
  }
}