Skip to content

Commit

Permalink
Merge pull request #106 from irontec/fetch-all-pages-helper
Browse files Browse the repository at this point in the history
Added fetchAllPages helper function
  • Loading branch information
mmadariaga authored Mar 8, 2024
2 parents 4c6bee6 + 45aa3bb commit bf8ffbf
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 74 deletions.
2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@irontec/ivoz-ui",
"version": "1.2.1",
"version": "1.3.0",
"description": "UI library used in ivozprovider",
"license": "GPL-3.0",
"main": "index.js",
Expand Down
97 changes: 24 additions & 73 deletions library/src/entities/DefaultEntityBehavior.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CancelToken } from 'axios';
import * as React from 'react';
import { EntityValues } from '../services/entity/EntityService';
import { StoreContainer } from '../store';
import {
ChildDecoratorProps,
ChildDecoratorType,
Expand All @@ -11,6 +10,7 @@ import {
calculateAclType,
} from './EntityInterface';

import { fetchAllPages } from '../helpers/fechAllPages';
import { EntityItem, isEntityItem } from '../router';
import autoForeignKeyResolver from './DefaultEntityBehavior/AutoForeignKeyResolver';
import autoSelectOptions from './DefaultEntityBehavior/AutoSelectOptions';
Expand All @@ -34,7 +34,6 @@ import marshaller, {
import unmarshaller from './DefaultEntityBehavior/Unmarshaller';
import validator from './DefaultEntityBehavior/Validator';
import View from './DefaultEntityBehavior/View';
import { DropdownArrayChoice, DropdownChoices } from 'services';

export const initialValues = {};

Expand Down Expand Up @@ -81,6 +80,7 @@ export const ChildDecoratorMemo = React.memo(

export type FormOnChangeEvent = React.ChangeEvent<{ name: string; value: any }>;

/** deprecated, use fetchAllPages instead */
const fetchFks = async (
endpoint: string,
properties:
Expand All @@ -89,66 +89,16 @@ const fetchFks = async (
setter: FetchFksCallback,
cancelToken?: CancelToken
): Promise<unknown> => {
const getAction = StoreContainer.store.getActions().api.get;

let keepGoing = true;
let _page: number = parseInt(
endpoint.match(/_page=([0-9]+)/)?.[1] || '1',
10
);

const response: DropdownChoices = [];
let loopCount = 0;
while (keepGoing) {
try {
if (loopCount > 100) {
console.error('Too much requests');
break;
}

loopCount++;
const result = await getAction({
path: endpoint,
silenceErrors: false,
params: {
_pagination: false,
_itemsPerPage: 100,
_properties: properties,
_page,
},
successCallback: async (data, headers: Record<string, string>) => {
response.push(...(data as Array<DropdownArrayChoice>));

const totalItems = parseInt(
headers?.['x-total-items'] || `${response.length}`,
10
);

if (
response.length >= totalItems ||
!(data as Array<DropdownArrayChoice>).length
) {
keepGoing = false;
}
_page++;
},
cancelToken,
});

if (!result) {
// Cancel token or 403
break;
}
} catch (error) {
console.error(error);
break;
}
}

setter(response);

return response;
return fetchAllPages({
endpoint,
params: {
properties,
},
setter,
cancelToken,
});
};

export type fetchFksType = typeof fetchFks;

const DefaultEntityBehavior = {
Expand Down Expand Up @@ -183,30 +133,31 @@ const DefaultEntityBehavior = {
return module.default;
},
fetchFks,
fetchAllFks: fetchAllPages,
defaultOrderBy: 'id',
defaultOrderDirection: OrderDirection.asc,
};

export default DefaultEntityBehavior;
export {
validator,
marshaller,
unmarshaller,
Form,
ListDecorator,
View,
autoForeignKeyResolver,
autoSelectOptions,
ListDecorator,
foreignKeyResolver,
foreignKeyGetter,
filterFieldsetGroups,
Form,
View,
foreignKeyGetter,
foreignKeyResolver,
marshaller,
unmarshaller,
validator,
};
export type {
PropertyFkChoices,
EntityFormProps,
EntityFormType,
FieldsetGroups,
FkChoices,
NullablePropertyFkChoices,
EntityFormProps,
MarshallerValues,
FieldsetGroups,
NullablePropertyFkChoices,
PropertyFkChoices,
};
78 changes: 78 additions & 0 deletions library/src/helpers/fechAllPages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { CancelToken } from 'axios';
import { StoreContainer } from '../store';
import { FetchFksCallback } from '../entities/EntityInterface';

import { DropdownArrayChoice, DropdownChoices } from '../services';

export type fetchAllPagesProps = {
endpoint: string;
params: Record<string, unknown>;
setter: FetchFksCallback;
cancelToken?: CancelToken;
};

export const fetchAllPages = async (
props: fetchAllPagesProps
): Promise<unknown> => {
const { endpoint, params, setter, cancelToken } = props;

const getAction = StoreContainer.store.getActions().api.get;

let keepGoing = true;
let _page: number = parseInt(
endpoint.match(/_page=([0-9]+)/)?.[1] || '1',
10
);

const response: DropdownChoices = [];
let loopCount = 0;
while (keepGoing) {
try {
if (loopCount > 5) {
console.error('Too much requests');
break;
}

loopCount++;
const result = await getAction({
path: endpoint,
silenceErrors: false,
params: {
...params,
_pagination: false,
_itemsPerPage: 200,
_page,
},
successCallback: async (data, headers: Record<string, string>) => {
response.push(...(data as Array<DropdownArrayChoice>));

const totalItems = parseInt(
headers?.['x-total-items'] || `${response.length}`,
10
);

if (
response.length >= totalItems ||
!(data as Array<DropdownArrayChoice>).length
) {
keepGoing = false;
}
_page++;
},
cancelToken,
});

if (!result) {
// Cancel token or 403
break;
}
} catch (error) {
console.error(error);
break;
}
}

setter(response);

return response;
};

0 comments on commit bf8ffbf

Please sign in to comment.