File "setInitialState.js"

Full Path: /home/fresvfqn/waterdamagerestorationandrepairsmithtown.com/wp-content/plugins/surerank/src/store/setInitialState.js
File size: 1.12 KB
MIME-type: text/x-java
Charset: utf-8

import apiFetch from '@wordpress/api-fetch';
import { dispatch } from '@wordpress/data';
import { addQueryArgs } from '@wordpress/url';
import { store } from './store';
import { EDITOR_URL } from '@Global/constants/api';

const setInitialState = () => {
	const queryParams = {};
	// If post page.
	if ( window?.surerank_globals?.post_id ) {
		queryParams.post_id = window?.surerank_globals.post_id;
	}
	// If term page.
	if ( window?.surerank_globals?.term_id ) {
		queryParams.term_id = window?.surerank_globals.term_id;
	}

	if ( window?.surerank_seo_popup?.post_id ) {
		queryParams.post_id = window?.surerank_seo_popup.post_id;
	}
	// If term page.
	if ( window?.surerank_seo_popup?.term_id ) {
		queryParams.term_id = window?.surerank_seo_popup.term_id;
	}

	apiFetch( {
		path: addQueryArgs( EDITOR_URL, queryParams ),
	} ).then( ( response ) => {
		if ( response?.success ) {
			let initialState = {
				variables: response.variables,
			};

			if ( response.other ) {
				initialState = { ...initialState, ...response.other };
			}

			dispatch( store ).updateInitialState( initialState );
		}
	} );
};

export default setInitialState;