Skip to content

Commit

Permalink
add export JSON action
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Dec 27, 2023
1 parent 17bde24 commit d26df02
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
@@ -0,0 +1,34 @@
/**
* External dependencies
*/
import { paramCase as kebabCase } from 'change-case';

/**
* WordPress dependencies
*/
import { downloadBlob } from '@wordpress/blob';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { PATTERN_TYPES } from '../../utils/constants';

export const exportJSONaction = {
id: 'duplicate-pattern',
label: __( 'Export as JSON' ),
isEligible: ( item ) => item.type === PATTERN_TYPES.user,
callback: ( item ) => {
const json = {
__file: item.type,
title: item.title || item.name,
content: item.patternBlock.content.raw,
syncStatus: item.patternBlock.wp_pattern_sync_status,
};
return downloadBlob(
`${ kebabCase( item.title || item.name ) }.json`,
JSON.stringify( json, null, 2 ),
'application/json'
);
},
};
Expand Up @@ -36,7 +36,7 @@ import {
PATTERN_SYNC_TYPES,
PATTERN_DEFAULT_CATEGORY,
} from '../../utils/constants';
// import { duplicatePatternAction } from './dataviews-pattern-actions';
import { exportJSONaction } from './dataviews-pattern-actions';
import usePatternSettings from './use-pattern-settings';
import { unlock } from '../../lock-unlock';
import usePatterns from './use-patterns';
Expand Down Expand Up @@ -283,7 +283,7 @@ export default function DataviewsPatterns() {
};
}, [ patterns, view, fields ] );

// const actions = useMemo( () => [ duplicatePatternAction ], [] );
const actions = useMemo( () => [ exportJSONaction ], [] );
const onChangeView = useCallback(
( viewUpdater ) => {
let updatedView =
Expand All @@ -303,8 +303,6 @@ export default function DataviewsPatterns() {
[ view, setView ]
);
const id = useId();
const titleId = `${ id }-title`;
const descriptionId = `${ id }-description`;
const settings = usePatternSettings();
// Wrap everything in a block editor provider.
// This ensures 'styles' that are needed for the previews are synced
Expand All @@ -320,13 +318,13 @@ export default function DataviewsPatterns() {
<PatternsHeader
categoryId={ categoryId }
type={ type }
titleId={ titleId }
descriptionId={ descriptionId }
titleId={ `${ id }-title` }
descriptionId={ `${ id }-description` }
/>
<DataViews
paginationInfo={ paginationInfo }
fields={ fields }
// actions={ actions }
actions={ actions }
data={ data || EMPTY_ARRAY }
getItemId={ ( item ) => item.name }
isLoading={ isResolving }
Expand Down

0 comments on commit d26df02

Please sign in to comment.