Skip to content

Commit 39718fc

Browse files
committed
Update states to support non existence of capabilities keys
1 parent 97251d6 commit 39718fc

File tree

2 files changed

+108
-67
lines changed

2 files changed

+108
-67
lines changed

src/components/App/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ const App = () => {
3232
}
3333
}, [] );
3434

35+
function capabilityKeyExists( key ) {
36+
return (
37+
typeof window.NewfoldRuntime !== 'undefined' &&
38+
window.NewfoldRuntime.capabilities &&
39+
Object.prototype.hasOwnProperty.call(
40+
window.NewfoldRuntime.capabilities,
41+
key
42+
)
43+
);
44+
}
45+
3546
return (
3647
<Root context={ { isRTL: false } }>
3748
<NotificationFeed />
@@ -81,9 +92,11 @@ const App = () => {
8192
>
8293
<ImageOptimization />
8394
</Container.Block>
84-
<Container.Block className="newfold-font-optimization">
85-
<FontOptimization />
86-
</Container.Block>
95+
{ capabilityKeyExists( 'hasCloudflareFonts' ) && (
96+
<Container.Block className="newfold-font-optimization">
97+
<FontOptimization />
98+
</Container.Block>
99+
) }
87100
</Container>
88101
</Page>
89102
</Root>

src/sections/ImageOptimization/index.js

Lines changed: 92 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,27 @@ const ImageOptimization = () => {
178178
fetchSettings();
179179
}, [] );
180180

181+
function capabilityKeyExists( key ) {
182+
if (
183+
typeof window.NewfoldRuntime !== 'undefined' &&
184+
window.NewfoldRuntime.capabilities &&
185+
Object.prototype.hasOwnProperty.call(
186+
window.NewfoldRuntime.capabilities,
187+
key
188+
)
189+
) {
190+
return true;
191+
}
192+
return false;
193+
}
194+
195+
function isCapabilityEnabled( key ) {
196+
return (
197+
capabilityKeyExists( key ) &&
198+
window.NewfoldRuntime.capabilities[ key ] === true
199+
);
200+
}
201+
181202
if ( isLoading ) return <p>{ imageOptimizationLoadingMessage }</p>;
182203

183204
if ( isError )
@@ -216,6 +237,15 @@ const ImageOptimization = () => {
216237
return `${ window.location.origin }${ basePath }/wp-admin/upload.php?autoSelectBulk`;
217238
};
218239

240+
const hasPolish = capabilityKeyExists( 'hasCloudflarePolish' );
241+
const hasMirage = capabilityKeyExists( 'hasCloudflareMirage' );
242+
const polishEnabled = isCapabilityEnabled( 'hasCloudflarePolish' );
243+
const mirageEnabled = isCapabilityEnabled( 'hasCloudflareMirage' );
244+
const showNothing = ! hasPolish && ! hasMirage;
245+
const showUpsell =
246+
( hasPolish || hasMirage ) && ! polishEnabled && ! mirageEnabled;
247+
const showToggles = polishEnabled || mirageEnabled;
248+
219249
return (
220250
<Container.SettingsField
221251
title={ imageOptimizationSettingsTitle }
@@ -347,71 +377,69 @@ const ImageOptimization = () => {
347377
}
348378
disabled={ isBanned }
349379
/>
350-
{ ! NewfoldRuntime.hasCapability(
351-
'hasCloudflarePolish'
352-
) &&
353-
! NewfoldRuntime.hasCapability(
354-
'hasCloudflareMirage'
355-
) && (
356-
<div>
357-
<FeatureUpsell
358-
cardText={ imageOptimizationUpsellText }
359-
cardLink={ imageOptimizationUpsellLink }
360-
>
361-
<ToggleField
362-
id="cloudflare-polish"
363-
label={
364-
imageOptimizationPolishLabel
365-
}
366-
description={
367-
imageOptimizationPolishDescription
368-
}
369-
checked={ false }
370-
disabled
371-
/>{ ' ' }
372-
<ToggleField
373-
id="cloudflare-mirage"
374-
label={
375-
imageOptimizationMirageLabel
376-
}
377-
description={
378-
imageOptimizationMirageDescription
379-
}
380-
checked={ false }
381-
disabled
382-
/>{ ' ' }
383-
</FeatureUpsell>
384-
</div>
385-
) }
386-
{ NewfoldRuntime.hasCapability(
387-
'hasCloudflarePolish'
388-
) && (
389-
<ToggleField
390-
id="cloudflare-polish"
391-
label={ imageOptimizationPolishLabel }
392-
description={
393-
imageOptimizationPolishDescription
394-
}
395-
checked={ polish }
396-
onChange={ () =>
397-
handleToggle( 'cloudflarePolish', ! polish )
398-
}
399-
/>
380+
{ ! showNothing && showUpsell && (
381+
<div>
382+
<FeatureUpsell
383+
cardText={ imageOptimizationUpsellText }
384+
cardLink={ imageOptimizationUpsellLink }
385+
>
386+
<ToggleField
387+
id="cloudflare-polish"
388+
label={ imageOptimizationPolishLabel }
389+
description={
390+
imageOptimizationPolishDescription
391+
}
392+
checked={ false }
393+
disabled
394+
/>{ ' ' }
395+
<ToggleField
396+
id="cloudflare-mirage"
397+
label={ imageOptimizationMirageLabel }
398+
description={
399+
imageOptimizationMirageDescription
400+
}
401+
checked={ false }
402+
disabled
403+
/>{ ' ' }
404+
</FeatureUpsell>
405+
</div>
400406
) }
401-
{ NewfoldRuntime.hasCapability(
402-
'hasCloudflareMirage'
403-
) && (
404-
<ToggleField
405-
id="cloudflare-mirage"
406-
label={ imageOptimizationMirageLabel }
407-
description={
408-
imageOptimizationMirageDescription
409-
}
410-
checked={ mirage }
411-
onChange={ () =>
412-
handleToggle( 'cloudflareMirage', ! mirage )
413-
}
414-
/>
407+
408+
{ ! showNothing && showToggles && (
409+
<>
410+
{ polishEnabled && (
411+
<ToggleField
412+
id="cloudflare-polish"
413+
label={ imageOptimizationPolishLabel }
414+
description={
415+
imageOptimizationPolishDescription
416+
}
417+
checked={ polish }
418+
onChange={ () =>
419+
handleToggle(
420+
'cloudflarePolish',
421+
! polish
422+
)
423+
}
424+
/>
425+
) }
426+
{ mirageEnabled && (
427+
<ToggleField
428+
id="cloudflare-mirage"
429+
label={ imageOptimizationMirageLabel }
430+
description={
431+
imageOptimizationMirageDescription
432+
}
433+
checked={ mirage }
434+
onChange={ () =>
435+
handleToggle(
436+
'cloudflareMirage',
437+
! mirage
438+
)
439+
}
440+
/>
441+
) }
442+
</>
415443
) }
416444
</>
417445
) }

0 commit comments

Comments
 (0)