Adds logic to recursively fetch input sources in the model method. This way we don't have to duplicate this logic every place that calls the method.

This commit is contained in:
mabashian 2020-06-09 18:06:30 -04:00
parent e7cd9bbb98
commit a1d1a1078b
3 changed files with 28 additions and 13 deletions

View File

@ -20,10 +20,31 @@ class Credentials extends Base {
return this.http.options(`${this.baseUrl}${id}/access_list/`);
}
readInputSources(id, params) {
return this.http.get(`${this.baseUrl}${id}/input_sources/`, {
params,
});
readInputSources(id) {
const fetchInputSources = async (pageNo = 1, inputSources = []) => {
try {
const { data } = await this.http.get(
`${this.baseUrl}${id}/input_sources/`,
{
params: {
page: pageNo,
page_size: 200,
},
}
);
if (data.next) {
return fetchInputSources(
pageNo + 1,
inputSources.concat(data.results)
);
}
return Promise.resolve(inputSources.concat(data.results));
} catch (error) {
return Promise.reject(error);
}
};
return fetchInputSources();
}
test(id, data) {

View File

@ -64,12 +64,10 @@ function CredentialDetail({ i18n, credential }) {
{
data: { inputs: credentialTypeInputs, managed_by_tower },
},
{
data: { results: loadedInputSources },
},
loadedInputSources,
] = await Promise.all([
CredentialTypesAPI.readDetail(credential_type.id),
CredentialsAPI.readInputSources(credentialId, { page_size: 200 }),
CredentialsAPI.readInputSources(credentialId),
]);
setFields(credentialTypeInputs.fields || []);

View File

@ -37,11 +37,7 @@ CredentialTypesAPI.readDetail.mockResolvedValue({
data: mockCredentialType,
});
CredentialsAPI.readInputSources.mockResolvedValue({
data: {
results: [mockInputSource],
},
});
CredentialsAPI.readInputSources.mockResolvedValue([mockInputSource]);
function expectDetailToMatch(wrapper, label, value) {
const detail = wrapper.find(`Detail[label="${label}"]`);