File size: 1,990 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
54
55
56
57
58
59
60
61
62
import { Store } from 'redux';
import wpcom from 'calypso/lib/wp';
import {
	SITE_REQUEST,
	SITE_REQUEST_FAILURE,
	SITE_REQUEST_SUCCESS,
	ODYSSEY_SITE_RECEIVE,
} from 'calypso/state/action-types';
import { getSite, isRequestingSite } from 'calypso/state/sites/selectors';
import { IAppState, CalypsoDispatch } from 'calypso/state/types';
import { setSelectedSiteId } from 'calypso/state/ui/actions';
import config from './config-api';
import { getApiNamespace, getApiPath } from './get-api';

/**
 * Initialize site data in the Redux store and fetch site details if needed.
 * This includes setting the selected site and fetching its data from the WordPress.com API.
 */
export async function initializeSiteData(
	store: Store< IAppState > & { dispatch: CalypsoDispatch }
): Promise< void > {
	const siteId = config( 'blog_id' );
	const dispatch = store.dispatch;
	const state = store.getState();

	dispatch( setSelectedSiteId( siteId ) );

	const isRequesting = isRequestingSite( state, siteId );
	const site = getSite( state, siteId );

	// If options stored on WPCOM exists or it's already requesting, we do not need to fetch it again.
	if ( ( site?.options && 'is_commercial' in site.options ) || isRequesting ) {
		return;
	}

	dispatch( { type: SITE_REQUEST, siteId: siteId } );

	try {
		const data = await wpcom.req.get(
			{
				path: getApiPath( '/site', { siteId } ),
				apiNamespace: getApiNamespace(),
			},
			{
				// Only add the http_envelope flag if it's a Simple Classic site.
				http_envelope: ! config.isEnabled( 'is_running_in_jetpack_site' ),
			}
		);

		// For Jetpack/Atomic sites, data format is { data: JSON string of SiteDetails }
		const siteData =
			config.isEnabled( 'is_running_in_jetpack_site' ) && 'data' in data
				? JSON.parse( data.data )
				: data;

		dispatch( { type: ODYSSEY_SITE_RECEIVE, site: siteData } );
		dispatch( { type: SITE_REQUEST_SUCCESS, siteId } );
	} catch ( error ) {
		dispatch( { type: SITE_REQUEST_FAILURE, siteId } );
	}
}