Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 62 additions & 56 deletions src/blocks/blocks/posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,67 @@ import { styles } from './constants.js';

const { attributes: defaultAttributes } = metadata;

const Preview = ({
posts,
categoriesList,
authors,
blockProps,
inlineStyles,
attributes,
isLoading
}) => {
if ( ! posts || ! categoriesList || ! authors || isLoading ) {
return (
<div { ...blockProps }>
<Placeholder>
<Spinner />
{ __( 'Loading Posts', 'otter-blocks' ) }
</Placeholder>
</div>
);
}

if ( 0 === posts.length ) {
return (
<div { ...blockProps }>
<Placeholder>
{ __( 'No Posts', 'otter-blocks' ) }
</Placeholder>
</div>
);
}
const featuredPost = posts?.[0];
const featuredPostAuthor = authors?.find(
( author ) => author?.id === featuredPost?.author
);

return (
<div { ...blockProps } style={ inlineStyles }>
<Disabled>
{ attributes.enableFeaturedPost && (
<FeaturedPost
attributes={ attributes }
post={ featuredPost }
categoriesList={ categoriesList }
author={ featuredPostAuthor }
/>
) }

<Layout
attributes={ attributes }
posts={ posts }
categoriesList={ categoriesList }
authors={ authors }
/>

</Disabled>
{
attributes.hasPagination && <PaginationPreview />
}
</div>
);
};

/**
* Posts component
* @param {import('./types').PostProps} param0
Expand Down Expand Up @@ -307,62 +368,6 @@ const Edit = ({

const blockProps = useBlockProps();

const Preview = ({
posts,
categoriesList,
authors,
blockProps,
inlineStyles,
attributes
}) => {
if ( ! posts || ! categoriesList || ! authors || isLoading ) {
return (
<div { ...blockProps }>
<Placeholder>
<Spinner />
{ __( 'Loading Posts', 'otter-blocks' ) }
</Placeholder>
</div>
);
}

if ( 0 === posts.length ) {
return (
<div { ...blockProps }>
<Placeholder>
{ __( 'No Posts', 'otter-blocks' ) }
</Placeholder>
</div>
);
}

return (
<div { ...blockProps } style={ inlineStyles }>
<Disabled>
{ attributes.enableFeaturedPost && (
<FeaturedPost
attributes={ attributes }
post={ posts?.[0] }
categoriesList={ categoriesList }
author={ authors[0] }
/>
) }

<Layout
attributes={ attributes }
posts={ posts }
categoriesList={ categoriesList }
authors={ authors }
/>

</Disabled>
{
attributes.hasPagination && <PaginationPreview />
}
</div>
);
};

return (
<Fragment>
{ categoriesList && (
Expand All @@ -386,6 +391,7 @@ const Edit = ({
blockProps={ blockProps }
inlineStyles={ inlineStyles }
attributes={ attributes }
isLoading={ isLoading }
/>
</Fragment>
);
Expand Down
Loading