Merge pull request #7786 from mabashian/convert-OrganizationLookup-useRequest

Updates OrganizationLookup to use useRequest

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot]
2020-07-31 19:29:50 +00:00
committed by GitHub
2 changed files with 30 additions and 20 deletions

View File

@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'; import React, { useCallback, useEffect } from 'react';
import { node, func, bool } from 'prop-types'; import { node, func, bool } from 'prop-types';
import { withRouter } from 'react-router-dom'; import { withRouter } from 'react-router-dom';
import { withI18n } from '@lingui/react'; import { withI18n } from '@lingui/react';
@@ -7,6 +7,7 @@ import { FormGroup } from '@patternfly/react-core';
import { OrganizationsAPI } from '../../api'; import { OrganizationsAPI } from '../../api';
import { Organization } from '../../types'; import { Organization } from '../../types';
import { getQSConfig, parseQueryString } from '../../util/qs'; import { getQSConfig, parseQueryString } from '../../util/qs';
import useRequest from '../../util/useRequest';
import OptionsList from '../OptionsList'; import OptionsList from '../OptionsList';
import Lookup from './Lookup'; import Lookup from './Lookup';
import LookupErrorMessage from './shared/LookupErrorMessage'; import LookupErrorMessage from './shared/LookupErrorMessage';
@@ -27,22 +28,28 @@ function OrganizationLookup({
value, value,
history, history,
}) { }) {
const [organizations, setOrganizations] = useState([]); const {
const [count, setCount] = useState(0); result: { itemCount, organizations },
const [error, setError] = useState(null); error: contentError,
request: fetchOrganizations,
} = useRequest(
useCallback(async () => {
const params = parseQueryString(QS_CONFIG, history.location.search);
const { data } = await OrganizationsAPI.read(params);
return {
organizations: data.results,
itemCount: data.count,
};
}, [history.location]),
{
organizations: [],
itemCount: 0,
}
);
useEffect(() => { useEffect(() => {
(async () => { fetchOrganizations();
const params = parseQueryString(QS_CONFIG, history.location.search); }, [fetchOrganizations]);
try {
const { data } = await OrganizationsAPI.read(params);
setOrganizations(data.results);
setCount(data.count);
} catch (err) {
setError(err);
}
})();
}, [history.location]);
return ( return (
<FormGroup <FormGroup
@@ -65,7 +72,7 @@ function OrganizationLookup({
<OptionsList <OptionsList
value={state.selectedItems} value={state.selectedItems}
options={organizations} options={organizations}
optionCount={count} optionCount={itemCount}
multiple={state.multiple} multiple={state.multiple}
header={i18n._(t`Organization`)} header={i18n._(t`Organization`)}
name="organization" name="organization"
@@ -77,11 +84,11 @@ function OrganizationLookup({
isDefault: true, isDefault: true,
}, },
{ {
name: i18n._(t`Created By (Username)`), name: i18n._(t`Created by (username)`),
key: 'created_by__username', key: 'created_by__username',
}, },
{ {
name: i18n._(t`Modified By (Username)`), name: i18n._(t`Modified by (username)`),
key: 'modified_by__username', key: 'modified_by__username',
}, },
]} ]}
@@ -97,7 +104,7 @@ function OrganizationLookup({
/> />
)} )}
/> />
<LookupErrorMessage error={error} /> <LookupErrorMessage error={contentError} />
</FormGroup> </FormGroup>
); );
} }

View File

@@ -181,7 +181,10 @@ describe('<CredentialAdd />', () => {
test('handleCancel should return the user back to the credentials list', async () => { test('handleCancel should return the user back to the credentials list', async () => {
await waitForElement(wrapper, 'isLoading', el => el.length === 0); await waitForElement(wrapper, 'isLoading', el => el.length === 0);
wrapper.find('Button[aria-label="Cancel"]').simulate('click'); await act(async () => {
wrapper.find('Button[aria-label="Cancel"]').simulate('click');
});
wrapper.update();
expect(history.location.pathname).toEqual('/credentials'); expect(history.location.pathname).toEqual('/credentials');
}); });
}); });