Merge pull request woocommerce/woocommerce-admin#1006 from woocommerce/fix/856-typeerror-interval-over-100-days

Use `wc-api` for Reports.
This commit is contained in:
Jeff Stieler 2018-12-10 08:26:42 -07:00 committed by GitHub
commit 4cd01a3b3b
15 changed files with 325 additions and 59 deletions

View File

@ -5,7 +5,6 @@
import { Component } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { format as formatDate } from '@wordpress/date';
import { withSelect } from '@wordpress/data';
import PropTypes from 'prop-types';
import { find, get } from 'lodash';
@ -28,6 +27,7 @@ import { Chart } from '@woocommerce/components';
*/
import { getReportChartData, getTooltipValueFormat } from 'store/reports/utils';
import ReportError from 'analytics/components/report-error';
import withSelect from 'wc-api/with-select';
export const DEFAULT_FILTER = 'all';

View File

@ -5,7 +5,6 @@
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import PropTypes from 'prop-types';
/**
@ -21,6 +20,7 @@ import { SummaryList, SummaryListPlaceholder, SummaryNumber } from '@woocommerce
import { getSummaryNumbers } from 'store/reports/utils';
import ReportError from 'analytics/components/report-error';
import { calculateDelta, formatValue } from './utils';
import withSelect from 'wc-api/with-select';
export class ReportSummary extends Component {
render() {

View File

@ -5,7 +5,6 @@
import { applyFilters } from '@wordpress/hooks';
import { Component } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import { get, orderBy } from 'lodash';
import PropTypes from 'prop-types';
@ -20,6 +19,7 @@ import { onQueryChange } from '@woocommerce/navigation';
*/
import ReportError from 'analytics/components/report-error';
import { getReportChartData, getReportTableData } from 'store/reports/utils';
import withSelect from 'wc-api/with-select';
const TABLE_FILTER = 'woocommerce_admin_report_table';

View File

@ -224,7 +224,7 @@ export default compose(
withSelect( ( select, props ) => {
const { query } = props;
const datesFromQuery = getCurrentDates( query );
const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-admin' );
const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-api' );
// TODO Support hour here when viewing a single day
const tableQuery = {

View File

@ -62,29 +62,29 @@ describe( 'getReportChartData()', () => {
};
beforeAll( () => {
select( 'wc-admin' ).getReportStats = jest.fn().mockReturnValue( {} );
select( 'wc-admin' ).isReportStatsRequesting = jest.fn().mockReturnValue( false );
select( 'wc-admin' ).isReportStatsError = jest.fn().mockReturnValue( false );
select( 'wc-api' ).getReportStats = jest.fn().mockReturnValue( {} );
select( 'wc-api' ).isReportStatsRequesting = jest.fn().mockReturnValue( false );
select( 'wc-api' ).isReportStatsError = jest.fn().mockReturnValue( false );
} );
afterAll( () => {
select( 'wc-admin' ).getReportStats.mockRestore();
select( 'wc-admin' ).isReportStatsRequesting.mockRestore();
select( 'wc-admin' ).isReportStatsError.mockRestore();
select( 'wc-api' ).getReportStats.mockRestore();
select( 'wc-api' ).isReportStatsRequesting.mockRestore();
select( 'wc-api' ).isReportStatsError.mockRestore();
} );
function setGetReportStats( func ) {
select( 'wc-admin' ).getReportStats.mockImplementation( ( ...args ) => func( ...args ) );
select( 'wc-api' ).getReportStats.mockImplementation( ( ...args ) => func( ...args ) );
}
function setIsReportStatsRequesting( func ) {
select( 'wc-admin' ).isReportStatsRequesting.mockImplementation( ( ...args ) =>
select( 'wc-api' ).isReportStatsRequesting.mockImplementation( ( ...args ) =>
func( ...args )
);
}
function setIsReportStatsError( func ) {
select( 'wc-admin' ).isReportStatsError.mockImplementation( ( ...args ) => func( ...args ) );
select( 'wc-api' ).isReportStatsError.mockImplementation( ( ...args ) => func( ...args ) );
}
it( 'returns isRequesting if first request is in progress', () => {
@ -262,29 +262,29 @@ describe( 'getSummaryNumbers()', () => {
};
beforeAll( () => {
select( 'wc-admin' ).getReportStats = jest.fn().mockReturnValue( {} );
select( 'wc-admin' ).isReportStatsRequesting = jest.fn().mockReturnValue( false );
select( 'wc-admin' ).isReportStatsError = jest.fn().mockReturnValue( false );
select( 'wc-api' ).getReportStats = jest.fn().mockReturnValue( {} );
select( 'wc-api' ).isReportStatsRequesting = jest.fn().mockReturnValue( false );
select( 'wc-api' ).isReportStatsError = jest.fn().mockReturnValue( false );
} );
afterAll( () => {
select( 'wc-admin' ).getReportStats.mockRestore();
select( 'wc-admin' ).isReportStatsRequesting.mockRestore();
select( 'wc-admin' ).isReportStatsError.mockRestore();
select( 'wc-api' ).getReportStats.mockRestore();
select( 'wc-api' ).isReportStatsRequesting.mockRestore();
select( 'wc-api' ).isReportStatsError.mockRestore();
} );
function setGetReportStats( func ) {
select( 'wc-admin' ).getReportStats.mockImplementation( ( ...args ) => func( ...args ) );
select( 'wc-api' ).getReportStats.mockImplementation( ( ...args ) => func( ...args ) );
}
function setIsReportStatsRequesting( func ) {
select( 'wc-admin' ).isReportStatsRequesting.mockImplementation( ( ...args ) =>
select( 'wc-api' ).isReportStatsRequesting.mockImplementation( ( ...args ) =>
func( ...args )
);
}
function setIsReportStatsError( func ) {
select( 'wc-admin' ).isReportStatsError.mockImplementation( ( ...args ) => func( ...args ) );
select( 'wc-api' ).isReportStatsError.mockImplementation( ( ...args ) => func( ...args ) );
}
it( 'returns isRequesting if a request is in progress', () => {
@ -460,58 +460,58 @@ describe( 'getReportTableData()', () => {
};
beforeAll( () => {
select( 'wc-admin' ).getReportItems = jest.fn().mockReturnValue( {} );
select( 'wc-admin' ).isGetReportItemsRequesting = jest.fn().mockReturnValue( false );
select( 'wc-admin' ).isGetReportItemsError = jest.fn().mockReturnValue( false );
select( 'wc-api' ).getReportItems = jest.fn().mockReturnValue( {} );
select( 'wc-api' ).isReportItemsRequesting = jest.fn().mockReturnValue( false );
select( 'wc-api' ).isReportItemsError = jest.fn().mockReturnValue( false );
} );
afterAll( () => {
select( 'wc-admin' ).getReportItems.mockRestore();
select( 'wc-admin' ).isGetReportItemsRequesting.mockRestore();
select( 'wc-admin' ).isGetReportItemsError.mockRestore();
select( 'wc-api' ).getReportItems.mockRestore();
select( 'wc-api' ).isReportItemsRequesting.mockRestore();
select( 'wc-api' ).isReportItemsError.mockRestore();
} );
function setGetReportItems( func ) {
select( 'wc-admin' ).getReportItems.mockImplementation( ( ...args ) => func( ...args ) );
select( 'wc-api' ).getReportItems.mockImplementation( ( ...args ) => func( ...args ) );
}
function setIsGetReportItemsRequesting( func ) {
select( 'wc-admin' ).isGetReportItemsRequesting.mockImplementation( ( ...args ) =>
function setisReportItemsRequesting( func ) {
select( 'wc-api' ).isReportItemsRequesting.mockImplementation( ( ...args ) =>
func( ...args )
);
}
function setIsGetReportItemsError( func ) {
select( 'wc-admin' ).isGetReportItemsError.mockImplementation( ( ...args ) => func( ...args ) );
function setisReportItemsError( func ) {
select( 'wc-api' ).isReportItemsError.mockImplementation( ( ...args ) => func( ...args ) );
}
it( 'returns isRequesting if a request is in progress', () => {
setIsGetReportItemsRequesting( () => true );
setisReportItemsRequesting( () => true );
const result = getReportTableData( 'coupons', query, select );
expect( result ).toEqual( { ...response, query, isRequesting: true } );
expect( select( 'wc-admin' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query );
expect( select( 'wc-admin' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith(
expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query );
expect( select( 'wc-api' ).isReportItemsRequesting ).toHaveBeenLastCalledWith(
'coupons',
query
);
expect( select( 'wc-admin' ).isGetReportItemsError ).toHaveBeenCalledTimes( 0 );
expect( select( 'wc-api' ).isReportItemsError ).toHaveBeenCalledTimes( 0 );
} );
it( 'returns isError if request errors', () => {
setIsGetReportItemsRequesting( () => false );
setIsGetReportItemsError( () => true );
setisReportItemsRequesting( () => false );
setisReportItemsError( () => true );
const result = getReportTableData( 'coupons', query, select );
expect( result ).toEqual( { ...response, query, isError: true } );
expect( select( 'wc-admin' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query );
expect( select( 'wc-admin' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith(
expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query );
expect( select( 'wc-api' ).isReportItemsRequesting ).toHaveBeenLastCalledWith(
'coupons',
query
);
expect( select( 'wc-admin' ).isGetReportItemsError ).toHaveBeenLastCalledWith(
expect( select( 'wc-api' ).isReportItemsError ).toHaveBeenLastCalledWith(
'coupons',
query
);
@ -519,19 +519,19 @@ describe( 'getReportTableData()', () => {
it( 'returns results after queries finish', () => {
const items = [ { id: 1 }, { id: 2 }, { id: 3 } ];
setIsGetReportItemsRequesting( () => false );
setIsGetReportItemsError( () => false );
setisReportItemsRequesting( () => false );
setisReportItemsError( () => false );
setGetReportItems( () => items );
const result = getReportTableData( 'coupons', query, select );
expect( result ).toEqual( { ...response, query, items } );
expect( select( 'wc-admin' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query );
expect( select( 'wc-admin' ).isGetReportItemsRequesting ).toHaveBeenLastCalledWith(
expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query );
expect( select( 'wc-api' ).isReportItemsRequesting ).toHaveBeenLastCalledWith(
'coupons',
query
);
expect( select( 'wc-admin' ).isGetReportItemsError ).toHaveBeenLastCalledWith(
expect( select( 'wc-api' ).isReportItemsError ).toHaveBeenLastCalledWith(
'coupons',
query
);

View File

@ -139,7 +139,7 @@ function getRequestQuery( endpoint, dataType, query ) {
* @return {Object} Object containing summary number responses.
*/
export function getSummaryNumbers( endpoint, query, select ) {
const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-admin' );
const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-api' );
const response = {
isRequesting: false,
isError: false,
@ -182,7 +182,7 @@ export function getSummaryNumbers( endpoint, query, select ) {
* @return {Object} Object containing API request information (response, fetching, and error details)
*/
export function getReportChartData( endpoint, dataType, query, select ) {
const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-admin' );
const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-api' );
const response = {
isEmpty: false,
@ -296,8 +296,8 @@ export function getReportTableQuery( urlQuery, query ) {
* @return {Object} Object Table data response
*/
export function getReportTableData( endpoint, urlQuery, select, query = {} ) {
const { getReportItems, isGetReportItemsRequesting, isGetReportItemsError } = select(
'wc-admin'
const { getReportItems, isReportItemsRequesting, isReportItemsError } = select(
'wc-api'
);
const tableQuery = reportsUtils.getReportTableQuery( urlQuery, query );
@ -309,9 +309,9 @@ export function getReportTableData( endpoint, urlQuery, select, query = {} ) {
};
const items = getReportItems( endpoint, tableQuery );
if ( isGetReportItemsRequesting( endpoint, tableQuery ) ) {
if ( isReportItemsRequesting( endpoint, tableQuery ) ) {
return { ...response, isRequesting: true };
} else if ( isGetReportItemsError( endpoint, tableQuery ) ) {
} else if ( isReportItemsError( endpoint, tableQuery ) ) {
return { ...response, isError: true };
}

View File

@ -33,11 +33,7 @@ const isGetOrdersRequesting = getResource => ( query = {} ) => {
const resourceName = getResourceName( 'order-query', query );
const { lastRequested, lastReceived } = getResource( resourceName );
if ( isNil( lastRequested ) ) {
return false;
}
if ( isNil( lastReceived ) ) {
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
return true;
}

View File

@ -0,0 +1,11 @@
/** @format */
/**
* Internal dependencies
*/
import operations from './operations';
import selectors from './selectors';
export default {
operations,
selectors,
};

View File

@ -0,0 +1,74 @@
/** @format */
/**
* External dependencies
*/
import apiFetch from '@wordpress/api-fetch';
/**
* WooCommerce dependencies
*/
import { stringifyQuery } from '@woocommerce/navigation';
/**
* Internal dependencies
*/
import { getResourceIdentifier, getResourcePrefix } from '../../utils';
import { NAMESPACE } from '../../constants';
import { SWAGGERNAMESPACE } from 'store/constants';
// TODO: Remove once swagger endpoints are phased out.
const swaggerEndpoints = [ 'categories', 'coupons', 'taxes' ];
const typeEndpointMap = {
'report-items-query-orders': 'orders',
'report-items-query-revenue': 'revenue',
'report-items-query-products': 'products',
'report-items-query-categories': 'categories',
'report-items-query-coupons': 'coupons',
'report-items-query-taxes': 'taxes',
'report-items-query-variations': 'variations',
};
function read( resourceNames, fetch = apiFetch ) {
const filteredNames = resourceNames.filter( name => {
const prefix = getResourcePrefix( name );
return Boolean( typeEndpointMap[ prefix ] );
} );
return filteredNames.map( async resourceName => {
const prefix = getResourcePrefix( resourceName );
const endpoint = typeEndpointMap[ prefix ];
const query = getResourceIdentifier( resourceName );
const fetchArgs = {
parse: false,
};
if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) {
fetchArgs.url = SWAGGERNAMESPACE + 'reports/' + endpoint + stringifyQuery( query );
} else {
fetchArgs.path = NAMESPACE + '/reports/' + endpoint + stringifyQuery( query );
}
try {
const response = await fetch( fetchArgs );
const report = await response.json();
const totalResults = parseInt( response.headers.get( 'x-wp-total' ) );
const totalPages = parseInt( response.headers.get( 'x-wp-totalpages' ) );
return {
[ resourceName ]: {
data: report,
totalResults,
totalPages,
},
};
} catch ( error ) {
return { [ resourceName ]: { error } };
}
} );
}
export default {
read,
};

View File

@ -0,0 +1,43 @@
/** @format */
/**
* External dependencies
*/
import { isNil } from 'lodash';
/**
* Internal dependencies
*/
import { getResourceName } from '../../utils';
import { DEFAULT_REQUIREMENT } from '../../constants';
const getReportItems = ( getResource, requireResource ) => (
type,
query = {},
requirement = DEFAULT_REQUIREMENT
) => {
const resourceName = getResourceName( `report-items-query-${ type }`, query );
return requireResource( requirement, resourceName ) || {};
};
const isReportItemsRequesting = getResource => ( type, query = {} ) => {
const resourceName = getResourceName( `report-items-query-${ type }`, query );
const { lastRequested, lastReceived } = getResource( resourceName );
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
return true;
}
return lastRequested > lastReceived;
};
const isReportItemsError = getResource => ( type, query = {} ) => {
const resourceName = getResourceName( `report-items-query-${ type }`, query );
return getResource( resourceName ).error;
};
export default {
getReportItems,
isReportItemsRequesting,
isReportItemsError,
};

View File

@ -0,0 +1,11 @@
/** @format */
/**
* Internal dependencies
*/
import operations from './operations';
import selectors from './selectors';
export default {
operations,
selectors,
};

View File

@ -0,0 +1,76 @@
/** @format */
/**
* External dependencies
*/
import apiFetch from '@wordpress/api-fetch';
/**
* WooCommerce dependencies
*/
import { stringifyQuery } from '@woocommerce/navigation';
/**
* Internal dependencies
*/
import { getResourceIdentifier, getResourcePrefix } from '../../utils';
import { NAMESPACE } from '../../constants';
import { SWAGGERNAMESPACE } from 'store/constants';
const statEndpoints = [ 'orders', 'revenue', 'products' ];
// TODO: Remove once swagger endpoints are phased out.
const swaggerEndpoints = [ 'categories', 'coupons', 'taxes' ];
const typeEndpointMap = {
'report-stats-query-orders': 'orders',
'report-stats-query-revenue': 'revenue',
'report-stats-query-products': 'products',
'report-stats-query-categories': 'categories',
'report-stats-query-coupons': 'coupons',
'report-stats-query-taxes': 'taxes',
};
function read( resourceNames, fetch = apiFetch ) {
const filteredNames = resourceNames.filter( name => {
const prefix = getResourcePrefix( name );
return Boolean( typeEndpointMap[ prefix ] );
} );
return filteredNames.map( async resourceName => {
const prefix = getResourcePrefix( resourceName );
const endpoint = typeEndpointMap[ prefix ];
const query = getResourceIdentifier( resourceName );
const fetchArgs = {
parse: false,
};
if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) {
fetchArgs.url = SWAGGERNAMESPACE + 'reports/' + endpoint + '/stats' + stringifyQuery( query );
} else if ( statEndpoints.indexOf( endpoint ) >= 0 ) {
fetchArgs.path = NAMESPACE + '/reports/' + endpoint + '/stats' + stringifyQuery( query );
} else {
fetchArgs.path = endpoint + stringifyQuery( query );
}
try {
const response = await fetch( fetchArgs );
const report = await response.json();
const totalResults = parseInt( response.headers.get( 'x-wp-total' ) );
const totalPages = parseInt( response.headers.get( 'x-wp-totalpages' ) );
return {
[ resourceName ]: {
data: report,
totalResults,
totalPages,
},
};
} catch ( error ) {
return { [ resourceName ]: { error } };
}
} );
}
export default {
read,
};

View File

@ -0,0 +1,45 @@
/** @format */
/**
* External dependencies
*/
import { isNil } from 'lodash';
/**
* Internal dependencies
*/
import { getResourceName } from '../../utils';
import { DEFAULT_REQUIREMENT } from '../../constants';
const getReportStats = ( getResource, requireResource ) => (
type,
query = {},
requirement = DEFAULT_REQUIREMENT
) => {
const resourceName = getResourceName( `report-stats-query-${ type }`, query );
const data = requireResource( requirement, resourceName ) || {};
return data;
};
const isReportStatsRequesting = getResource => ( type, query = {} ) => {
const resourceName = getResourceName( `report-stats-query-${ type }`, query );
const { lastRequested, lastReceived } = getResource( resourceName );
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
return true;
}
return lastRequested > lastReceived;
};
const isReportStatsError = getResource => ( type, query = {} ) => {
const resourceName = getResourceName( `report-stats-query-${ type }`, query );
return getResource( resourceName ).error;
};
export default {
getReportStats,
isReportStatsRequesting,
isReportStatsError,
};

View File

@ -5,8 +5,12 @@ export function getResourceName( prefix, identifier ) {
return `${ prefix }:${ identifierString }`;
}
export function getResourcePrefix( resourceName ) {
return resourceName.substring( 0, resourceName.indexOf( ':' ) );
}
export function isResourcePrefix( resourceName, prefix ) {
const resourcePrefix = resourceName.substring( 0, resourceName.indexOf( ':' ) );
const resourcePrefix = getResourcePrefix( resourceName );
return resourcePrefix === prefix;
}

View File

@ -5,18 +5,24 @@
*/
import notes from './notes';
import orders from './orders';
import reportItems from './reports/items';
import reportStats from './reports/stats';
function createWcApiSpec() {
return {
selectors: {
...notes.selectors,
...orders.selectors,
...reportItems.selectors,
...reportStats.selectors,
},
operations: {
read( resourceNames ) {
return [
...notes.operations.read( resourceNames ),
...orders.operations.read( resourceNames ),
...reportItems.operations.read( resourceNames ),
...reportStats.operations.read( resourceNames ),
];
},
},