From e77dc31561aad8675d7e524b7230472a1ebba17b Mon Sep 17 00:00:00 2001 From: Allen Benny Date: Fri, 24 Nov 2023 18:35:35 +0530 Subject: [PATCH] Get data from Backend --- includes/RestApi/SiteGenController.php | 18 ++++++++++++++++++ .../NewfoldInterfaceSkeleton/SiteGen/index.js | 19 +++++++------------ src/OnboardingSPA/utils/api/siteGen.js | 8 ++++++++ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/includes/RestApi/SiteGenController.php b/includes/RestApi/SiteGenController.php index 61e99f664..f83eed0b2 100644 --- a/includes/RestApi/SiteGenController.php +++ b/includes/RestApi/SiteGenController.php @@ -30,6 +30,15 @@ class SiteGenController { * @return void */ public function register_routes() { + \register_rest_route( + $this->namespace, + $this->rest_base . '/get-identifiers', + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_valid_identifiers' ), + 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), + ) + ); \register_rest_route( $this->namespace, $this->rest_base . '/generate', @@ -64,6 +73,15 @@ public function sitegen_meta_args() { ); } + /** + * Gets all the valid Identifiers + * + * @return array + */ + public function get_valid_identifiers() { + return array_keys( array_filter( SiteGenService::get_identifiers() ) ); + } + /** * Generate Sitegen meta data. * diff --git a/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js b/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js index ab881654e..0e16ea38d 100644 --- a/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js +++ b/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js @@ -11,7 +11,10 @@ import ToggleDarkMode from '../../ToggleDarkMode'; import { ThemeProvider } from '../../ThemeContextProvider'; import { store as nfdOnboardingStore } from '../../../store'; import { setFlow } from '../../../utils/api/flow'; -import { generateSiteGenMeta } from '../../../utils/api/siteGen'; +import { + generateSiteGenMeta, + getSiteGenIdentifiers, +} from '../../../utils/api/siteGen'; // Wrapping the NewfoldInterfaceSkeleton with the HOC to make theme available const ThemedNewfoldInterfaceSkeleton = themeToggleHOC( @@ -39,7 +42,7 @@ const SiteGen = () => { } } - function generateSiteGenData() { + async function generateSiteGenData() { // Start the API Requests when the loader is shown. if ( ! ( @@ -58,16 +61,8 @@ const SiteGen = () => { return; } - let identifiers = [ - 'siteclassification', - 'targetaudience', - 'contenttones', - 'contentstructure', - 'colorpalette', - 'sitemap', - 'pluginrecommendation', - 'fontpair', - ]; + let identifiers = await getSiteGenIdentifiers(); + identifiers = identifiers.body; const midIndex = Math.floor( identifiers.length / 2 ); if ( location.pathname.includes( 'experience' ) ) { diff --git a/src/OnboardingSPA/utils/api/siteGen.js b/src/OnboardingSPA/utils/api/siteGen.js index 6f150705c..b41fdc75f 100644 --- a/src/OnboardingSPA/utils/api/siteGen.js +++ b/src/OnboardingSPA/utils/api/siteGen.js @@ -3,6 +3,14 @@ import apiFetch from '@wordpress/api-fetch'; import { resolve } from './resolve.js'; import { onboardingRestURL } from './common'; +export async function getSiteGenIdentifiers() { + return await resolve( + apiFetch( { + url: onboardingRestURL( 'sitegen/get-identifiers' ), + } ).then() + ); +} + export async function generateSiteGenMeta( siteInfo, identifier,