|
|
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'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ( ( 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(), |
|
|
}, |
|
|
{ |
|
|
|
|
|
http_envelope: ! config.isEnabled( 'is_running_in_jetpack_site' ), |
|
|
} |
|
|
); |
|
|
|
|
|
|
|
|
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 } ); |
|
|
} |
|
|
} |
|
|
|