Skip to content

Commit

Permalink
fix pagination for other periods
Browse files Browse the repository at this point in the history
  • Loading branch information
mavegaf committed Dec 10, 2024
1 parent 9f2350a commit b211b9a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
14 changes: 12 additions & 2 deletions client/my-sites/stats/stats-period-navigation/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ class StatsPeriodNavigation extends PureComponent {
const previousDayQuery = qs.stringify( Object.assign( {}, queryParams, newQueryParams ), {
addQueryPrefix: true,
} );
const href = `${ url }${ previousDayQuery }`;

let href = null;
if ( url ) {
href = `${ url }${ previousDayQuery }`;
}

this.handleArrowEvent( 'previous', href );
};

Expand All @@ -125,7 +130,12 @@ class StatsPeriodNavigation extends PureComponent {
const nextDayQuery = qs.stringify( Object.assign( {}, queryParams, newQueryParams ), {
addQueryPrefix: true,
} );
const href = `${ url }${ nextDayQuery }`;

let href = null;
if ( url ) {
href = `${ url }${ nextDayQuery }`;
}

this.handleArrowEvent( 'next', href );
};

Expand Down
1 change: 0 additions & 1 deletion client/my-sites/stats/stats-post-detail/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ class StatsPostDetail extends Component {
siteId={ siteId }
postId={ postId }
supportsUTMStats={ supportsUTMStats }
siteSlug={ siteSlug }
/>
<PostDetailTableSection siteId={ siteId } postId={ postId } />
</>
Expand Down
28 changes: 15 additions & 13 deletions client/my-sites/stats/stats-post-summary/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class StatsPostSummary extends Component {
siteId: PropTypes.number,
translate: PropTypes.func,
supportsUTMStats: PropTypes.bool,
siteSlug: PropTypes.string,
};

static MAX_RECORDS_PER_DAY = 10;
Expand Down Expand Up @@ -69,9 +68,7 @@ class StatsPostSummary extends Component {
selectedRecord = chartData[ chartData.length - 1 ];
}

const recordIndex = chartData.findIndex(
( item ) => item.startDate === selectedRecord.startDate
);
const recordIndex = chartData.findIndex( ( item ) => item.period === selectedRecord.period );

if ( 'previous' === direction ) {
if ( recordIndex > 0 ) {
Expand Down Expand Up @@ -221,7 +218,7 @@ class StatsPostSummary extends Component {
}

render() {
const { isRequesting, postId, siteId, translate, siteSlug, stats } = this.props;
const { isRequesting, postId, siteId, translate, stats } = this.props;
const periods = [
{ id: 'day', label: translate( 'Days' ) },
{ id: 'week', label: translate( 'Weeks' ) },
Expand All @@ -234,13 +231,18 @@ class StatsPostSummary extends Component {
selectedRecord = chartData[ chartData.length - 1 ];
}

const maxPages = Math.ceil( stats.data.length / StatsPostSummary.MAX_RECORDS_PER_DAY );
let disablePreviousArrow = false;
if ( this.state.page >= maxPages ) {
const selectedRecordIndex = chartData.findIndex(
( item ) => item.startDate === selectedRecord.startDate
);
let disableNextArrow = false;
const selectedRecordIndex = chartData.findIndex(
( item ) => item.period === selectedRecord.period
);
if ( 'day' === this.state.period ) {
const maxPages = Math.ceil( stats.data.length / StatsPostSummary.MAX_RECORDS_PER_DAY );
disablePreviousArrow = this.state.page >= maxPages && selectedRecordIndex === 0;
disableNextArrow = 1 === this.state.page && selectedRecordIndex === chartData.length - 1;
} else {
disablePreviousArrow = selectedRecordIndex === 0;
disableNextArrow = selectedRecordIndex === chartData.length - 1;
}

const summaryWrapperClass = clsx( 'stats-post-summary', 'is-chart-tabs', {
Expand All @@ -256,10 +258,10 @@ class StatsPostSummary extends Component {
<StatsPeriodNavigation
showArrows
onPeriodChange={ this.onPeriodChange }
url={ `/stats/post/${ postId }/${ siteSlug }` }
date={ selectedRecord?.startDate }
period={ this.state.period }
disablePreviousArrow={ disablePreviousArrow }
disableNextArrow={ disableNextArrow }
date={ null }
period={ this.state.period }
>
<DatePicker period={ this.state.period } date={ selectedRecord?.startDate } isShort />
</StatsPeriodNavigation>
Expand Down

0 comments on commit b211b9a

Please sign in to comment.