Skip to content

Commit

Permalink
Fix: Check ability to create patterns on the add new pattern modal.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jun 17, 2024
1 parent 6633dc3 commit f808c13
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions packages/edit-site/src/components/add-new-pattern/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,29 @@ export default function AddNewPattern() {
const [ showPatternModal, setShowPatternModal ] = useState( false );
const [ showTemplatePartModal, setShowTemplatePartModal ] =
useState( false );
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const { createPatternFromFile } = unlock( useDispatch( patternsStore ) );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
const patternUploadInputRef = useRef();
const { isBlockBasedTheme, addNewPatternLabel, addNewTemplatePartLabel } =
useSelect( ( select ) => {
const { getCurrentTheme, getPostType } = select( coreStore );
return {
isBlockBasedTheme: getCurrentTheme()?.is_block_theme,
addNewPatternLabel: getPostType( PATTERN_TYPES.user )?.labels
?.add_new_item,
addNewTemplatePartLabel: getPostType( TEMPLATE_PART_POST_TYPE )
?.labels?.add_new_item,
};
}, [] );
const {
isBlockBasedTheme,
addNewPatternLabel,
addNewTemplatePartLabel,
canCreatePattern,
canCreateTemplatePart,
} = useSelect( ( select ) => {
const { getCurrentTheme, getPostType, canUser } = select( coreStore );
return {
isBlockBasedTheme: getCurrentTheme()?.is_block_theme,
addNewPatternLabel: getPostType( PATTERN_TYPES.user )?.labels
?.add_new_item,
addNewTemplatePartLabel: getPostType( TEMPLATE_PART_POST_TYPE )
?.labels?.add_new_item,
canCreatePattern: canUser( 'create', 'blocks' ),
canCreateTemplatePart: canUser( 'create', 'template-parts' ),
};
}, [] );

function handleCreatePattern( { pattern } ) {
setShowPatternModal( false );
Expand Down Expand Up @@ -78,31 +86,37 @@ export default function AddNewPattern() {
setShowTemplatePartModal( false );
}

const controls = [
{
const controls = [];
if ( canCreatePattern ) {
controls.push( {
icon: symbol,
onClick: () => setShowPatternModal( true ),
title: addNewPatternLabel,
},
];
} );
}

if ( isBlockBasedTheme ) {
if ( isBlockBasedTheme && canCreateTemplatePart ) {
controls.push( {
icon: symbolFilled,
onClick: () => setShowTemplatePartModal( true ),
title: addNewTemplatePartLabel,
} );
}

controls.push( {
icon: upload,
onClick: () => {
patternUploadInputRef.current.click();
},
title: __( 'Import pattern from JSON' ),
} );
if ( canCreatePattern ) {
controls.push( {
icon: upload,
onClick: () => {
patternUploadInputRef.current.click();
},
title: __( 'Import pattern from JSON' ),
} );
}

const { categoryMap, findOrCreateTerm } = useAddPatternCategory();
if ( controls.length === 0 ) {
return null;
}
return (
<>
{ addNewPatternLabel && (
Expand All @@ -114,8 +128,16 @@ export default function AddNewPattern() {
showTooltip: false,
__next40pxDefaultSize: true,
} }
text={ addNewPatternLabel }
label={ addNewPatternLabel }
text={
canCreatePattern
? addNewPatternLabel
: addNewTemplatePartLabel
}
label={
canCreatePattern
? addNewPatternLabel
: addNewTemplatePartLabel
}
/>
) }
{ showPatternModal && (
Expand Down

0 comments on commit f808c13

Please sign in to comment.