From c3ce698e30ef6ecb0ebbdf24bbf4d02d9611dd34 Mon Sep 17 00:00:00 2001 From: Allen Benny Date: Wed, 22 Nov 2023 11:43:29 +0530 Subject: [PATCH 1/9] Start the code :star2: --- includes/RestApi/RestApi.php | 1 + includes/RestApi/SiteGenController.php | 81 ++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 includes/RestApi/SiteGenController.php diff --git a/includes/RestApi/RestApi.php b/includes/RestApi/RestApi.php index 8c2fba99b..e3bfb7dd3 100644 --- a/includes/RestApi/RestApi.php +++ b/includes/RestApi/RestApi.php @@ -29,6 +29,7 @@ final class RestApi { 'NewfoldLabs\WP\\Module\\Onboarding\\RestApi\\Themes\\ThemeFontsController', 'NewfoldLabs\WP\\Module\\Onboarding\\RestApi\\Themes\\ThemeColorsController', 'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\SiteClassificationController', + 'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\SiteGenController', ); /** diff --git a/includes/RestApi/SiteGenController.php b/includes/RestApi/SiteGenController.php new file mode 100644 index 000000000..fffe25c16 --- /dev/null +++ b/includes/RestApi/SiteGenController.php @@ -0,0 +1,81 @@ +namespace, + $this->rest_base . '/generate', + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'generate_sitegen_meta' ), + 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), + 'args' => $this->sitegen_meta_args(), + ) + ); + } + + /** + * Required Args for Generating Site Gen Meta. + * + * @return array + */ + public function sitegen_meta_args() { + return array( + 'site_info' => array( + 'required' => true, + 'type' => 'object', + ), + 'identifier' => array( + 'required' => true, + 'type' => 'string', + ), + ); + } + + /** + * Generate Sitegen meta data. + * + * @param \WP_REST_Request $request Request model. + * + * @return array + */ + public function generate_sitegen_meta( \WP_REST_Request $request ) { + + $site_info = $request->get_param( 'site_info' ); + $identifier = $request->get_param( 'identifier' ); + + if ( SiteGenService::is_enabled() ) { + // TODO Implement the main function + } + + return false; + } +} From d6459958f6772a8327d325748b39a188a9532f86 Mon Sep 17 00:00:00 2001 From: Allen Benny Date: Wed, 22 Nov 2023 12:00:37 +0530 Subject: [PATCH 2/9] Something... --- includes/RestApi/SiteGenController.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/includes/RestApi/SiteGenController.php b/includes/RestApi/SiteGenController.php index fffe25c16..61e99f664 100644 --- a/includes/RestApi/SiteGenController.php +++ b/includes/RestApi/SiteGenController.php @@ -57,6 +57,10 @@ public function sitegen_meta_args() { 'required' => true, 'type' => 'string', ), + 'skip_cache' => array( + 'required' => false, + 'type' => 'boolean', + ), ); } @@ -65,17 +69,23 @@ public function sitegen_meta_args() { * * @param \WP_REST_Request $request Request model. * - * @return array + * @return array|WP_Error */ public function generate_sitegen_meta( \WP_REST_Request $request ) { $site_info = $request->get_param( 'site_info' ); $identifier = $request->get_param( 'identifier' ); + $skip_cache = $request->get_param( 'skip_cache' ); if ( SiteGenService::is_enabled() ) { - // TODO Implement the main function + // TODO Implement the main function and do computations if required. + return SiteGenService::instantiate_site_meta( $site_info, $identifier, $skip_cache ); } - return false; + return new \WP_Error( + 'sitegen-error', + 'SiteGen is Disabled.', + array( 'status' => 404 ) + ); } } From d645062ead31dbabd084934fbffdea36d1a7ef53 Mon Sep 17 00:00:00 2001 From: Allen Benny Date: Wed, 22 Nov 2023 12:24:25 +0530 Subject: [PATCH 3/9] React Code Dump! --- .../NewfoldInterfaceSkeleton/SiteGen/index.js | 57 ++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js b/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js index ac1433532..39471720a 100644 --- a/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js +++ b/src/OnboardingSPA/components/NewfoldInterfaceSkeleton/SiteGen/index.js @@ -1,10 +1,16 @@ -import NewfoldInterfaceSkeleton from '../index'; +import { useSelect } from '@wordpress/data'; +import { useEffect } from '@wordpress/element'; +import { useLocation } from 'react-router-dom'; + import Header from '../../Header'; import Content from '../../Content'; import Sidebar from '../../Sidebar'; +import themeToggleHOC from '../themeToggleHOC'; +import NewfoldInterfaceSkeleton from '../index'; import ToggleDarkMode from '../../ToggleDarkMode'; import { ThemeProvider } from '../../ThemeContextProvider'; -import themeToggleHOC from '../themeToggleHOC'; +import { store as nfdOnboardingStore } from '../../../store'; +import { setFlow } from '../../../utils/api/flow'; // Wrapping the NewfoldInterfaceSkeleton with the HOC to make theme available const ThemedNewfoldInterfaceSkeleton = themeToggleHOC( @@ -14,6 +20,53 @@ const ThemedNewfoldInterfaceSkeleton = themeToggleHOC( ); const SiteGen = () => { + const location = useLocation(); + + const { currentData } = useSelect( ( select ) => { + return { + currentData: + select( nfdOnboardingStore ).getCurrentOnboardingData(), + }; + } ); + + async function syncStoreToDB() { + if ( currentData ) { + //Set the Flow Data and sync store and DB + setFlow( currentData ); + } + } + + async function generateSiteGenData() { + // TODO Implement SiteGen Sync + } + + const handlePreviousStepTracking = () => { + const previousStep = window.nfdOnboarding?.previousStep; + if ( typeof previousStep !== 'object' ) { + window.nfdOnboarding.previousStep = { + path: location.pathname, + url: window.location.href, + }; + return; + } + + const previousStepPath = previousStep.path; + + if ( previousStepPath.includes( 'site-details' ) ) { + generateSiteGenData(); + } + + window.nfdOnboarding.previousStep = { + path: location.pathname, + url: window.location.href, + }; + }; + + useEffect( () => { + syncStoreToDB(); + handlePreviousStepTracking(); + }, [ location.pathname ] ); + return ( Date: Wed, 22 Nov 2023 12:27:30 +0530 Subject: [PATCH 4/9] Create siteGen.js --- src/OnboardingSPA/utils/api/siteGen.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/OnboardingSPA/utils/api/siteGen.js diff --git a/src/OnboardingSPA/utils/api/siteGen.js b/src/OnboardingSPA/utils/api/siteGen.js new file mode 100644 index 000000000..1e0b7cb7b --- /dev/null +++ b/src/OnboardingSPA/utils/api/siteGen.js @@ -0,0 +1,14 @@ +import apiFetch from '@wordpress/api-fetch'; + +import { resolve } from './resolve.js'; +import { onboardingRestURL } from './common'; + +export async function generateSiteGenMeta( data ) { + return await resolve( + apiFetch( { + url: onboardingRestURL( 'sitegen/generate' ), + method: 'POST', + data, + } ).then() + ); +} From 32084f9ffaa29b43aa173480b9f2c87f7957b42f Mon Sep 17 00:00:00 2001 From: Allen Benny Date: Fri, 24 Nov 2023 12:54:50 +0530 Subject: [PATCH 5/9] Imitate Calls and maintain state :rotating_light: --- .../Button/NextButtonSiteGen/index.js | 11 ++- .../Button/NextButtonSiteGen/stylesheet.scss | 9 +++ .../NewfoldInterfaceSkeleton/SiteGen/index.js | 70 ++++++++++++++++--- .../steps/SiteGen/SiteDetails/index.js | 4 ++ src/OnboardingSPA/utils/api/siteGen.js | 8 ++- 5 files changed, 90 insertions(+), 12 deletions(-) diff --git a/src/OnboardingSPA/components/Button/NextButtonSiteGen/index.js b/src/OnboardingSPA/components/Button/NextButtonSiteGen/index.js index 145f392ea..1bfbe9627 100644 --- a/src/OnboardingSPA/components/Button/NextButtonSiteGen/index.js +++ b/src/OnboardingSPA/components/Button/NextButtonSiteGen/index.js @@ -5,7 +5,12 @@ import { Button } from '@wordpress/components'; import { Icon, chevronRight } from '@wordpress/icons'; import { store as nfdOnboardingStore } from '../../../store'; -const NextButtonSiteGen = ( { text, className, callback = null } ) => { +const NextButtonSiteGen = ( { + text, + className, + callback = null, + disabled = false, +} ) => { const navigate = useNavigate(); const { nextStep } = useSelect( ( select ) => { return { @@ -16,9 +21,13 @@ const NextButtonSiteGen = ( { text, className, callback = null } ) => {