Skip to content

Commit

Permalink
Removing the last subscriber of the last page goes to previous page (#…
Browse files Browse the repository at this point in the history
…85587)

* Removing the last subscriber of the last page goes to previous page

* Using pages value instead of substracting one
  • Loading branch information
JuanLucha authored Dec 21, 2023
1 parent 3acc72d commit f7e7eeb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { numberFormat, translate } from 'i18n-calypso';
import { useEffect } from 'react';
import Pagination from 'calypso/components/pagination';
import { EmptyListView } from 'calypso/my-sites/subscribers/components/empty-list-view';
import { NoSearchResults } from 'calypso/my-sites/subscribers/components/no-search-results';
Expand All @@ -12,7 +13,6 @@ import isAtomicSite from 'calypso/state/selectors/is-site-automated-transfer';
import { isSimpleSite } from 'calypso/state/sites/selectors';
import { useRecordSearch } from '../../tracks';
import { GrowYourAudience } from '../grow-your-audience';

import './style.scss';

type SubscriberListContainerProps = {
Expand All @@ -26,14 +26,29 @@ const SubscriberListContainer = ( {
onClickView,
onClickUnsubscribe,
}: SubscriberListContainerProps ) => {
const { grandTotal, total, perPage, page, pageChangeCallback, searchTerm, isLoading } =
useSubscribersPage();
const {
grandTotal,
total,
perPage,
page,
pageChangeCallback,
searchTerm,
isLoading,
subscribers,
pages,
} = useSubscribersPage();
useRecordSearch();

const isSimple = useSelector( isSimpleSite );
const isAtomic = useSelector( ( state ) => isAtomicSite( state, siteId ) );
const EmptyComponent = isSimple || isAtomic ? SubscriberLaunchpad : EmptyListView;

useEffect( () => {
if ( ! isLoading && subscribers.length === 0 && page > 1 ) {
pageChangeCallback( pages ?? 0 );
}
}, [ isLoading, subscribers, page, pageChangeCallback ] );

return (
<section className="subscriber-list-container">
{ ! isLoading && ( Boolean( grandTotal ) || searchTerm ) && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type SubscribersPageContextProps = {
closeAllModals: typeof closeAllModals;
siteId: number | null;
isLoading: boolean;
pages?: number;
};

const SubscribersPageContext = createContext< SubscribersPageContextProps | undefined >(
Expand Down Expand Up @@ -112,6 +113,7 @@ export const SubscribersPageProvider = ( {
filterOption: subscriberType,
} );
const grandTotal = subscribersQueryResult.data?.total || 0;
const pages = subscribersQueryResult.data?.pages || 0;

const { pageChangeCallback } = usePagination(
pageNumber,
Expand Down Expand Up @@ -220,6 +222,7 @@ export const SubscribersPageProvider = ( {
migrateSubscribersCallback,
siteId,
isLoading: subscribersQueryResult.isLoading,
pages,
} }
>
{ children }
Expand Down

0 comments on commit f7e7eeb

Please sign in to comment.