import React from 'react'; import { EmptyState, EmptyStateBody, EmptyStateIcon, EmptyStateVariant, EmptyStateSecondaryActions, Bullseye, Title, Button, } from '@patternfly/react-core'; import PropTypes from 'prop-types'; import { translate as __ } from 'foremanReact/common/I18n'; import { CubeIcon, ExclamationCircleIcon, SearchIcon, CheckCircleIcon } from '@patternfly/react-icons'; import { global_danger_color_200 as dangerColor, global_success_color_100 as successColor } from '@patternfly/react-tokens'; import { useDispatch, useSelector } from 'react-redux'; import { selectHostDetailsClearSearch } from '../extensions/HostDetails/HostDetailsSelectors'; const KatelloEmptyStateIcon = ({ error, search, customIcon, happyIcon, }) => { if (error) return ; if (search) return ; if (happyIcon) return ; if (customIcon) return ; return ; }; const EmptyStateMessage = ({ title, body, error, search, customIcon, happy, ...extraTableProps }) => { let emptyStateTitle = title; let emptyStateBody = body; const { primaryActionTitle, showPrimaryAction, showSecondaryAction, secondaryActionTitle, primaryActionLink, secondaryActionLink, searchIsActive, resetFilters, filtersAreActive, requestKey, primaryActionButton, } = extraTableProps; if (error) { if (error?.response?.data?.error) { const { response: { data: { error: { message, details } } } } = error; emptyStateTitle = message; emptyStateBody = details; } else if (error?.response?.status) { const { response: { status } } = error; emptyStateTitle = status; emptyStateBody = error?.response?.data?.displayMessage || __('Something went wrong! Please check server logs!'); } } const secondaryActionText = searchIsActive ? __('Clear search') : __('Clear filters'); const dispatch = useDispatch(); const clearSearch = useSelector(selectHostDetailsClearSearch); const handleClick = () => { if (searchIsActive) { clearSearch(); } if (filtersAreActive) { resetFilters(); } dispatch({ type: `${requestKey}_REQUEST`, key: requestKey, }); }; const actionButton = primaryActionButton ?? ( ); return ( {emptyStateTitle} {emptyStateBody} {showPrimaryAction ? actionButton : null} {showSecondaryAction && } {(searchIsActive || !!filtersAreActive) && } ); }; KatelloEmptyStateIcon.propTypes = { error: PropTypes.bool, search: PropTypes.bool, customIcon: PropTypes.elementType, happyIcon: PropTypes.bool, }; KatelloEmptyStateIcon.defaultProps = { error: false, search: false, customIcon: undefined, happyIcon: undefined, }; EmptyStateMessage.propTypes = { title: PropTypes.string, body: PropTypes.oneOfType([PropTypes.string, PropTypes.shape({})]), error: PropTypes.oneOfType([ PropTypes.shape({}), PropTypes.string, ]), search: PropTypes.bool, customIcon: PropTypes.elementType, happy: PropTypes.bool, searchIsActive: PropTypes.bool, activeFilters: PropTypes.arrayOf(PropTypes.oneOfType([ PropTypes.string, PropTypes.arrayOf(PropTypes.string), ])), defaultFilters: PropTypes.arrayOf(PropTypes.oneOfType([ PropTypes.string, PropTypes.arrayOf(PropTypes.string), ])), // eslint-disable-next-line react/require-default-props resetFilters: (props, propName) => { if (props.defaultFilters?.length || props.activeFilters?.length) { if (typeof props[propName] !== 'function') { return new Error(`A ${propName} function is required when using activeFilters or defaultFilters`); } } return null; }, showPrimaryAction: PropTypes.bool, showSecondaryAction: PropTypes.bool, primaryActionButton: PropTypes.element, }; EmptyStateMessage.defaultProps = { title: __('Unable to connect'), body: __('There was an error retrieving data from the server. Check your connection and try again.'), error: undefined, search: false, customIcon: undefined, happy: false, searchIsActive: false, activeFilters: [], defaultFilters: [], showPrimaryAction: false, showSecondaryAction: false, primaryActionButton: undefined, }; export default EmptyStateMessage;