add-library.js
2 years ago
category-list.js
2 years ago
insert-pattern.js
2 years ago
library-cache.js
2 years ago
library-layout.js
2 years ago
library-provider.js
2 years ago
library-selector.js
2 years ago
manage-libraries.js
2 years ago
pattern-details-header.js
2 years ago
pattern-details.js
2 years ago
pattern-list.js
2 years ago
pattern-search.js
2 years ago
pattern.js
2 years ago
selected-patterns.js
2 years ago
library-cache.js
79 lines
| 1 | import { useState, useEffect } from '@wordpress/element'; |
| 2 | import { useLibrary } from './library-provider'; |
| 3 | import apiFetch from '@wordpress/api-fetch'; |
| 4 | import { addQueryArgs } from '@wordpress/url'; |
| 5 | import { isEmpty } from 'lodash'; |
| 6 | import { Button, Icon, Spinner } from '@wordpress/components'; |
| 7 | import { __ } from '@wordpress/i18n'; |
| 8 | import { backup } from '@wordpress/icons'; |
| 9 | |
| 10 | export default function LibraryCache( { setCacheIsClearing, cacheIsClearing } ) { |
| 11 | const { activeLibrary, setLibraryCategories, setLibraryPatterns, isLocal } = useLibrary(); |
| 12 | const [ cacheData, setCacheData ] = useState( false ); |
| 13 | |
| 14 | async function checkCacheData() { |
| 15 | if ( isLocal ) { |
| 16 | setCacheData( {} ); |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | const cacheDataResponse = await apiFetch( { |
| 21 | path: addQueryArgs( `/generateblocks/v1/pattern-library/get-cache-data`, { |
| 22 | id: activeLibrary.id, |
| 23 | } ), |
| 24 | method: 'GET', |
| 25 | } ); |
| 26 | |
| 27 | if ( cacheDataResponse.success ) { |
| 28 | setCacheData( cacheDataResponse?.response?.data ?? {} ); |
| 29 | } else { |
| 30 | setCacheData( {} ); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | useEffect( () => { |
| 35 | ( async function() { |
| 36 | if ( ! activeLibrary?.id ) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | checkCacheData(); |
| 41 | }() ); |
| 42 | }, [ activeLibrary?.id ] ); |
| 43 | |
| 44 | if ( isEmpty( cacheData ) ) { |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | return ( |
| 49 | <Button |
| 50 | className="has-icon" |
| 51 | variant="tertiary" |
| 52 | size="compact" |
| 53 | disabled={ ! cacheData.can_clear || cacheIsClearing } |
| 54 | label={ __( 'Refresh patterns', 'generateblocks' ) } |
| 55 | showTooltip |
| 56 | onClick={ async() => { |
| 57 | setCacheIsClearing( true ); |
| 58 | const response = await apiFetch( { |
| 59 | path: '/generateblocks/v1/pattern-library/clear-cache', |
| 60 | data: { |
| 61 | id: activeLibrary?.id, |
| 62 | }, |
| 63 | method: 'POST', |
| 64 | } ); |
| 65 | |
| 66 | if ( response.success ) { |
| 67 | await setLibraryCategories(); |
| 68 | await setLibraryPatterns(); |
| 69 | await checkCacheData(); |
| 70 | } |
| 71 | |
| 72 | setCacheIsClearing( false ); |
| 73 | } } |
| 74 | > |
| 75 | { !! cacheIsClearing ? <Spinner /> : <Icon icon={ backup } /> } |
| 76 | </Button> |
| 77 | ); |
| 78 | } |
| 79 |