-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathSetupAccount.js
138 lines (119 loc) · 3.89 KB
/
SetupAccount.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* AdSense SetupAccount component.
*
* Site Kit by Google, Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import PropTypes from 'prop-types';
/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
/**
* Internal dependencies
*/
import Data from 'googlesitekit-data';
import {
MODULES_ADSENSE,
API_STATE_NEEDS_ATTENTION,
API_STATE_REQUIRES_REVIEW,
API_STATE_GETTING_READY,
} from '../../../datastore/constants';
import {
ACCOUNT_STATUS_NO_CLIENT,
ACCOUNT_STATUS_NEEDS_ATTENTION,
ACCOUNT_STATUS_READY,
ACCOUNT_STATUS_CLIENT_GETTING_READY,
ACCOUNT_STATUS_CLIENT_REQUIRES_REVIEW,
SITE_STATUS_NONE,
} from '../../../util/status';
import ProgressBar from '../../../../../components/ProgressBar';
import SetupAccountSite from './SetupAccountSite';
import SetupAccountNoClient from './SetupAccountNoClient';
import SetupAccountCreateSite from './SetupAccountCreateSite';
import SetupAccountPendingTasks from './SetupAccountPendingTasks';
const { useSelect, useDispatch } = Data;
export default function SetupAccount( { account, finishSetup } ) {
const { _id: accountID, state: accountState } = account;
const clientID = useSelect( ( select ) =>
select( MODULES_ADSENSE ).getClientID()
);
const clients = useSelect( ( select ) =>
select( MODULES_ADSENSE ).getClients( accountID )
);
const site = useSelect( ( select ) =>
select( MODULES_ADSENSE ).getCurrentSite( accountID )
);
const afcClient = useSelect( ( select ) =>
select( MODULES_ADSENSE ).getAFCClient( accountID )
);
const { setClientID, setAccountStatus, setSiteStatus } = useDispatch(
MODULES_ADSENSE
);
useEffect( () => {
if ( afcClient?._id && clientID !== afcClient._id ) {
setClientID( afcClient._id );
}
}, [ afcClient, clientID, setClientID ] );
useEffect( () => {
if ( site === null ) {
setSiteStatus( SITE_STATUS_NONE );
}
}, [ setSiteStatus, site ] );
useEffect( () => {
// Do nothing if clients aren't loaded because we can't determine afcClientID yet.
if ( clients === undefined || site === undefined ) {
return;
}
if ( ! clientID ) {
setAccountStatus( ACCOUNT_STATUS_NO_CLIENT );
} else if ( accountState === API_STATE_NEEDS_ATTENTION ) {
setAccountStatus( ACCOUNT_STATUS_NEEDS_ATTENTION );
} else if ( afcClient?.state === API_STATE_REQUIRES_REVIEW ) {
setAccountStatus( ACCOUNT_STATUS_CLIENT_REQUIRES_REVIEW );
} else if ( afcClient?.state === API_STATE_GETTING_READY ) {
setAccountStatus( ACCOUNT_STATUS_CLIENT_GETTING_READY );
} else {
setAccountStatus( ACCOUNT_STATUS_READY );
}
}, [ accountState, afcClient, clientID, clients, setAccountStatus, site ] );
// Show the progress bar if clients or site aren't loaded yet.
if ( clients === undefined || site === undefined ) {
return <ProgressBar />;
}
if ( ! clientID ) {
return <SetupAccountNoClient />;
}
if ( site === null ) {
return <SetupAccountCreateSite accountID={ accountID } />;
}
if (
accountState === API_STATE_NEEDS_ATTENTION ||
afcClient?.state === API_STATE_REQUIRES_REVIEW ||
afcClient?.state === API_STATE_GETTING_READY
) {
return <SetupAccountPendingTasks accountID={ accountID } />;
}
return <SetupAccountSite site={ site } finishSetup={ finishSetup } />;
}
SetupAccount.propTypes = {
account: PropTypes.shape( {
_id: PropTypes.string,
state: PropTypes.string,
} ),
finishSetup: PropTypes.func,
};