File size: 1,702 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import PropTypes from 'prop-types';
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { requestSitePost } from 'calypso/state/posts/actions';
import {
	requestEmailPeriodStats,
	requestEmailAlltimeStats,
} from 'calypso/state/stats/emails/actions';

const requestPeriodStats = ( siteId, postId, period, date, statType, quantity ) => ( dispatch ) => {
	dispatch( requestEmailPeriodStats( siteId, postId, period, statType, date, quantity ) );
};

const requestAlltimeStats = ( siteId, postId, statType, quantity ) => ( dispatch ) => {
	dispatch( requestEmailAlltimeStats( siteId, postId, statType, quantity ) );
};

function QueryEmailStats( { siteId, postId, period, date, quantity, hasValidDate, statType } ) {
	const dispatch = useDispatch();

	useEffect( () => {
		dispatch( requestSitePost( siteId, postId ) );
	}, [ dispatch, siteId, postId ] );

	useEffect( () => {
		if ( siteId && postId > -1 ) {
			dispatch( requestAlltimeStats( siteId, postId, statType ) );
		}
	}, [ dispatch, siteId, postId, statType ] );

	useEffect( () => {
		// if hasValidatedDate is false, the date was not set we don't have a post publish date yet
		if ( siteId && postId > -1 && hasValidDate ) {
			dispatch( requestPeriodStats( siteId, postId, period, date, statType, quantity ) );
		}
	}, [ dispatch, siteId, postId, hasValidDate, period, date, statType, quantity ] );

	return null;
}

QueryEmailStats.propTypes = {
	siteId: PropTypes.number,
	postId: PropTypes.number,
	period: PropTypes.string,
	statType: PropTypes.string,
	date: PropTypes.string,
	quantity: PropTypes.number,
	hasValidDate: PropTypes.bool,
	isRequesting: PropTypes.bool,
};

export default QueryEmailStats;