Skip to content

Commit

Permalink
fix(cards-list): update empty state correctly (#20)
Browse files Browse the repository at this point in the history
Signed-off-by: Petr Kadlec <[email protected]>
  • Loading branch information
kapetr authored Oct 23, 2024
1 parent 8e58238 commit 400613b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/components/CardsList/CardsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function CardsList<T extends OrderBy>({
children,
}: PropsWithChildren<Props<T>>) {
const [search, setSearch] = useState<string | null>(null);
const isEmpty = useRef(false);
const [isEmptyState, setEmptyState] = useState(false);

const { ref: fetchMoreAnchorRef } = useFetchNextPageInView({
onFetchNextPage,
Expand All @@ -87,11 +87,14 @@ export function CardsList<T extends OrderBy>({
});

const noResults = totalCount === 0 && !isFetching;
const isEmpty = isEmptyState && noResults;

if (noResults && search == null) isEmpty.current = true;
useEffect(() => {
if (noResults && search == null) setEmptyState(true);
if (totalCount > 0) setEmptyState(false);
}, [noResults, search, totalCount]);

const showBarWithNewButton =
(onSearchChange || newButtonProps) && !isEmpty.current;
const showBarWithNewButton = (onSearchChange || newButtonProps) && !isEmpty;

return (
<section className={classes.root}>
Expand All @@ -117,7 +120,7 @@ export function CardsList<T extends OrderBy>({
<div className={classes.heading}>
{heading && <h2>{heading}</h2>}

{orderByProps && !isEmpty.current && (
{orderByProps && !isEmpty && (
<div className={classes.order}>
<OrderBySelect<T> {...orderByProps} />
</div>
Expand All @@ -127,9 +130,9 @@ export function CardsList<T extends OrderBy>({
</header>
)}

{!error && (isEmpty.current || noResults) && (
{!error && (isEmpty || noResults) && (
<div className={classes.empty}>
{isEmpty.current ? (
{isEmpty ? (
<>
<p>{noItemsText}</p>
{noItemsDescr && (
Expand Down

0 comments on commit 400613b

Please sign in to comment.