fix loading jank in MultiCredentialLookup

This commit is contained in:
Keith Grant
2019-12-04 10:57:01 -08:00
parent 6e64b5c070
commit c003e89ea9
3 changed files with 41 additions and 6 deletions

View File

@@ -4,8 +4,8 @@ import { withI18n } from '@lingui/react';
import { EmptyState, EmptyStateBody } from '@patternfly/react-core'; import { EmptyState, EmptyStateBody } from '@patternfly/react-core';
// TODO: Better loading state - skeleton lines / spinner, etc. // TODO: Better loading state - skeleton lines / spinner, etc.
const ContentLoading = ({ i18n }) => ( const ContentLoading = ({ className, i18n }) => (
<EmptyState> <EmptyState className={className}>
<EmptyStateBody>{i18n._(t`Loading...`)}</EmptyStateBody> <EmptyStateBody>{i18n._(t`Loading...`)}</EmptyStateBody>
</EmptyState> </EmptyState>
); );

View File

@@ -37,6 +37,7 @@ function MultiCredentialsLookup(props) {
const [selectedType, setSelectedType] = useState(null); const [selectedType, setSelectedType] = useState(null);
const [credentials, setCredentials] = useState([]); const [credentials, setCredentials] = useState([]);
const [credentialsCount, setCredentialsCount] = useState(0); const [credentialsCount, setCredentialsCount] = useState(0);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => { useEffect(() => {
(async () => { (async () => {
@@ -56,6 +57,7 @@ function MultiCredentialsLookup(props) {
return; return;
} }
try { try {
setIsLoading(true);
const params = parseQueryString(QS_CONFIG, history.location.search); const params = parseQueryString(QS_CONFIG, history.location.search);
const { results, count } = await loadCredentials( const { results, count } = await loadCredentials(
params, params,
@@ -63,6 +65,7 @@ function MultiCredentialsLookup(props) {
); );
setCredentials(results); setCredentials(results);
setCredentialsCount(count); setCredentialsCount(count);
setIsLoading(false);
} catch (err) { } catch (err) {
onError(err); onError(err);
} }
@@ -133,6 +136,7 @@ function MultiCredentialsLookup(props) {
name="credentials" name="credentials"
qsConfig={QS_CONFIG} qsConfig={QS_CONFIG}
readOnly={!canDelete} readOnly={!canDelete}
isLoading={isLoading}
selectItem={item => { selectItem={item => {
if (isMultiple) { if (isMultiple) {
return dispatch({ type: 'SELECT_ITEM', item }); return dispatch({ type: 'SELECT_ITEM', item });

View File

@@ -25,10 +25,34 @@ import PaginatedDataListItem from './PaginatedDataListItem';
class PaginatedDataList extends React.Component { class PaginatedDataList extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {
height: 0,
};
this.ref = React.createRef();
this.handleSetPage = this.handleSetPage.bind(this); this.handleSetPage = this.handleSetPage.bind(this);
this.handleSetPageSize = this.handleSetPageSize.bind(this); this.handleSetPageSize = this.handleSetPageSize.bind(this);
} }
componentDidUpdate(prevProps) {
const { items } = this.props;
if (prevProps.items !== items) {
this.findHeight();
}
}
findHeight() {
if (!this.ref || !this.ref.current) {
return;
}
const { state } = this;
const height = this.ref.current.scrollHeight;
if (height && height !== state.height) {
this.setState({
height: this.ref.current.scrollHeight,
});
}
}
handleSetPage(event, pageNumber) { handleSetPage(event, pageNumber) {
const { history, qsConfig } = this.props; const { history, qsConfig } = this.props;
const { search } = history.location; const { search } = history.location;
@@ -66,6 +90,7 @@ class PaginatedDataList extends React.Component {
i18n, i18n,
renderToolbar, renderToolbar,
} = this.props; } = this.props;
const { height } = this.state;
const columns = toolbarColumns.length const columns = toolbarColumns.length
? toolbarColumns ? toolbarColumns
: [ : [
@@ -85,8 +110,14 @@ class PaginatedDataList extends React.Component {
const emptyContentTitle = i18n._(t`No ${pluralizedItemName} Found `); const emptyContentTitle = i18n._(t`No ${pluralizedItemName} Found `);
let Content; let Content;
if (hasContentLoading && items.length <= 0) { if (hasContentLoading) {
Content = <ContentLoading />; Content = (
<ContentLoading
css={`
min-height: ${height}px;
`}
/>
);
} else if (contentError) { } else if (contentError) {
Content = <ContentError error={contentError} />; Content = <ContentError error={contentError} />;
} else if (items.length <= 0) { } else if (items.length <= 0) {
@@ -100,7 +131,7 @@ class PaginatedDataList extends React.Component {
} }
return ( return (
<Fragment> <div ref={this.ref}>
<ListHeader <ListHeader
itemCount={itemCount} itemCount={itemCount}
renderToolbar={renderToolbar} renderToolbar={renderToolbar}
@@ -129,7 +160,7 @@ class PaginatedDataList extends React.Component {
onPerPageSelect={this.handleSetPageSize} onPerPageSelect={this.handleSetPageSize}
/> />
) : null} ) : null}
</Fragment> </div>
); );
} }
} }