Skip to content

Commit

Permalink
Merge pull request #380 from newfold-labs/api-integration-preview-hom…
Browse files Browse the repository at this point in the history
…e-page

Api integration preview home page
  • Loading branch information
arunshenoy99 authored Jan 15, 2024
2 parents 814ca25 + 00afc88 commit 105a327
Show file tree
Hide file tree
Showing 40 changed files with 2,396 additions and 209 deletions.
200 changes: 200 additions & 0 deletions includes/RestApi/SiteGenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace NewfoldLabs\WP\Module\Onboarding\RestApi;

use NewfoldLabs\WP\Module\Onboarding\Permissions;
use NewfoldLabs\WP\Module\AI\SiteGen\SiteGen;
use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService;
use NewfoldLabs\WP\Module\Onboarding\Data\Options;

/**
* Class SiteGenController
Expand Down Expand Up @@ -49,6 +51,35 @@ public function register_routes() {
'args' => $this->sitegen_meta_args(),
)
);
\register_rest_route(
$this->namespace,
$this->rest_base . '/get-homepages',
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'get_homepages' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
'args' => $this->get_homepages_args(),
)
);
\register_rest_route(
$this->namespace,
$this->rest_base . '/get-homepages-regenerate',
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'get_regenerated_homepages' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
'args' => $this->get_homepages_regenerate_args(),
)
);
\register_rest_route(
$this->namespace,
$this->rest_base . '/favourites',
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'toggle_favourite_homepage' ),
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
)
);
}

/**
Expand All @@ -73,6 +104,53 @@ public function sitegen_meta_args() {
);
}

/**
* Gets the arguments for the 'get-homepages' endpoint.
*
* @return array The array of arguments.
*/
public function get_homepages_args() {
return array(
'site_description' => array(
'required' => true,
'validate_callback' => function ( $param ) {
return is_string( $param );
},
'sanitize_callback' => 'sanitize_text_field',
),
'regenerate' => array(
'required' => false,
),
// Add other parameters here as needed.
);
}

/**
* Gets the arguments for the 'get-homepages' endpoint.
*
* @return array The array of arguments.
*/
public function get_homepages_regenerate_args() {
return array(
'site_description' => array(
'required' => true,
'validate_callback' => function ( $param ) {
return is_string( $param );
},
'sanitize_callback' => 'sanitize_text_field',
),
'regenerate' => array(
'required' => false,
),
'slug' => array(
'required' => false,
),
'colorPalettes' => array(
'required' => false,
),
);
}

/**
* Gets all the valid Identifiers
*
Expand Down Expand Up @@ -105,4 +183,126 @@ public function generate_sitegen_meta( \WP_REST_Request $request ) {
// TODO Implement the main function and do computations if required.
return SiteGenService::instantiate_site_meta( $site_info, $identifier, $skip_cache );
}

/**
* Gets the preview homepages
*
* @param \WP_REST_Request $request parameter.
* @return array
*/
public function get_homepages( \WP_REST_Request $request ) {

$site_description = $request->get_param( 'site_description' );
$regenerate = $request->get_param( 'regenerate' );
$site_info = array( 'site_description' => $site_description );
// If the option exists and is not empty, return it.
$existing_homepages = get_option( Options::get_option_name( 'sitegen_homepages' ), array() );
if ( ! empty( $existing_homepages ) && ! $regenerate ) {
return new \WP_REST_Response( $existing_homepages, 200 );
}
$target_audience = SiteGenService::instantiate_site_meta( $site_info, 'target_audience' );
$content_style = SiteGenService::instantiate_site_meta( $site_info, 'content_tones' );

if ( ! $target_audience || is_wp_error( $target_audience ) ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Required data is missing.', 'wp-module-onboarding' ),
array( 'status' => 400 )
);
}
if ( ! $content_style || is_wp_error( $content_style ) ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Required data is missing.', 'wp-module-onboarding' ),
array( 'status' => 400 )
);
}

$processed_home_pages = SiteGenService::generate_homepages(
$site_description,
$content_style,
$target_audience,
$regenerate
);

if ( is_wp_error( $processed_home_pages ) ) {
return $processed_home_pages;
}

return new \WP_REST_Response( $processed_home_pages, 200 );
}

/**
* Gets the regenerated preview homepages
*
* @param \WP_REST_Request $request parameter.
* @return array
*/
public function get_regenerated_homepages( \WP_REST_Request $request ) {
$site_description = $request->get_param( 'site_description' );
$regenerate_slug = $request->get_param( 'slug' );
$regenerate_color_palattes = $request->get_param( 'colorPalettes' );
$is_favourite = $request->get_param( 'isFavourited' );
$site_info = array( 'site_description' => $site_description );
$target_audience = SiteGenService::instantiate_site_meta( $site_info, 'target_audience' );
$content_style = SiteGenService::instantiate_site_meta( $site_info, 'content_tones' );

if ( ! $target_audience || is_wp_error( $target_audience ) ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Required data is missing.', 'wp-module-onboarding' ),
array( 'status' => 400 )
);
}

if ( ! $content_style || is_wp_error( $content_style ) ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Required data is missing.', 'wp-module-onboarding' ),
array( 'status' => 400 )
);
}

if ( $is_favourite ) {
$result = SiteGenService::handle_favorite_regeneration( $regenerate_slug, $regenerate_color_palattes );
} else {
$result = SiteGenService::handle_regular_regeneration( $site_description, $content_style, $target_audience );
}

if ( null === $result ) {
return new \WP_Error(
'nfd_onboarding_error',
__( 'Error at Regenerating home pages.', 'wp-module-onboarding' ),
array(
'status' => 400,
)
);
}

return new \WP_REST_Response( $result, 200 );
}

/**
* Updates favourite status
*
* @param \WP_REST_Request $request parameter.
* @return array
*/
public function toggle_favourite_homepage( \WP_REST_Request $request ) {
$slug = $request->get_param( 'slug' );

$response = SiteGenService::toggle_favourite_homepage( $slug );

if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return new \WP_Error(
'nfd_onboarding_error',
__( 'Error at updating Favourite status', 'wp-module-onboarding' ),
array(
'status' => 404,
)
);
}
return new \WP_REST_Response( $response, 200 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

&--dark {
background-color: var(--nfd-onboarding-navigation-back-background);
width: 74px;
height: 36px;
color: var(--nfd-onboarding-primary);
border-radius: 8px;
padding: 0, 13px, 0 13px;

&:hover {
background-color: #fff;
color: #272d30;
}
}
}
16 changes: 9 additions & 7 deletions src/OnboardingSPA/components/Button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
* Common Button Component
* Different variants can be added later based on our requirements
*
* @returns Button
* @return Button
*/

const Button = ({ text, handleClick, disabled, className }) => {
const Button = ( { children, onClick, disabled, className } ) => {
const handleBtnClick = () => {
handleClick();
if ( onClick ) {
onClick();
}
};

return (
<button
type="button"
className={`${className} nfd-card-button`}
onClick={handleBtnClick}
disabled={disabled}
className={ `${ className } nfd-card-button` }
onClick={ handleBtnClick }
disabled={ disabled }
>
{text}
{ children }
</button>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@ import {
FOOTER_END,
} from '../../../../../constants';
import NextButtonSiteGen from '../../../Button/NextButtonSiteGen';
import { stepSiteGenEditor } from '../../.././../steps/SiteGen/Editor/step';

const SiteGenFooter = () => {
const isLargeViewport = useViewportMatch( 'small' );
const { footerNavEnabled } = useSelect( ( select ) => {
const { footerNavEnabled, currentStep } = useSelect( ( select ) => {
return {
footerNavEnabled:
select( nfdOnboardingStore ).getFooterNavEnabled(),
currentStep: select( nfdOnboardingStore ).getCurrentStep(),
};
} );

const isEditorStep = currentStep === stepSiteGenEditor;
return (
<>
<Fill name={ `${ FOOTER_SITEGEN }/${ FOOTER_START }` }>
<ToggleDarkMode />
{ ! isEditorStep && <ToggleDarkMode /> }
</Fill>
{ ! isLargeViewport && (
<Fill name={ `${ FOOTER_SITEGEN }/${ FOOTER_END }` }>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const SiteGenHeader = () => {
path: currentStep?.path,
} );
const progress = ( currentStepIndex / allSteps.length ) * 100;

return (
<>
<Fill name={ `${ HEADER_SITEGEN }/${ HEADER_TOP }` }>
Expand All @@ -48,10 +47,14 @@ const SiteGenHeader = () => {
) }
</>
</Fill>
<Fill name={ `${ HEADER_SITEGEN }/${ HEADER_START }` }>
<>{ isHeaderNavigationEnabled && <StepNavigation /> }</>
</Fill>
{ currentStep?.header && <currentStep.header /> }

{ currentStep?.header?.component
? isHeaderNavigationEnabled && <currentStep.header.component />
: isHeaderNavigationEnabled && (
<Fill name={ `${ HEADER_SITEGEN }/${ HEADER_START }` }>
<StepNavigation />
</Fill>
) }
</>
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/OnboardingSPA/components/Header/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Slot } from '@wordpress/components';
import { Fragment, Suspense } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import classNames from 'classnames';

import { store as nfdOnboardingStore } from '../../store';
import {
Expand All @@ -9,19 +10,18 @@ import {
HEADER_START,
HEADER_TOP,
} from '../../../constants';
import classNames from 'classnames';
import { stepSiteGenEditor } from '../../steps/SiteGen/Editor/step';
import { SITEGEN_FLOW } from '../../data/flows/constants';

const Header = () => {
const { headers, headerActiveView, isHeaderEnabled, currentStep } =
useSelect( ( select ) => {
return {
currentStep: select( nfdOnboardingStore ).getCurrentStep(),
headers: select( nfdOnboardingStore ).getHeaders(),
headerActiveView:
select( nfdOnboardingStore ).getHeaderActiveView(),
isHeaderEnabled: select( nfdOnboardingStore ).isHeaderEnabled(),
currentStep: select( nfdOnboardingStore ).getCurrentStep(),
};
} );

Expand Down
Loading

0 comments on commit 105a327

Please sign in to comment.