diff --git a/CHANGELOG.md b/CHANGELOG.md index 79abd1d..4fe4eb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to Freemius for WordPress are documented in this file. ## [Unreleased] +- improved: added a Clear Cache button in the Freemius block sidebar (mapping panel and parent scope settings) so you can refresh pricing data from the API without leaving the editor ## [0.4.2] - fixed: missing import for MappingSettings diff --git a/src/blocks/modifier/edit.js b/src/blocks/modifier/edit.js index e1260fe..5abb8d7 100644 --- a/src/blocks/modifier/edit.js +++ b/src/blocks/modifier/edit.js @@ -33,6 +33,7 @@ import './editor.scss'; import { useData, useModifiers } from '../../hooks'; import { ModifierButtons } from './ModifierButtons'; +import ClearCacheButton from '../../scope/ClearCacheButton'; export default function Edit( props ) { const { attributes, setAttributes, scopeData } = props; @@ -175,12 +176,19 @@ export default function Edit( props ) { 'freemius' ) } - + + + { - if ( cachedData && ! data ) setData( cachedData ); + if ( cachedData ) setData( cachedData ); + else if ( data ) setData( null ); }, [ cachedData, data ] ); // Immediate fetch on mount diff --git a/src/scope/ClearCacheButton.js b/src/scope/ClearCacheButton.js new file mode 100644 index 0000000..7c77de2 --- /dev/null +++ b/src/scope/ClearCacheButton.js @@ -0,0 +1,63 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { Button, Tooltip } from '@wordpress/components'; +import { useDispatch, useSelect } from '@wordpress/data'; +import { store as noticesStore } from '@wordpress/notices'; + +/** + * Internal dependencies + */ +import { API_STORE } from '../stores'; +import { useData, usePlans } from '../hooks'; + +const ClearCacheButton = ( { size } ) => { + const { clearServerCache } = useDispatch( API_STORE ); + const { createNotice } = useDispatch( noticesStore ); + + const isClearing = useSelect( ( select ) => + select( API_STORE ).isLoading( 'cache-clear' ) + ); + + const { data } = useData(); + const { refetch } = usePlans( data?.product_id ); + + const handleClearCache = async () => { + try { + await clearServerCache(); + if ( data?.product_id ) await refetch( true ); + createNotice( + 'success', + __( 'Cache cleared. Data refreshed.', 'freemius' ), + { type: 'snackbar' } + ); + } catch { + createNotice( 'error', __( 'Could not clear cache.', 'freemius' ), { + type: 'snackbar', + } ); + } + }; + + return ( + + + + ); +}; + +export default ClearCacheButton; diff --git a/src/scope/MappingSettings.js b/src/scope/MappingSettings.js index 4290eff..1a927ac 100644 --- a/src/scope/MappingSettings.js +++ b/src/scope/MappingSettings.js @@ -14,6 +14,7 @@ import { TextControl, Button, SelectControl, + Flex, __experimentalSpacer as Spacer, } from '@wordpress/components'; import { useContext, useEffect } from '@wordpress/element'; @@ -23,6 +24,7 @@ import { useContext, useEffect } from '@wordpress/element'; */ import { useData, useMapping } from '../hooks'; import { FreemiusContext } from '../context'; +import ClearCacheButton from './ClearCacheButton'; const MappingSettings = ( props ) => { const { attributes } = props; @@ -67,6 +69,9 @@ const MappingSettings = ( props ) => { ) } + + + { isError && ( <> diff --git a/src/scope/Settings.js b/src/scope/Settings.js index 216f3ec..16fb2de 100644 --- a/src/scope/Settings.js +++ b/src/scope/Settings.js @@ -14,6 +14,7 @@ import { PanelBody, __experimentalToolsPanelItem as ToolsPanelItem, Button, + Flex, __experimentalSpacer as Spacer, Notice, } from '@wordpress/components'; @@ -25,6 +26,7 @@ import EnableCheckbox from './EnableCheckbox'; import { useSettings, useData } from '../hooks'; import Property from './Property'; import ButtonSettings from './ButtonSettings'; +import ClearCacheButton from './ClearCacheButton'; const PanelDescription = styled.div` grid-column: span 2; @@ -106,17 +108,22 @@ const Settings = ( props ) => { } { ...props } /> - { freemius_enabled && freemius_modifications && ( - + { freemius_enabled && ( + + { freemius_modifications && ( + + ) } + + ) } { freemius_enabled && errorMessage && (