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

Stats: use Redux for Post Trends #5847

Merged
merged 13 commits into from
Jun 10, 2016
6 changes: 2 additions & 4 deletions client/my-sites/stats/post-trends/day.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getSiteStatsPostsCountByDay,
getSiteStatsMaxPostsByDay
} from 'state/stats/lists/selectors';
import { getStatsStreakQuery } from 'state/stats/lists/utils';

const PostTrendsDay = React.createClass( {

Expand Down Expand Up @@ -117,10 +118,7 @@ const PostTrendsDay = React.createClass( {

export default connect( ( state, ownProps ) => {
const siteId = getSelectedSiteId( state );
const query = {
startDate: i18n.moment().subtract( 1, 'year' ).startOf( 'month' ).format( 'YYYY-MM-DD' ),
endDate: i18n.moment().endOf( 'month' ).format( 'YYYY-MM-DD' )
};
const query = getStatsStreakQuery();

return {
postCount: getSiteStatsPostsCountByDay( state, siteId, query, ownProps.date.format( 'YYYY-MM-DD' ) ),
Expand Down
6 changes: 2 additions & 4 deletions client/my-sites/stats/post-trends/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getSelectedSiteId } from 'state/ui/selectors';
import {
isRequestingSiteStatsForQuery
} from 'state/stats/lists/selectors';
import { getStatsStreakQuery } from 'state/stats/lists/utils';

const PostTrends = React.createClass( {

Expand Down Expand Up @@ -176,10 +177,7 @@ const PostTrends = React.createClass( {

export default connect( ( state ) => {
const siteId = getSelectedSiteId( state );
const query = {
startDate: i18n.moment().subtract( 1, 'year' ).startOf( 'month' ).format( 'YYYY-MM-DD' ),
endDate: i18n.moment().endOf( 'month' ).format( 'YYYY-MM-DD' )
};
const query = getStatsStreakQuery();

return {
requesting: isRequestingSiteStatsForQuery( state, siteId, 'statsStreak', query ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Not using this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doh good catch. Probably should rip out streakList from being passed in as a prop too!

Expand Down
29 changes: 28 additions & 1 deletion client/state/stats/lists/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
* External dependencies
*/
import { expect } from 'chai';
import i18n from 'i18n-calypso';

/**
* Internal dependencies
*/
import {
getSerializedStatsQuery
getSerializedStatsQuery,
getStatsStreakQuery
} from '../utils';

describe( 'utils', () => {
Expand Down Expand Up @@ -35,4 +37,29 @@ describe( 'utils', () => {
expect( serializedQuery ).to.eql( serializedQueryTwo );
} );
} );

describe( 'getStatsStreakQuery()', () => {
it( 'should return the correct default query', () => {
const streakQuery = getStatsStreakQuery( {} );
Copy link
Contributor

Choose a reason for hiding this comment

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

Could omit the {} here so that we test the default parameter value.


expect( streakQuery ).to.eql( {
startDate: i18n.moment().subtract( 1, 'year' ).startOf( 'month' ).format( 'YYYY-MM-DD' ),
endDate: i18n.moment().endOf( 'month' ).format( 'YYYY-MM-DD' ),
max: 3000
} );
} );

it( 'should allow defaults to be over-ridden', () => {
const streakQuery = getStatsStreakQuery( {
startDate: '1999-12-31',
endDate: 'out-of-time'
} );

expect( streakQuery ).to.eql( {
startDate: '1999-12-31',
endDate: 'out-of-time',
max: 3000
} );
} );
} );
} );
17 changes: 17 additions & 0 deletions client/state/stats/lists/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import sortBy from 'lodash/sortBy';
import toPairs from 'lodash/toPairs';
import i18n from 'i18n-calypso';

/**
* Returns a serialized stats query, used as the key in the
Expand All @@ -14,3 +15,19 @@ import toPairs from 'lodash/toPairs';
export function getSerializedStatsQuery( query = {} ) {
return JSON.stringify( sortBy( toPairs( query ), ( pair ) => pair[ 0 ] ) );
}

/**
* Returns a standard query for use with the `statsStreak` endpoint
*
* @param {Object} query Query options
* @return {String} Serialized stats query
*/
export function getStatsStreakQuery( query = {} ) {
const defaultQuery = {
startDate: i18n.moment().subtract( 1, 'year' ).startOf( 'month' ).format( 'YYYY-MM-DD' ),
endDate: i18n.moment().endOf( 'month' ).format( 'YYYY-MM-DD' ),
max: 3000
};

return Object.assign( {}, defaultQuery, query );
}