Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MGMT-18567: Solving errors in clusters list #2652

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Toolbar,
ToolbarItem,
ToolbarContent,
ToolbarFilter,
InputGroup,
TextInput,
ToolbarProps,
Expand All @@ -28,6 +27,7 @@ import { useTranslation } from '../../../common/hooks/use-translation-wrapper';
import { Cluster } from '@openshift-assisted/types/assisted-installer-service';
import { useDispatchDay1, useSelectorDay1 } from '../../store';
import { selectClustersUIState } from '../../store/slices/clusters/selectors';
import { CustomToolbarFilter } from './CustomToolbarFilter';

export type ClusterFiltersType = {
[key: string]: string[]; // value from clusterStatusLabels
Expand Down Expand Up @@ -103,10 +103,6 @@ const ClustersListToolbar: React.FC<ClustersListToolbarProps> = ({
);
const { t } = useTranslation();

React.useEffect(() => {
window.addEventListener('resize', () => setStatusExpanded(false));
}, []);

return (
<Toolbar
id="clusters-list-toolbar"
Expand All @@ -131,7 +127,7 @@ const ClustersListToolbar: React.FC<ClustersListToolbarProps> = ({
</InputGroupItem>
</InputGroup>
</ToolbarItem>
<ToolbarFilter
<CustomToolbarFilter
chips={filters.status}
deleteChip={onDeleteChip}
deleteChipGroup={onDeleteChipGroup}
Expand All @@ -155,7 +151,7 @@ const ClustersListToolbar: React.FC<ClustersListToolbarProps> = ({
/>
))}
</Select>
</ToolbarFilter>
</CustomToolbarFilter>
<ToolbarButton
variant={ButtonVariant.primary}
onClick={() => navigate(`~new`)}
Expand Down
131 changes: 131 additions & 0 deletions libs/ui-lib/lib/ocm/components/clusters/CustomToolbarFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {
Chip,
ChipGroup,
PickOptional,
ToolbarChip,
ToolbarContentContext,
ToolbarContext,
ToolbarFilterProps,
ToolbarItem,
} from '@patternfly/react-core';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

interface ToolbarFilterState {
isMounted: boolean;
}

class CustomToolbarFilter extends React.Component<ToolbarFilterProps, ToolbarFilterState> {
static displayName = 'CustomToolbarFilter';
static contextType = ToolbarContext;
context!: React.ContextType<typeof ToolbarContext>;
static defaultProps: PickOptional<ToolbarFilterProps> = {
chips: [] as (string | ToolbarChip)[],
showToolbarItem: true,
};

constructor(props: ToolbarFilterProps) {
super(props);
this.state = {
isMounted: false,
};
}

componentDidMount() {
const { categoryName, chips } = this.props;
this.context.updateNumberFilters(
typeof categoryName !== 'string' && categoryName.hasOwnProperty('key')
? categoryName.key
: categoryName.toString(),
chips ? chips.length : 0,
);
this.setState({ isMounted: true });
}

componentDidUpdate() {
const { categoryName, chips } = this.props;
this.context.updateNumberFilters(
typeof categoryName !== 'string' && categoryName.hasOwnProperty('key')
? categoryName.key
: categoryName.toString(),
chips ? chips.length : 0,
);
}

render() {
const {
children,
chips,
deleteChipGroup,
deleteChip,
chipGroupExpandedText,
chipGroupCollapsedText,
categoryName,
showToolbarItem,
isExpanded,
expandableChipContainerRef,
...props
} = this.props;
const { isExpanded: managedIsExpanded, chipGroupContentRef } = this.context;
const _isExpanded = isExpanded !== undefined ? isExpanded : managedIsExpanded;
const categoryKey =
typeof categoryName !== 'string' && categoryName.hasOwnProperty('key')
? categoryName.key
: categoryName.toString();

const chipGroup =
chips !== undefined && chips.length ? (
<ToolbarItem variant="chip-group">
<ChipGroup
key={categoryKey}
categoryName={typeof categoryName === 'string' ? categoryName : categoryName.name}
isClosable={deleteChipGroup !== undefined}
onClick={() => deleteChipGroup && deleteChipGroup(categoryName)}
collapsedText={chipGroupCollapsedText}
expandedText={chipGroupExpandedText}
>
{chips.map((chip) =>
typeof chip === 'string' ? (
<Chip key={chip} onClick={() => deleteChip && deleteChip(categoryKey, chip)}>
{chip}
</Chip>
) : (
<Chip key={chip.key} onClick={() => deleteChip && deleteChip(categoryKey, chip)}>
{chip.node}
</Chip>
),
)}
</ChipGroup>
</ToolbarItem>
) : null;

if (!_isExpanded && this.state.isMounted) {
return (
<React.Fragment>
{showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>}
{chipGroupContentRef &&
chipGroupContentRef.current !== null &&
chipGroupContentRef.current.firstElementChild !== null &&
ReactDOM.createPortal(chipGroup, chipGroupContentRef.current.firstElementChild)}
</React.Fragment>
);
}

return (
<ToolbarContentContext.Consumer>
{({ chipContainerRef }) => (
<React.Fragment>
{showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>}
{chipContainerRef.current &&
ReactDOM.createPortal(chipGroup, chipContainerRef.current as Element)}
{expandableChipContainerRef &&
expandableChipContainerRef.current &&
ReactDOM.createPortal(chipGroup, expandableChipContainerRef.current)}
</React.Fragment>
)}
</ToolbarContentContext.Consumer>
);
}
}

export { CustomToolbarFilter };
Loading