-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hosting Config: Enable current Cache Clear logic for all users (#73360)
* Enable current Cache Clear logic for all users * Add tests for the new selector * Remove Clear Cache confirmation dialog * Update reason which is no longer correct * Rename card, refresh copy * Reduce rate-limit time to one minute * Fix remaining code style issue * Fix component name * Limit was reduced to one minute * Use sentence case * Consolidate to one line * Use `<ExternalLink>` component for fancy icon * Fix the test descriptions --------- Co-authored-by: Daniel Bachhuber <[email protected]>
- Loading branch information
1 parent
e4e2508
commit 669b13b
Showing
10 changed files
with
145 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Button, Card } from '@automattic/components'; | ||
import { localizeUrl } from '@automattic/i18n-utils'; | ||
import { localize } from 'i18n-calypso'; | ||
import { connect } from 'react-redux'; | ||
import CardHeading from 'calypso/components/card-heading'; | ||
import ExternalLink from 'calypso/components/external-link'; | ||
import MaterialIcon from 'calypso/components/material-icon'; | ||
import { CLEAR_CACHE } from 'calypso/lib/url/support'; | ||
import { clearWordPressCache } from 'calypso/state/hosting/actions'; | ||
import getRequest from 'calypso/state/selectors/get-request'; | ||
import { shouldRateLimitAtomicCacheClear } from 'calypso/state/selectors/should-rate-limit-atomic-cache-clear'; | ||
import { getSelectedSiteId } from 'calypso/state/ui/selectors'; | ||
|
||
import './style.scss'; | ||
|
||
const CacheCard = ( { | ||
disabled, | ||
shouldRateLimitCacheClear, | ||
clearAtomicWordPressCache, | ||
isClearingCache, | ||
siteId, | ||
translate, | ||
} ) => { | ||
const clearCache = () => { | ||
clearAtomicWordPressCache( siteId, 'Manually clearing again.' ); | ||
}; | ||
|
||
const getClearCacheContent = () => { | ||
return ( | ||
<div> | ||
<p> | ||
{ translate( | ||
'Be careful, clearing the cache may make your site unresponsive while it is being rebuilt. {{a}}Learn more about clearing your site’s cache{{/a}}', | ||
{ | ||
components: { | ||
a: <ExternalLink icon target="_blank" href={ localizeUrl( CLEAR_CACHE ) } />, | ||
}, | ||
} | ||
) } | ||
</p> | ||
<Button | ||
primary | ||
onClick={ clearCache } | ||
busy={ isClearingCache } | ||
disabled={ disabled || isClearingCache || shouldRateLimitCacheClear } | ||
> | ||
<span>{ translate( 'Clear cache' ) }</span> | ||
</Button> | ||
{ shouldRateLimitCacheClear && ( | ||
<p class="form-setting-explanation"> | ||
{ translate( 'You cleared the cache recently. Please wait a minute and try again.' ) } | ||
</p> | ||
) } | ||
</div> | ||
); | ||
}; | ||
//autorenew | ||
return ( | ||
<Card className="cache-card"> | ||
<MaterialIcon icon="autorenew" size={ 24 } /> | ||
<CardHeading>{ translate( 'Cache' ) }</CardHeading> | ||
{ getClearCacheContent() } | ||
</Card> | ||
); | ||
}; | ||
|
||
export default connect( | ||
( state ) => { | ||
const siteId = getSelectedSiteId( state ); | ||
|
||
return { | ||
shouldRateLimitCacheClear: shouldRateLimitAtomicCacheClear( state, siteId ), | ||
isClearingCache: getRequest( state, clearWordPressCache( siteId ) )?.isLoading ?? false, | ||
siteId, | ||
}; | ||
}, | ||
{ | ||
clearAtomicWordPressCache: clearWordPressCache, | ||
} | ||
)( localize( CacheCard ) ); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
client/state/selectors/should-provide-reason-to-clear-atomic-cache.js
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
client/state/selectors/should-rate-limit-atomic-cache-clear.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'calypso/state/hosting/init'; | ||
|
||
const ONE_MINUTE_IN_MILLISECONDS = 60 * 1000; | ||
|
||
export function shouldRateLimitAtomicCacheClear( state, siteId ) { | ||
const lastCacheCleared = state.atomicHosting?.[ siteId ]?.lastCacheClearTimestamp; | ||
|
||
if ( ! lastCacheCleared ) { | ||
return false; | ||
} | ||
|
||
const rateLimitTime = new Date().valueOf() - ONE_MINUTE_IN_MILLISECONDS; | ||
return lastCacheCleared > rateLimitTime; | ||
} |
48 changes: 0 additions & 48 deletions
48
client/state/selectors/test/should-provide-reason-to-clear-atomic-cache.js
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
client/state/selectors/test/should-rate-limit-atomic-cache-clear.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { shouldRateLimitAtomicCacheClear } from 'calypso/state/selectors/should-rate-limit-atomic-cache-clear'; | ||
|
||
const ONE_MINUTE_IN_MILLISECONDS = 60 * 1000; | ||
const ONE_SECOND = 1000; | ||
const SITE_ID = 123; | ||
const TIMESTAMP = 1234567890; | ||
|
||
const generateState = ( { timestamp } ) => ( { | ||
atomicHosting: { | ||
[ SITE_ID ]: { | ||
lastCacheClearTimestamp: timestamp, | ||
}, | ||
}, | ||
} ); | ||
|
||
describe( 'shouldRateLimitAtomicCacheClear', () => { | ||
beforeAll( () => { | ||
jest.useFakeTimers( 'modern' ).setSystemTime( TIMESTAMP ); | ||
} ); | ||
|
||
afterAll( () => { | ||
jest.useRealTimers(); | ||
} ); | ||
|
||
test( 'should return false if there is no stored timestamp', () => { | ||
expect( shouldRateLimitAtomicCacheClear( generateState( { timestamp: null } ), SITE_ID ) ).toBe( | ||
false | ||
); | ||
} ); | ||
|
||
test( 'should return false if the cache was cleared more than a minute ago', () => { | ||
expect( | ||
shouldRateLimitAtomicCacheClear( | ||
generateState( { timestamp: TIMESTAMP - ONE_MINUTE_IN_MILLISECONDS - ONE_SECOND } ), | ||
SITE_ID | ||
) | ||
).toBe( false ); | ||
} ); | ||
|
||
test( 'should return true if the cache was cleared less than a minute ago', () => { | ||
expect( | ||
shouldRateLimitAtomicCacheClear( | ||
generateState( { timestamp: TIMESTAMP - ONE_MINUTE_IN_MILLISECONDS + ONE_SECOND } ), | ||
SITE_ID | ||
) | ||
).toBe( true ); | ||
} ); | ||
} ); |