Skip to content

Commit

Permalink
mapParams - make mapPageToOffset a bool
Browse files Browse the repository at this point in the history
  • Loading branch information
himdel committed Nov 18, 2023
1 parent 28bfe3f commit 5ee2adb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 31 deletions.
51 changes: 21 additions & 30 deletions src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class BaseAPI {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
http: any;
sortParam: string; // translate ?sort into sortParam in list()
mapPageToOffset: boolean;

// a request URL is created from:
// * API_HOST - optional, for use with different hostname
Expand All @@ -26,45 +27,35 @@ export class BaseAPI {
this.http.interceptors.request.use((request) => this.authHandler(request));
}

// The api uses offset/limit OR page/page_size for pagination
// the UI uses page/page size and maps to whatever the api expects
// (override mapPageToOffset for page)
public mapPageToOffset(p) {
// Need to copy the object to make sure we aren't accidentally
// setting page state
const params = { ...p };
public mapParams(params) {
const newParams = { ...params };

const pageSize =
parseInt(params['page_size']) || Constants.DEFAULT_PAGE_SIZE;
const page = parseInt(params['page']) || 1;
if (this.mapPageToOffset) {
// The api uses offset/limit OR page/page_size for pagination
// the UI uses page/page size and maps to whatever the api expects

delete params['page'];
delete params['page_size'];
const pageSize =
parseInt(newParams['page_size'], 10) || Constants.DEFAULT_PAGE_SIZE;
const page = parseInt(newParams['page'], 10) || 1;

params['offset'] = page * pageSize - pageSize;
params['limit'] = pageSize;
delete newParams['page'];
delete newParams['page_size'];

return params;
}
newParams['offset'] = page * pageSize - pageSize;
newParams['limit'] = pageSize;
}

public mapParams(params) {
return {
params: this.mapSort(
this.mapPageToOffset ? this.mapPageToOffset(params) : params,
),
};
}
if (this.sortParam && newParams['sort'] && this.sortParam !== 'sort') {
// The api uses sort/ordering/order_by for sort
// the UI uses sort and maps to whatever the api expects

// The api uses sort/ordering/order_by for sort
// the UI uses sort and maps to whatever the api expects
// (set sortParam)
public mapSort(params) {
const newParams = { ...params };
if (newParams['sort'] && this.sortParam !== 'sort') {
newParams[this.sortParam] = newParams['sort'];
delete newParams['sort'];
}
return newParams;

return {
params: newParams,
};
}

list(params?: object, apiPath?: string) {
Expand Down
1 change: 1 addition & 0 deletions src/api/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { BaseAPI } from './base';

export class HubAPI extends BaseAPI {
apiBase = API_BASE_PATH;
mapPageToOffset = true; // offset & limit
sortParam = 'sort';
}
2 changes: 1 addition & 1 deletion src/api/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { BaseAPI } from './base';

export class LegacyAPI extends BaseAPI {
apiBase = API_BASE_PATH;
mapPageToOffset = false; // using page & page_size
mapPageToOffset = false; // page & page_size
sortParam = 'order_by';
}
1 change: 1 addition & 0 deletions src/api/pulp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { BaseAPI } from './base';

export class PulpAPI extends BaseAPI {
apiBase = PULP_API_BASE_PATH;
mapPageToOffset = true; // offset & limit
sortParam = 'ordering';
}

0 comments on commit 5ee2adb

Please sign in to comment.