import { createElement, Component, useEffect, useState, useMemo, createContext, Fragment, } from '@wordpress/element' import { __ } from 'ct-i18n' import classnames from 'classnames' import useActivationAction from '../helpers/useActivationAction' import { Transition, animated } from 'blocksy-options' import DemosList from './DemoInstall/DemosList' import DemoToInstall from './DemoInstall/DemoToInstall' import useDemoListFilters from './DemoInstall/filters/useDemoListFilters' import SubmitSupport from '../helpers/SubmitSupport' import NoLicense from '../NoLicense' import { getStarterSitesStatus } from '../helpers/starter-sites' export const DemosContext = createContext({ demos: [], }) let demos_cache = null let demos_error_cache = null const DemoInstall = ({ children, path, location }) => { const [isLoading, setIsLoading] = useState(!demos_cache) const [demos_list, setDemosList] = useState(demos_cache || []) const [pluginsStatus, setPluginsStatus] = useState( ctDashboardLocalizations.plugin_data.active_plugins ) const [currentDemo, setCurrentDemo] = useState(null) const [currentlyInstalledDemo, setCurrentlyInstalledDemo] = useState( ctDashboardLocalizations.plugin_data.current_installed_demo ) const filtersPayload = useDemoListFilters({ demos_list, }) const [forceEmptyDemos, setForceEmptyDemos] = useState(false) const [demo_error, setDemoError] = useState({ isError: false, message: '', // generic | remote_fetch_failed | ajax_request_failed reason: 'generic', }) const [demoConfiguration, setDemoConfiguration] = useState({ builder: '', }) const [installerBlockingReleased, setInstallerBlockingReleased] = useState(false) const syncDemos = async (verbose = false) => { if (verbose) { setIsLoading(true) } try { let demosResponse = await getStarterSitesStatus() if (demosResponse.status && demosResponse.status === 511) { // This is a special case where the license is invalid setForceEmptyDemos(true) return } const data = await demosResponse.json() setDemosList(data) demos_cache = data } catch (response) { // Usually this is a network error or some policty blocking // external frontend requests console.error('Blocksy:Dashboard:DemoInstall:demos_list', { response, reason: 'frontend_starter_sites_fetch_failed', }) } setIsLoading(false) } useEffect(() => { if (ctDashboardLocalizations.plugin_data.demo_install_error) { setIsLoading(false) setDemoError({ isError: true, message: ctDashboardLocalizations.plugin_data.demo_install_error, reason: 'generic', }) } else { syncDemos(!demos_cache) } }, []) if (forceEmptyDemos) { return } return (
{ return phase === 'leave' ? { duration: 300, } : { duration: 300, } }}> {(props, isLoading) => { if (isLoading) { return ( {__( 'Loading Starter Sites...', 'blocksy-companion' )} ) } if (demo_error.isError) { return ( {demo_error.reason === 'ajax_request_failed' && (
Error code - %s', 'blocksy-companion' ), '', '', demo_error.message ), }} /> )} {demo_error.reason === 'remote_fetch_failed' && (
Error code - %s', 'blocksy-companion' ), demo_error.message ), }} /> )} {demo_error.reason === 'generic' && (
)} ) } return ( {filtersPayload.display()} ) }}
) } export default DemoInstall