| 1 |
import { createContext, useEffect, useState } from 'react'; |
| 2 |
import apiFetch from '@wordpress/api-fetch'; |
| 3 |
import { __ } from '@wordpress/i18n'; |
| 4 |
|
| 5 |
export const AdminContext = createContext(); |
| 6 |
|
| 7 |
export const AdminProvider = ( { children } ) => { |
| 8 |
const [ isLoading, setIsLoading ] = React.useState( true ); |
| 9 |
const [ onboardingSettings, setOnboardingSettings ] = React.useState( {} ); |
| 10 |
const [ elementorKitSettings, setElementorKitSettings ] = React.useState( {} ); |
| 11 |
const [ stepAction, setStepAction ] = useState( '' ); |
| 12 |
const [ step, setStep ] = useState( 0 ); |
| 13 |
const [ buttonText, setButtonText ] = useState( '' ); |
| 14 |
const { elementorInstalled, elementorActive, wizardCompleted } = onboardingSettings; |
| 15 |
const { elementorAppConfig } = window; |
| 16 |
|
| 17 |
useEffect( () => { |
| 18 |
if ( elementorAppConfig ) { |
| 19 |
setElementorKitSettings( elementorAppConfig[ 'kit-library' ] ); |
| 20 |
} |
| 21 |
}, [ elementorAppConfig ] ); |
| 22 |
|
| 23 |
useEffect( () => { |
| 24 |
if ( wizardCompleted ) { |
| 25 |
setStep( 2 ); |
| 26 |
return; |
| 27 |
} |
| 28 |
if ( false === elementorInstalled ) { |
| 29 |
setStepAction( 'install-elementor' ); |
| 30 |
setButtonText( __( 'Start building my website', 'hello-plus' ) ); |
| 31 |
} |
| 32 |
if ( elementorInstalled && false === elementorActive ) { |
| 33 |
setStepAction( 'activate-elementor' ); |
| 34 |
setButtonText( __( 'Start building my website', 'hello-plus' ) ); |
| 35 |
} |
| 36 |
if ( elementorInstalled && elementorActive ) { |
| 37 |
setStepAction( 'install-kit' ); |
| 38 |
setButtonText( __( 'Install Kit', 'hello-plus' ) ); |
| 39 |
setStep( 1 ); |
| 40 |
} |
| 41 |
}, [ elementorInstalled, elementorActive, wizardCompleted ] ); |
| 42 |
|
| 43 |
useEffect( () => { |
| 44 |
Promise.all( [ |
| 45 |
apiFetch( { path: '/elementor-hello-plus/v1/onboarding-settings' } ), |
| 46 |
] ).then( ( [ onboarding ] ) => { |
| 47 |
setOnboardingSettings( onboarding.settings ); |
| 48 |
} ).finally( () => { |
| 49 |
setIsLoading( false ); |
| 50 |
} ); |
| 51 |
}, [] ); |
| 52 |
|
| 53 |
return ( |
| 54 |
<AdminContext.Provider value={ { |
| 55 |
onboardingSettings, |
| 56 |
stepAction, |
| 57 |
setStepAction, |
| 58 |
buttonText, |
| 59 |
step, |
| 60 |
setStep, |
| 61 |
isLoading, |
| 62 |
elementorKitSettings, |
| 63 |
setIsLoading, |
| 64 |
} }> |
| 65 |
{ children } |
| 66 |
</AdminContext.Provider> |
| 67 |
); |
| 68 |
}; |
| 69 |
|