feat: Extend renderer functionality to work with pagination
This commit is contained in:
@@ -1,14 +1,27 @@
|
||||
const isPaginated = (object) =>
|
||||
object?.pageInfo &&
|
||||
object?.totalCount !== undefined &&
|
||||
Array.isArray(object?.records);
|
||||
|
||||
const isArray = (object) =>
|
||||
Array.isArray(object) || Array.isArray(object?.records);
|
||||
|
||||
const totalCount = (object) =>
|
||||
isPaginated(object) ? object.totalCount : isArray(object) ? object.length : 1;
|
||||
|
||||
const renderObject = (response, object) => {
|
||||
const isArray = Array.isArray(object);
|
||||
const data = isPaginated(object) ? object.records : object;
|
||||
|
||||
const computedPayload = {
|
||||
data: object,
|
||||
data,
|
||||
meta: {
|
||||
type: object.constructor.name,
|
||||
count: isArray ? object.length : 1,
|
||||
isArray,
|
||||
currentPage: null,
|
||||
totalPages: null,
|
||||
type: isPaginated(object)
|
||||
? object.records[0].constructor.name
|
||||
: object.constructor.name,
|
||||
count: totalCount(object),
|
||||
isArray: isArray(object),
|
||||
currentPage: isPaginated(object) ? object.pageInfo.currentPage : null,
|
||||
totalPages: isPaginated(object) ? object.pageInfo.totalPages : null,
|
||||
},
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user