Refactor fetch of credential types

Refactor fetch of credential types
This commit is contained in:
nixocio
2022-02-23 09:12:12 -05:00
parent dc2a392f4c
commit afb8be4f0b

View File

@@ -12,6 +12,20 @@ import {
import useRequest from 'hooks/useRequest'; import useRequest from 'hooks/useRequest';
import CredentialForm from '../shared/CredentialForm'; import CredentialForm from '../shared/CredentialForm';
const fetchCredentialTypes = async (pageNo = 1, credentialTypes = []) => {
const { data } = await CredentialTypesAPI.read({
page_size: 200,
page: pageNo,
});
if (data.next) {
return fetchCredentialTypes(
pageNo + 1,
credentialTypes.concat(data.results)
);
}
return credentialTypes.concat(data.results);
};
function CredentialAdd({ me }) { function CredentialAdd({ me }) {
const history = useHistory(); const history = useHistory();
@@ -76,6 +90,7 @@ function CredentialAdd({ me }) {
history.push(`/credentials/${credentialId}/details`); history.push(`/credentials/${credentialId}/details`);
} }
}, [credentialId, history]); }, [credentialId, history]);
const { const {
isLoading, isLoading,
error, error,
@@ -83,23 +98,7 @@ function CredentialAdd({ me }) {
result, result,
} = useRequest( } = useRequest(
useCallback(async () => { useCallback(async () => {
const { data } = await CredentialTypesAPI.read({ page_size: 200 }); const credTypes = await fetchCredentialTypes();
const credTypes = data.results;
if (data.next && data.next.includes('page=2')) {
let pageNo = 2;
/* eslint-disable no-await-in-loop */
do {
const {
data: { results },
} = await CredentialTypesAPI.read({
page_size: 200,
page: pageNo,
});
credTypes.push(...results);
pageNo++;
} while (data.count !== credTypes.length);
} /* eslint-enable no-await-in-loop */
const creds = credTypes.reduce((credentialTypesMap, credentialType) => { const creds = credTypes.reduce((credentialTypesMap, credentialType) => {
credentialTypesMap[credentialType.id] = credentialType; credentialTypesMap[credentialType.id] = credentialType;
return credentialTypesMap; return credentialTypesMap;