-
Notifications
You must be signed in to change notification settings - Fork 21
/
useGoogleAdsAccount.js
64 lines (55 loc) · 1.84 KB
/
useGoogleAdsAccount.js
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
54
55
56
57
58
59
60
61
62
63
64
/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import { STORE_KEY } from '.~/data/constants';
import { useAppDispatch } from '.~/data';
import { GOOGLE_ADS_ACCOUNT_STATUS } from '.~/constants';
import useGoogleAccount from './useGoogleAccount';
const googleAdsAccountSelector = 'getGoogleAdsAccount';
const useGoogleAdsAccount = () => {
const { google, isResolving, hasFinishedResolution } = useGoogleAccount();
const dispatcher = useAppDispatch();
const refetchGoogleAdsAccount = useCallback( () => {
dispatcher.invalidateResolution( googleAdsAccountSelector, [] );
}, [ dispatcher ] );
return useSelect(
( select ) => {
if ( ! google || google.active === 'no' ) {
return {
googleAdsAccount: undefined,
isResolving,
hasFinishedResolution,
};
}
const selector = select( STORE_KEY );
const acc = selector[ googleAdsAccountSelector ]();
const isResolvingGoogleAdsAccount = selector.isResolving(
googleAdsAccountSelector
);
// The "incomplete" status means there is a connected account but billing is not yet set
// so it's considered as `true`.
// The main reason for not using a naming like `isGoogleAdsConnected` here is to make
// a slight distinction from the "connected" status.
const hasGoogleAdsConnection = [
GOOGLE_ADS_ACCOUNT_STATUS.CONNECTED,
GOOGLE_ADS_ACCOUNT_STATUS.INCOMPLETE,
].includes( acc?.status );
return {
googleAdsAccount: acc,
isResolving: isResolvingGoogleAdsAccount,
refetchGoogleAdsAccount,
hasFinishedResolution: selector.hasFinishedResolution(
googleAdsAccountSelector
),
hasGoogleAdsConnection,
};
},
[ google, isResolving, hasFinishedResolution, refetchGoogleAdsAccount ]
);
};
export default useGoogleAdsAccount;