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

Enhance/press7 60 jetpack boost #25

Merged
merged 27 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f7801ae
New branch Jetpack Boost
Nov 4, 2024
fbf0991
Fix: phpcs
Nov 5, 2024
3236585
Tweak: aligned toggle input next to labels
Nov 8, 2024
1764da6
Tweak: replace "console.log" with "console.error"
Nov 13, 2024
94f7456
Tweak: groups imports by type
AleTorrisi Nov 18, 2024
d4c4074
Tweak: component and variable name in "Advanced Settings" section
AleTorrisi Nov 18, 2024
319e8c9
Tweak: better handling of Rest API
AleTorrisi Nov 18, 2024
23ed28b
Tweak: handling of failure during saving options
AleTorrisi Nov 18, 2024
6bf3faf
Tweak: remove unused constant
AleTorrisi Nov 18, 2024
a569d2a
Tweak: parameter name
AleTorrisi Nov 18, 2024
3e39933
Tweak: import 'useState' and ''useEffect' from WordPress library inst…
AleTorrisi Nov 18, 2024
be0bf02
Tweak: fetch options function
AleTorrisi Nov 18, 2024
d1c6e62
Fix remove unused text
AleTorrisi Nov 18, 2024
b71cd5f
Tweak: store Jetpack Boost options in NewFold Runtime object avoiding…
AleTorrisi Nov 18, 2024
b58793d
Tweak: handling of style script
AleTorrisi Nov 18, 2024
717557b
Tweak: replaced "src/css" folder with "styles" folder
AleTorrisi Nov 18, 2024
70b1e1f
JS Lint
AleTorrisi Nov 18, 2024
37799c1
Fix phpcs
AleTorrisi Nov 18, 2024
a113d03
phpcs fix
AleTorrisi Nov 18, 2024
736cf48
New: upsell option "Optimize Critical CSS Loading"
AleTorrisi Nov 25, 2024
3bbc47a
Removed useless comment
AleTorrisi Nov 25, 2024
2f0e7d7
Fix: hide premium option "Critical CSS" when module is disabled
AleTorrisi Nov 26, 2024
0e5d09a
Test commit for cy test
AleTorrisi Nov 26, 2024
c748863
Fix: Jetpack Boost upsell link
AleTorrisi Nov 26, 2024
74361d5
Fix: get correctly the site url in case of subdomain
AleTorrisi Nov 27, 2024
e7e7c10
Testing cypress fix
arunshenoy99 Dec 12, 2024
64bf1bd
Fix Web and Mojo fatal error
arunshenoy99 Dec 12, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// WordPress
import { useState } from '@wordpress/element';

// Newfold
import { Spinner } from '@newfold/ui-component-library';
import { NewfoldRuntime } from '@newfold-labs/wp-module-runtime';

const InstallActivatePluginButton = ( {
methods,
constants,
setModuleStatus,
} ) => {
const [ isLoading, setIsLoading ] = useState( false );
const [ isActive, setIsActive ] = useState( false );
const [ message, setMessage ] = useState( null );

const handleInstallActivate = async () => {
setIsLoading( true );
setMessage( constants.text.jetpackBoostInstalling );

const apiUrl = methods.NewfoldRuntime.createApiUrl(
'/newfold-installer/v1/plugins/install'
);
const INSTALL_TOKEN = NewfoldRuntime.sdk.performance.install_token;
const plugin = 'jetpack-boost';

try {
await methods.apiFetch( {
url: apiUrl,
method: 'POST',
headers: { 'X-NFD-INSTALLER': INSTALL_TOKEN },
data: { plugin, activate: true, queue: false },
} );
setIsActive( true );
setModuleStatus( true );
setMessage( constants.text.jetpackBoostInstalling );
} catch ( error ) {
setMessage( constants.text.jetpackBoostActivationFailed );
} finally {
setIsLoading( false );
}
};

return (
<div className="nfd-performance-jetpack-boost-container-install-activate-button">
<button
className="nfd-button--upsell"
onClick={ handleInstallActivate }
>
{ isLoading ? (
<>
<Spinner />
<p>{ message }</p>
</>
) : (
! isActive && (
<>
<span>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth="2"
stroke="currentColor"
aria-hidden="true"
className="nfd-w-5 nfd-h-5 nfd--ml-1 nfd-shrink-0"
role="img"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M8 11V7a4 4 0 118 0m-4 8v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2z"
></path>
</svg>
</span>
<span>{ constants.text.jetpackBoostCtaText }</span>
</>
)
) }
</button>
</div>
);
};

export default InstallActivatePluginButton;
184 changes: 184 additions & 0 deletions components/advancedSettings/JetpackBoost/SingleOption.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// WordPress
import apiFetch from '@wordpress/api-fetch';
import { useRef, useState } from '@wordpress/element';

// Newfold
import {
ToggleField,
Textarea,
FeatureUpsell,
} from '@newfold/ui-component-library';
import { NewfoldRuntime } from '@newfold-labs/wp-module-runtime';

// Third-parts
import parse from 'html-react-parser';

const SingleOption = ( { params, isChild, methods, constants } ) => {
const [ optionDetails, setOptionDetails ] = useState( {
id: params.id,
label: params.label,
description: params.description,
value: params.value ? String( params.value ) : '',
type: params.type,
externalText: params.externalText,
premiumUrl: params.premiumUrl ?? '',
children: params.children,
} );

const [ isShown, setIsShown ] = useState( false );

const debounceTimeout = useRef( null ); // Mantiene il timeout tra i render

const handleChangeOption = ( value, id ) => {
if ( typeof value === 'object' ) {
value = value.target.value;
}

setOptionDetails( { ...optionDetails, value } );

// Clear the previous timeout if user types again.
if ( debounceTimeout.current ) {
clearTimeout( debounceTimeout.current );
}

// Set a new timeout of 2 seconds.
debounceTimeout.current = setTimeout( () => {
apiFetch( {
path: 'newfold-performance/v1/jetpack/settings',
method: 'POST',
data: {
field: {
id,
value,
},
},
} )
.then( () => {
methods.makeNotice(
'cache-level-change-notice',
constants.text.optionSet,
'',
'success',
5000
);
} )
.catch( () => {
methods.makeNotice(
'cache-level-change-notice',
constants.text.optionNotSet,
'',
'error',
5000
);
} );
}, 1000 );
};

const displayOption = ( option ) => {
switch ( option.type ) {
case 'toggle':
return (
<>
{ option.premiumUrl &&
! NewfoldRuntime.sdk.performance
.jetpack_boost_premium_is_active && (
<FeatureUpsell
cardText="Upgrade to Unlock"
cardLink={ option.premiumUrl }
>
<ToggleField
id={ option.id }
label={ option.label }
description={ parse(
option.description
) }
checked={ !! option.value }
onChange={ ( value ) =>
handleChangeOption(
value,
option.id
)
}
/>
{ option.externalText ? (
<p
style={ {
textDecoration: 'underline',
margin: '10px 0',
} }
>
{ parse( option.externalText ) }
</p>
) : null }
</FeatureUpsell>
) }
{ ( ! option.premiumUrl ||
NewfoldRuntime.sdk.performance
.jetpack_boost_premium_is_active ) && (
<>
<ToggleField
id={ option.id }
label={ option.label }
description={ option.description }
checked={ !! option.value }
onChange={ ( value ) =>
handleChangeOption( value, option.id )
}
/>
{ option.externalText ? (
<p
style={ {
textDecoration: 'underline',
margin: '10px 0',
} }
>
{ parse( option.externalText ) }
</p>
) : null }
</>
) }
</>
);

case 'textarea':
return (
<>
<p className="field-label">{ option.label }</p>
<Textarea
id={ option.id }
description={ option.description }
value={ option.value ?? '' }
onChange={ ( value ) => {
handleChangeOption( value, option.id );
} }
/>
</>
);
default:
return null;
}
};

return (
<>
{ isChild && (
<div className="child-field">
<div
className="wrap-button"
style={ { textAlign: 'right' } }
>
<button onClick={ () => setIsShown( ! isShown ) }>
{ isShown
? constants.text.jetpackBoostShowLess
: constants.text.jetpackBoostShowMore }
</button>
</div>
{ isShown && displayOption( optionDetails ) }
</div>
) }
{ ! isChild && displayOption( optionDetails ) }
</>
);
};

export default SingleOption;
Loading
Loading