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

Input Score Analyser #397

Merged
merged 10 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
72 changes: 44 additions & 28 deletions src/OnboardingSPA/components/TextInput/TextInputSiteGen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,46 @@
height,
placeholder,
customerInput,
setIsValidInput,
setCustomerInput,
} ) => {
const textareaRef = useRef( null );
const [ analysisScore, setAnalysisScore ] = useState( 0 );
const [ inputText, setInputText ] = useState( 'nfd-sg-input-box__field' );

useEffect( () => {
textareaRef.current.style.height = height;
const scrollHeight = textareaRef.current.scrollHeight;
textareaRef.current.style.height = scrollHeight + 'px';
const analysisResult = calculateAnalysisScore( customerInput );
setAnalysisScore( analysisResult );
setIsValidInput( analysisResult >= 2 );
}, [ customerInput ] );

Check warning on line 23 in src/OnboardingSPA/components/TextInput/TextInputSiteGen/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'height' and 'setIsValidInput'. Either include them or remove the dependency array. If 'setIsValidInput' changes too often, find the parent component that defines it and wrap that definition in useCallback

const calculateDetail = ( num ) => {
const calculateAnalysisScore = ( input ) => {
/* Number of Characters in the input
* Count < 100 => 0
* 100 < Count <= 150 => 1
* 150 < Count <= 200 => 2
* 200 < Count => 3
*/
const characterCount = input?.length;
let characterScore = 0;
if ( characterCount > 200 ) {
characterScore = 3;
} else if ( characterCount > 150 ) {
characterScore = 2;
} else if ( characterCount > 100 ) {
characterScore = 1;
}

return characterScore;
};

const addInputScoreSyling = ( num ) => {
const selectedButton = 'nfd-sg-input-box__info-icon--selected';
switch ( num ) {
case 1:
if ( customerInput?.length > 30 ) return selectedButton;
break;
case 2:
if ( customerInput?.length > 60 ) return selectedButton;
break;
case 3:
if ( customerInput?.length > 100 ) return selectedButton;
if ( num <= analysisScore ) {
return selectedButton;
}
};

Expand All @@ -37,6 +55,21 @@
setInputText( 'nfd-sg-input-box__field' );
};

const renderDetails = () => {
const buttons = [];
for ( let i = 1; i <= 3; i++ ) {
buttons.push(
<div
className={ classNames(
'nfd-sg-input-box__info-icon',
addInputScoreSyling( i )
) }
/>
);
}
return buttons;
};

return (
<div className={ 'nfd-sg-input' }>
<label htmlFor={ inputText }>
Expand All @@ -56,24 +89,7 @@
<div className={ 'nfd-sg-input-box__info-text' }>
Detail
</div>
<div
className={ classNames(
'nfd-sg-input-box__info-icon',
calculateDetail( 1 )
) }
/>
<div
className={ classNames(
'nfd-sg-input-box__info-icon',
calculateDetail( 2 )
) }
/>
<div
className={ classNames(
'nfd-sg-input-box__info-icon',
calculateDetail( 3 )
) }
/>
{ renderDetails() }
</div>
) : (
<p className={ 'nfd-sg-input-box__hint' }>{ hint }</p>
Expand Down
9 changes: 4 additions & 5 deletions src/OnboardingSPA/steps/SiteGen/SiteDetails/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
const content = getContents();
const isLargeViewport = useViewportMatch( 'small' );
const [ customerInput, setCustomerInput ] = useState();
const [ isValidInput, setIsValidInput ] = useState( false );

const { currentData } = useSelect( ( select ) => {
return {
Expand Down Expand Up @@ -42,13 +43,13 @@
return setCustomerInput( currentData.sitegen.siteDetails.prompt );
}
setFooterNavEnabled( false );
}, [] );

Check warning on line 46 in src/OnboardingSPA/steps/SiteGen/SiteDetails/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'currentData.sitegen.siteDetails.prompt', 'setDrawerActiveView', 'setFooterNavEnabled', 'setHeaderActiveView', 'setIsHeaderEnabled', and 'setSidebarActiveView'. Either include them or remove the dependency array

useEffect( () => {
setFooterNavEnabled( customerInput !== '' );
setFooterNavEnabled( isValidInput );
currentData.sitegen.siteDetails.prompt = customerInput;
setCurrentOnboardingData( currentData );
}, [ customerInput ] );

Check warning on line 52 in src/OnboardingSPA/steps/SiteGen/SiteDetails/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'currentData', 'isValidInput', 'setCurrentOnboardingData', and 'setFooterNavEnabled'. Either include them or remove the dependency array

return (
<CommonLayout isCentered>
Expand All @@ -60,17 +61,15 @@
hint={ content.inputHint }
height={ '40px' }
customerInput={ customerInput }
setIsValidInput={ setIsValidInput }
setCustomerInput={ setCustomerInput }
/>
{ isLargeViewport && (
<div className={ 'nfd-sg-site-details-endrow' }>
<NextButtonSiteGen
className={ 'nfd-sg-site-details--next-btn' }
text={ content.buttonText }
disabled={
customerInput === undefined ||
customerInput === ''
}
disabled={ ! isValidInput }
/>
</div>
) }
Expand Down