Skip to content

Commit

Permalink
Using new constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Sep 15, 2023
1 parent 902dc6a commit b1cfcc0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
* Internal dependencies
*/
import {
TEMPLATE_PARTS,
PATTERNS,
SYNC_TYPES,
USER_PATTERNS,
USER_PATTERN_CATEGORY,
} from './utils';
TEMPLATE_PART_POST_TYPE,
PATTERN_THEME_TYPE,
PATTERN_SYNC_STATUSES,
PATTERN_POST_TYPE,
PATTERN_DEFAULT_CATEGORY,
} from '../../utils/constants';
import {
useExistingTemplateParts,
getUniqueTemplatePartTitle,
Expand All @@ -28,12 +28,12 @@ import { unlock } from '../../lock-unlock';
const { useHistory } = unlock( routerPrivateApis );

function getPatternMeta( item ) {
if ( item.type === PATTERNS ) {
return { wp_pattern_sync_status: SYNC_TYPES.unsynced };
if ( item.type === PATTERN_THEME_TYPE ) {
return { wp_pattern_sync_status: PATTERN_SYNC_STATUSES.unsynced };
}

const syncStatus = item.reusableBlock.wp_pattern_sync_status;
const isUnsynced = syncStatus === SYNC_TYPES.unsynced;
const isUnsynced = syncStatus === PATTERN_SYNC_STATUSES.unsynced;

return {
...item.reusableBlock.meta,
Expand Down Expand Up @@ -88,9 +88,9 @@ export default function DuplicateMenuItem( {
);

history.push( {
postType: TEMPLATE_PARTS,
postType: TEMPLATE_PART_POST_TYPE,
postId: result?.id,
categoryType: TEMPLATE_PARTS,
categoryType: TEMPLATE_PART_POST_TYPE,
categoryId,
} );

Expand All @@ -113,7 +113,7 @@ export default function DuplicateMenuItem( {

async function createPattern() {
try {
const isThemePattern = item.type === PATTERNS;
const isThemePattern = item.type === PATTERN_THEME_TYPE;
const title = sprintf(
/* translators: %s: Existing pattern title */
__( '%s (Copy)' ),
Expand Down Expand Up @@ -147,9 +147,9 @@ export default function DuplicateMenuItem( {
);

history.push( {
categoryType: USER_PATTERNS,
categoryId: USER_PATTERN_CATEGORY,
postType: USER_PATTERNS,
categoryType: PATTERN_POST_TYPE,
categoryId: PATTERN_DEFAULT_CATEGORY,
postType: PATTERN_POST_TYPE,
postId: result?.id,
} );

Expand All @@ -169,7 +169,9 @@ export default function DuplicateMenuItem( {
}

const createItem =
item.type === TEMPLATE_PARTS ? createTemplatePart : createPattern;
item.type === TEMPLATE_PART_POST_TYPE
? createTemplatePart
: createPattern;

return <MenuItem onClick={ createItem }>{ label }</MenuItem>;
}
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/page-patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getQueryArgs } from '@wordpress/url';
*/
import {
PATTERN_DEFAULT_CATEGORY,
PATTERN_DEFAULT_POST_TYPE,
PATTERN_POST_TYPE,
} from '../../utils/constants';
import Page from '../page';
import PatternsList from './patterns-list';
Expand All @@ -21,7 +21,7 @@ const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis );

export default function PagePatterns() {
const { categoryType, categoryId } = getQueryArgs( window.location.href );
const type = categoryType || PATTERN_DEFAULT_POST_TYPE;
const type = categoryType || PATTERN_POST_TYPE;
const category = categoryId || PATTERN_DEFAULT_CATEGORY;
const settings = usePatternSettings();

Expand Down
32 changes: 18 additions & 14 deletions packages/edit-site/src/components/template-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import { store as reusableBlocksStore } from '@wordpress/reusable-blocks';
import { store as editSiteStore } from '../../store';
import isTemplateRemovable from '../../utils/is-template-removable';
import isTemplateRevertable from '../../utils/is-template-revertable';
import RenameTemplate from './rename-menu-item';
import RenameMenuItem from './rename-menu-item';
import {
TEMPLATE_POST_TYPE,
TEMPLATE_PART_P0ST_TYPE,
PATTERN_DEFAULT_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
PATTERN_POST_TYPE,
POST_TYPE_LABELS,
} from '../../utils/constants';

Expand All @@ -49,14 +49,17 @@ export default function TemplateActions( {
const { __experimentalDeleteReusableBlock } =
useDispatch( reusableBlocksStore );
const isRemovable = isTemplateRemovable( record );

const isUserPattern = record?.type === PATTERN_DEFAULT_POST_TYPE;
// Only custom patterns or custom template parts can be renamed or deleted.
const isTemplate =
record?.type === TEMPLATE_POST_TYPE ||
record?.type === TEMPLATE_PART_P0ST_TYPE;

if ( ! isTemplate && ! isRemovable && ! isUserPattern ) {
const isUserPattern = record?.type === PATTERN_POST_TYPE;
const isTemplate = record?.type === TEMPLATE_POST_TYPE;
const isTemplatePart = record?.type === TEMPLATE_PART_POST_TYPE;

if (
! isRemovable &&
! isTemplatePart &&
! isTemplate &&
! isUserPattern
) {
return null;
}

Expand Down Expand Up @@ -113,7 +116,8 @@ export default function TemplateActions( {
const fallbackErrorMessage = sprintf(
// translators: %s is a post type label, e.g., Template, Template Part or Pattern.
__( 'An error occurred while reverting the %s.' ),
POST_TYPE_LABELS[ postType ] ?? POST_TYPE_LABELS.wp_template
POST_TYPE_LABELS[ postType ] ??
POST_TYPE_LABELS[ TEMPLATE_POST_TYPE ]
);

const errorMessage =
Expand Down Expand Up @@ -142,20 +146,20 @@ export default function TemplateActions( {
<MenuGroup>
{ isEditable && (
<>
<RenameTemplate
<RenameMenuItem
postId={ postId }
postType={ postType }
onClose={ onClose }
/>

<DeleteMenuItem
isDestructive={ true }
onRemove={ () => {
deleteItem( record );
onRemove?.();
onClose();
} }
title={ decodeEntities(
record.title.rendered
record.title.rendered || record.title.raw
) }
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { decodeEntities } from '@wordpress/html-entities';
*/
import {
TEMPLATE_POST_TYPE,
TEMPLATE_PART_P0ST_TYPE,
PATTERN_DEFAULT_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
PATTERN_POST_TYPE,
TEMPLATE_CUSTOM_SOURCE,
POST_TYPE_LABELS,
} from '../../utils/constants';
Expand All @@ -43,8 +43,8 @@ export default function RenameMenuItem( { postType, postId, onClose } ) {
useDispatch( noticesStore );

const isTemplate = record?.type === TEMPLATE_POST_TYPE;
const isTemplatePart = record?.type === TEMPLATE_PART_P0ST_TYPE;
const isUserPattern = record?.type === PATTERN_DEFAULT_POST_TYPE;
const isTemplatePart = record?.type === TEMPLATE_PART_POST_TYPE;
const isUserPattern = record?.type === PATTERN_POST_TYPE;

if (
( isTemplate || isTemplatePart ) &&
Expand Down Expand Up @@ -80,9 +80,11 @@ export default function RenameMenuItem( { postType, postId, onClose } ) {

createSuccessNotice(
sprintf(
// translators: %s is a post type label, e.g., Template, Template Part or Pattern.
__( '%s renamed.' ),
POST_TYPE_LABELS[ postType ] ?? POST_TYPE_LABELS.wp_template
// translators: %1$s is a post type label, e.g., Template, Template Part or Pattern. %2$s is the new title.
__( '%1$s renamed to "%2$s".' ),
POST_TYPE_LABELS[ postType ] ??
POST_TYPE_LABELS.wp_template,
editedTitle
),
{
type: 'snackbar',
Expand Down
13 changes: 6 additions & 7 deletions packages/edit-site/src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ export const NAVIGATION_POST_TYPE = 'wp_navigation';

// Templates.
export const TEMPLATE_POST_TYPE = 'wp_template';
export const TEMPLATE_PART_P0ST_TYPE = 'wp_template_part';
export const TEMPLATE_PART_POST_TYPE = 'wp_template_part';
export const TEMPLATE_CUSTOM_SOURCE = 'custom';

// Patterns.
export const PATTERN_DEFAULT_POST_TYPE = 'wp_block';
export const PATTERN_POST_TYPE = 'wp_block';
export const PATTERN_DEFAULT_CATEGORY = 'my-patterns';
export const PATTERN_USER_CATEGORY = 'my-patterns';
export const PATTERN_THEME_TYPE = 'pattern';
export const PATTERN_CORE_SOURCES = [
'core',
Expand All @@ -29,14 +28,14 @@ export const PATTERN_SYNC_STATUSES = {

// Entities that are editable in focus mode.
export const FOCUSABLE_ENTITIES = [
TEMPLATE_PART_P0ST_TYPE,
TEMPLATE_PART_POST_TYPE,
NAVIGATION_POST_TYPE,
PATTERN_DEFAULT_POST_TYPE,
PATTERN_POST_TYPE,
];

export const POST_TYPE_LABELS = {
[ TEMPLATE_POST_TYPE ]: __( 'Template' ),
[ TEMPLATE_PART_P0ST_TYPE ]: __( 'Template Part' ),
[ PATTERN_DEFAULT_POST_TYPE ]: __( 'Pattern' ),
[ TEMPLATE_PART_POST_TYPE ]: __( 'Template Part' ),
[ PATTERN_POST_TYPE ]: __( 'Pattern' ),
[ NAVIGATION_POST_TYPE ]: __( 'Navigation' ),
};

0 comments on commit b1cfcc0

Please sign in to comment.