Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRESS-2 834 Implement Site Classification API (Frontend) #221

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7e1acff
Primary Step Redesign
officiallygod Apr 26, 2023
dd95f26
Sample Site Types
officiallygod Apr 26, 2023
6fe5351
Refactor Code
officiallygod Apr 26, 2023
c3b6cd9
Code Refactoring v0.1
officiallygod Apr 27, 2023
609734b
Making Things Better
officiallygod Apr 27, 2023
c422d4d
Merge branch 'trunk' into PRESS2-834-Implement-Site-Classification-AP…
officiallygod Apr 28, 2023
a0309ec
Added API For Site Class.
officiallygod Apr 28, 2023
c458108
Linting Somethin
officiallygod Apr 28, 2023
0902969
Secondary Linting
officiallygod Apr 28, 2023
39058c6
Revert "Secondary Linting"
officiallygod Apr 28, 2023
3303812
Delete build directory
officiallygod Apr 28, 2023
433a0ba
Add original Build Files
officiallygod Apr 28, 2023
eee31ce
Merge branch 'PRESS2-834-Implement-Site-Classification-API-Frontend' …
officiallygod Apr 28, 2023
6027bab
Add Build Files 2.0
officiallygod Apr 28, 2023
b419b61
Fix Errors
officiallygod Apr 28, 2023
0092ee2
Update site-classifications.js
officiallygod Apr 28, 2023
21dd818
Fix Lint
officiallygod May 2, 2023
e1adff8
Fixed Code
officiallygod May 2, 2023
e8bb64e
Add a fallback
officiallygod May 4, 2023
1606f36
Merge branch 'trunk' into PRESS2-834-Implement-Site-Classification-AP…
officiallygod May 5, 2023
af0b70b
Fixed New CSS for Trunk
officiallygod May 5, 2023
a5961a4
CR Comments v0.1
officiallygod May 8, 2023
bf665bc
use map
officiallygod May 8, 2023
1c85f2f
Refactor
officiallygod May 8, 2023
d06f13b
CSS Naming Fix
officiallygod May 8, 2023
4824260
Adding Custom State
officiallygod May 8, 2023
ce5565b
Update index.js
officiallygod May 8, 2023
27d9851
Added Rotating Queue Logic
officiallygod May 8, 2023
957e830
Some Comments
officiallygod May 8, 2023
25f5382
New Data Structure
officiallygod May 9, 2023
d0a2331
Data Structure Rename
officiallygod May 9, 2023
2742472
fix logic for commerce flow and limit to business type only
arunshenoy99 May 9, 2023
c5525a6
remove missed update
arunshenoy99 May 9, 2023
1e983df
switch to primary for ecommerce
arunshenoy99 May 9, 2023
73ef4ed
more efficient way to update
arunshenoy99 May 9, 2023
90edaba
Merge branch 'trunk' of https://github.com/newfold-labs/wp-module-onb…
arunshenoy99 May 9, 2023
58d59b7
fix flow when user revisits Onboarding
arunshenoy99 May 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions includes/Data/Flows.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ final class Flows {
// to populate the step fields if a user is resuming a flow.
'data' => array(
'siteType' => array(
'label' => '',
'referTo' => 'site',
'primary' => '',
'secondary' => '',
'primary' => array(
'refers' => '',
'value' => '',
),
'secondary' => array(
'refers' => '',
'value' => '',
),
),

'wpComfortLevel' => '0',
Expand Down
8 changes: 2 additions & 6 deletions includes/RestApi/FlowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function save_onboarding_flow_data( \WP_REST_Request $request ) {
At least the primary and secondary update does not run on every flow data request.
*/
if ( ! empty( $params['data']['siteType']['primary']['value'] ) &&
( $flow_data['data']['siteType']['primary']['value'] !== $params['data']['siteType']['primary']['value'] ) ) {
( empty( $flow_data['data']['siteType']['primary']['value'] ) || $flow_data['data']['siteType']['primary']['value'] !== $params['data']['siteType']['primary']['value'] ) ) {
if ( class_exists( 'NewfoldLabs\WP\Module\Data\SiteClassification\PrimaryType' ) ) {
$primary_type = new PrimaryType( $params['data']['siteType']['primary']['refers'], $params['data']['siteType']['primary']['value'] );
if ( ! $primary_type->save() ) {
Expand All @@ -145,7 +145,7 @@ public function save_onboarding_flow_data( \WP_REST_Request $request ) {
}

if ( ! empty( $params['data']['siteType']['secondary']['value'] ) &&
( $flow_data['data']['siteType']['secondary']['value'] !== $params['data']['siteType']['secondary']['value'] ) ) {
( empty( $flow_data['data']['siteType']['secondary']['value'] ) || $flow_data['data']['siteType']['secondary']['value'] !== $params['data']['siteType']['secondary']['value'] ) ) {
if ( class_exists( 'NewfoldLabs\WP\Module\Data\SiteClassification\SecondaryType' ) ) {
$secondary_type = new SecondaryType( $params['data']['siteType']['secondary']['refers'], $params['data']['siteType']['secondary']['value'] );
if ( ! $secondary_type->save() ) {
Expand Down Expand Up @@ -208,10 +208,6 @@ private function update_default_data_for_ecommerce( $data ) {
if ( 'ecommerce' === $flow_type ) {
// update default data with ecommerce data
$data['data']['topPriority']['priority1'] = 'selling';
$data['data']['siteType'] = array(
'label' => '',
'referTo' => 'business',
);
}
return $data;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { __, sprintf } from '@wordpress/i18n';
import CommonLayout from '../../../../../components/Layouts/Common';
import NewfoldLargeCard from '../../../../../components/NewfoldLargeCard';
import {
SIDEBAR_LEARN_MORE,
VIEW_NAV_GET_STARTED,
} from '../../../../../../constants';
import getContents from '../contents';
import { store as nfdOnboardingStore } from '../../../../../store';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';
import CardHeader from '../../../../../components/CardHeader';
import NavCardButton from '../../../../../components/Button/NavCardButton';
import NeedHelpTag from '../../../../../components/NeedHelpTag';
import content from '../content.json';
import { translations } from '../../../../../utils/locales/translations';
import Animate from '../../../../../components/Animate';
import { getSiteClassification } from '../../../../../utils/api/siteClassification';

const StepPrimarySetup = () => {
const {
Expand All @@ -24,9 +23,9 @@ const StepPrimarySetup = () => {
setCurrentOnboardingData,
} = useDispatch( nfdOnboardingStore );

const { currentStep, currentData } = useSelect( ( select ) => {
const contents = getContents();
const { currentData } = useSelect( ( select ) => {
return {
currentStep: select( nfdOnboardingStore ).getCurrentStep(),
currentData:
select( nfdOnboardingStore ).getCurrentOnboardingData(),
};
Expand All @@ -37,131 +36,139 @@ const StepPrimarySetup = () => {
setIsDrawerSuppressed( true );
setDrawerActiveView( VIEW_NAV_GET_STARTED );
setIsHeaderNavigationEnabled( true );
getSiteClassificationData();
}, [] );

const [ clickedIndex, changeCategory ] = useState( -1 );
const [ inputCategVal, changeInputCateg ] = useState( '' );
const [ custom, setCustom ] = useState( false );
const [ siteClassification, setSiteClassification ] = useState();
const [ primaryCategory, setPrimaryCategory ] = useState( '' );

const categoriesArray = content.categories;
const selectedPrimaryCategoryInStore = currentData?.data?.siteType?.primary;
/**
* Function which fetches the Site Classifications
*
*/
const getSiteClassificationData = async () => {
const siteClassificationData = await getSiteClassification();
setSiteClassification( siteClassificationData?.body );

/**This condition fills the data in input box if the saved category isn't a subcategory from the content*/
if ( selectedPrimaryCategoryInStore && ! inputCategVal ) {
const found = categoriesArray.find(
( e ) => e.name === selectedPrimaryCategoryInStore
);
if ( ! found && selectedPrimaryCategoryInStore !== 'primaryCategory' )
changeInputCateg( selectedPrimaryCategoryInStore );
}
// Incase old user comes again with data, we need to save it
if (
typeof currentData?.data?.siteType?.primary === 'string' ||
typeof currentData?.data?.siteType?.secondary === 'string'
) {
const primaryValue = currentData?.data?.siteType?.primary;
const secondaryValue = currentData?.data?.siteType?.secondary;
currentData.data.siteType.primary = {
refers: 'custom',
value: primaryValue,
};
currentData.data.siteType.secondary = {
refers: 'custom',
value: secondaryValue,
};
setCurrentOnboardingData( currentData );
}

const handleCategoryClick = ( idxOfElm ) => {
changeCategory( idxOfElm );
changeInputCateg( '' );
const currentDataCopy = currentData;
currentDataCopy.data.siteType.primary =
content.categories[ idxOfElm ]?.name;
setCurrentOnboardingData( currentDataCopy );
setPrimaryCategory( currentData?.data?.siteType?.primary?.value ?? '' );
if ( currentData?.data?.siteType?.primary?.refers === 'custom' ) {
categoryInput( currentData?.data?.siteType?.primary?.value );
}
};

/**
* Function which saves data in redux when category name is selected via chips
*
* @param {string} primType
*/
const handleCategoryClick = ( primType ) => {
setCustom( false );
setPrimaryCategory( primType );
currentData.data.siteType.primary.refers = 'slug';
currentData.data.siteType.primary.value = primType;
setCurrentOnboardingData( currentData );
};

/**
* Function which saves data in redux when category name is put-in via input box
*
* @param input
* @param {string} value
*/
const categoryInput = ( input ) => {
changeCategory( -1 );
changeInputCateg( input?.target?.value );
const currentDataCopy = currentData;
currentDataCopy.data.siteType.primary = input?.target?.value;
setCurrentOnboardingData( currentDataCopy );
const categoryInput = ( value ) => {
setCustom( true );
setPrimaryCategory( value );
currentData.data.siteType.primary.refers = 'custom';
currentData.data.siteType.primary.value = value;
setCurrentOnboardingData( currentData );
};

const primarySiteTypeChips = () => {
const types = siteClassification?.types;
return Object.keys( types ).map( ( type, idx ) => {
return (
<div
key={ types[ type ]?.slug }
tabIndex={ idx + 1 }
role="button"
className={ `${
types[ type ].slug === primaryCategory && ! custom
? 'chosenPrimaryCategory '
: ''
}nfd-card-pri-category` }
onClick={ () => handleCategoryClick( types[ type ].slug ) }
onKeyDown={ () =>
handleCategoryClick( types[ type ].slug )
}
>
<div className="nfd-card-pri-category-wrapper">
<span
className={ `nfd-card-pri-category-wrapper__icon ${
types[ type ].slug === primaryCategory
? 'nfd-card-pri-category-wrapper__icon-selected '
: ''
}` }
style={ {
backgroundImage: `url(${ types[ type ]?.icon })`,
} }
></span>
<span className="categName">
{ types[ type ]?.label }
</span>
</div>
</div>
);
} );
};

return (
<CommonLayout isBgPrimary isCentered>
<NewfoldLargeCard>
<div className="nfd-card-heading center">
<CardHeader
heading={ __(
currentStep?.heading,
'wp-module-onboarding'
) }
subHeading={ sprintf(
__( content.subHeading, 'wp-module-onboarding' ),
translations( 'SITE' )
) }
question={ __(
currentStep?.subheading,
'wp-module-onboarding'
) }
heading={ contents.cardHeading }
subHeading={ contents.subHeading }
question={ contents.question }
/>
</div>
<Animate
type="fade-in-disabled"
after={
content.categories &&
selectedPrimaryCategoryInStore !== null
}
>
<Animate type="fade-in-disabled" after={ siteClassification }>
<div className="nfd-setup-primary-categories">
{ content.categories.map( ( item, idx ) => {
return (
<div
key={ item?.name }
className={ `${
clickedIndex === idx ||
item.name ===
selectedPrimaryCategoryInStore
? 'chosenPrimaryCategory '
: ''
}nfd-card-category` }
onClick={ ( e ) =>
handleCategoryClick( idx )
}
>
<div className="nfd-card-category-wrapper">
<span
className="icon"
style={ {
backgroundImage:
clickedIndex !== idx &&
item.name !==
selectedPrimaryCategoryInStore
? item?.icon
: item?.iconWhite,
} }
></span>
<span className="categName">
{ item?.name }
</span>
</div>
</div>
);
} ) }
{ siteClassification && primarySiteTypeChips() }
</div>

<div className="nfd-setup-primary-second">
<div className="nfd-setup-primary-second-top">
<p className="blackText">or tell us here:</p>
<input
type="text"
onChange={ ( e ) => categoryInput( e ) }
className="tellUsInput"
placeholder={ sprintf(
__(
content.placeholderSiteTypeInput,
'wp-module-onboarding'
),
translations( 'site' )
) }
value={ inputCategVal }
/>
</div>
<div className="nfd-setup-primary-custom">
<p className="nfd-setup-primary-custom__tellus-text">
or tell us here:
</p>
<input
type="search"
onChange={ ( e ) =>
categoryInput( e?.target?.value )
}
className="nfd-setup-primary-custom__tellus-input"
placeholder={ contents.placeholderSiteTypeInput }
value={ custom ? primaryCategory : '' }
/>
</div>
</Animate>
<NavCardButton
text={ __( content.buttonText ) }
disabled={ content.categories === null }
/>
<NavCardButton text={ contents.buttonText } />
<NeedHelpTag />
</NewfoldLargeCard>
</CommonLayout>
Expand Down
Loading