Skip to content

Commit

Permalink
Blocks: Check for post context values before using data
Browse files Browse the repository at this point in the history
If used in the post editor, these are valid and reflect the current post. If used in the template editor, these are null, and the block should display a placeholder value. This will display the placeholder value in the template editor regardless of sessions (published or not) on the site.

Fixes #829, fixes #832
  • Loading branch information
ryelle committed Mar 17, 2023
1 parent eb97bf4 commit 4401a3d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@ import {
useBlockProps,
} from '@wordpress/block-editor';
import { Notice } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';

export default function Edit( { attributes, setAttributes, context: { postId, postType } } ) {
const { key, text, textAlign } = attributes;
const url = useSelect(
( select ) => {
const { getEntityRecord } = select( coreStore );
const post = getEntityRecord( 'postType', postType, postId );
return post.meta[ key ];
},
[ key ]
);
const [ meta = {} ] = useEntityProp( 'postType', postType, 'meta', postId );
const url = meta[ key ] || '';

const blockProps = useBlockProps( {
className: classnames( {
Expand All @@ -45,10 +38,13 @@ export default function Edit( { attributes, setAttributes, context: { postId, po
} }
/>
</BlockControls>
{ ! url && (
{ postId && postType && ! url && (
<InspectorControls>
<Notice status="error" isDismissible={ false }>
{ __( 'The link for this content is missing. Add the URL in the Session tab.', 'wordcamporg' ) }
{ __(
'The link for this content is missing. Add the URL in the Session tab.',
'wordcamporg'
) }
</Notice>
</InspectorControls>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ import { __, _x } from '@wordpress/i18n';
import { AlignmentControl, BlockControls, InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { dateI18n, __experimentalGetSettings as getDateSettings } from '@wordpress/date'; // eslint-disable-line @wordpress/no-unsafe-wp-apis
import { PanelBody, SelectControl, ToggleControl } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';

export default function Edit( { attributes, setAttributes, context: { postId, postType } } ) {
const { format, showTimezone, textAlign } = attributes;
const date = useSelect( ( select ) => {
const { getEntityRecord } = select( coreStore );
const session = getEntityRecord( 'postType', postType, postId );
return session.meta._wcpt_session_time * 1000; // Convert from s to ms.
}, [] );
const [ meta = {} ] = useEntityProp( 'postType', postType, 'meta', postId );
const date = meta._wcpt_session_time;

const { formats } = getDateSettings();
const defaultFormat = formats.datetime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ import {
useBlockProps,
} from '@wordpress/block-editor';
import { PanelBody, ToggleControl } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';

export default function( { attributes, setAttributes, context: { postId, postType }, isSelected } ) {
const { byline, isLink, textAlign } = attributes;
const speakers = useSelect( ( select ) => {
const { getEntityRecord } = select( coreStore );
const session = getEntityRecord( 'postType', postType, postId );
return session.session_speakers || [];
}, [] );
const [ speakers = [] ] = useEntityProp( 'postType', postType, 'session_speakers', postId );

const blockProps = useBlockProps( {
className: classnames( {
Expand Down Expand Up @@ -62,11 +57,17 @@ export default function( { attributes, setAttributes, context: { postId, postTyp
onChange={ ( value ) => setAttributes( { byline: value } ) }
/>
) }
{ speakers.map( ( { id, name, link } ) => (
<span key={ id } className="wp-block-wordcamp-session-speakers__name">
{ isLink ? <a href={ link }>{ name }</a> : name }
{ postType && postId ? (
speakers.map( ( { id, name, link } ) => (
<span key={ id } className="wp-block-wordcamp-session-speakers__name">
{ isLink ? <a href={ link }>{ name }</a> : name }
</span>
) )
) : (
<span className="wp-block-wordcamp-session-speakers__name">
{ __( 'Speaker Name', 'wordcamporg' ) }
</span>
) ) }
) }
</div>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ import { getSessionDetails, sortSessionByTime } from '../sessions/utils';
export default function( { attributes, setAttributes, context: { postId } } ) {
const { hasSessionDetails, isLink, textAlign } = attributes;
const sessions = useSelect( ( select ) => {
if ( ! postId ) {
return [
{
id: 1,
title: { rendered: 'Session Name' },
link: '#',
session_date_time: { date: 'November 1, 2023', time: '10:15 am' },
session_track: [],
},
];
}

const { getEntityRecords } = select( coreStore );
const _sessions =
getEntityRecords( 'postType', 'wcb_session', {
Expand Down

0 comments on commit 4401a3d

Please sign in to comment.