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

Add arrows to switch dates for the post details page #97196

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
100 changes: 85 additions & 15 deletions client/my-sites/stats/stats-post-summary/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ class StatsPostSummary extends Component {
supportsUTMStats: PropTypes.bool,
};

static MAX_RECORDS_PER_DAY = 10;

state = {
selectedRecord: null,
period: 'day',
page: 1,
};

selectPeriod( period ) {
Expand All @@ -54,29 +57,76 @@ class StatsPostSummary extends Component {
this.setState( { selectedRecord: record } );
};

getChartData() {
onPeriodChange = ( { direction } ) => {
let chartData = this.getChartData();
if ( ! chartData.length ) {
return;
}

let selectedRecord = this.state.selectedRecord;
if ( ! selectedRecord ) {
selectedRecord = chartData[ chartData.length - 1 ];
}

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

if ( 'previous' === direction ) {
if ( recordIndex > 0 ) {
this.setState( { selectedRecord: chartData[ recordIndex - 1 ] } );
} else {
const nextPage = this.state.page + 1;
chartData = this.getChartData( nextPage );
if ( chartData ) {
this.setState( { selectedRecord: chartData[ chartData.length - 1 ] } );
}
}
} else if ( 'next' === direction ) {
if ( recordIndex < chartData.length - 1 ) {
this.setState( { selectedRecord: chartData[ recordIndex + 1 ] } );
} else {
const nextPage = this.state.page - 1;
chartData = this.getChartData( nextPage );
this.setState( { selectedRecord: chartData[ 0 ] } );
}
}
};

getChartData( newPage = 0 ) {
const { moment, stats } = this.props;
if ( ! stats ) {
return [];
}

let page = this.state.page;
if ( newPage ) {
page = newPage;
this.setState( { page: newPage } );
}

switch ( this.state.period ) {
case 'day':
case 'day': {
if ( ! stats.data ) {
return [];
}

return stats.data
.slice( Math.max( stats.data.length - 10, 0 ) )
.map( ( [ date, value ] ) => {
const momentDate = moment( date );
return {
period: momentDate.format( 'MMM D' ),
periodLabel: momentDate.format( 'LL' ),
startDate: date,
value,
};
} );
const dataStart = Math.max(
stats.data.length - StatsPostSummary.MAX_RECORDS_PER_DAY * page,
0
);
const dataEnd = Math.max(
stats.data.length - StatsPostSummary.MAX_RECORDS_PER_DAY * ( page - 1 ),
0
);
return stats.data.slice( dataStart, dataEnd ).map( ( [ date, value ] ) => {
const momentDate = moment( date );
return {
period: momentDate.format( 'MMM D' ),
periodLabel: momentDate.format( 'LL' ),
startDate: date,
value,
};
} );
}
case 'year':
if ( ! stats.years ) {
return [];
Expand Down Expand Up @@ -168,7 +218,7 @@ class StatsPostSummary extends Component {
}

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

let disablePreviousArrow = false;
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', {
'is-period-year': this.state.period === 'year',
} );
Expand All @@ -191,7 +255,13 @@ class StatsPostSummary extends Component {
<QueryPostStats siteId={ siteId } postId={ postId } />

<StatsPeriodHeader>
<StatsPeriodNavigation showArrows={ false }>
<StatsPeriodNavigation
showArrows
onPeriodChange={ this.onPeriodChange }
disablePreviousArrow={ disablePreviousArrow }
disableNextArrow={ disableNextArrow }
date={ null }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix the isToday check in a follow-up PR, which would make passing Null date unnecessary.

>
<DatePicker period={ this.state.period } date={ selectedRecord?.startDate } isShort />
</StatsPeriodNavigation>
<SegmentedControl primary>
Expand Down
Loading