Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 7a548ac

Browse files
authored
Add featured category preview (#1122)
1 parent 93085f7 commit 7a548ac

File tree

6 files changed

+120
-18
lines changed

6 files changed

+120
-18
lines changed

assets/js/blocks/featured-category/block.js

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
MediaUploadCheck,
1212
PanelColorSettings,
1313
withColors,
14+
RichText,
1415
} from '@wordpress/editor';
1516
import {
1617
Button,
@@ -221,6 +222,50 @@ const FeaturedCategory = ( {
221222
);
222223
};
223224

225+
const renderButton = () => {
226+
const buttonClasses = classnames(
227+
'wp-block-button__link',
228+
'is-style-fill'
229+
);
230+
const buttonStyle = {
231+
backgroundColor: 'vivid-green-cyan',
232+
borderRadius: '5px',
233+
};
234+
const wrapperStyle = {
235+
width: '100%',
236+
};
237+
return attributes.categoryId === 'preview' ? (
238+
<div className="wp-block-button aligncenter" style={ wrapperStyle }>
239+
<RichText.Content
240+
tagName="a"
241+
className={ buttonClasses }
242+
href={ category.permalink }
243+
title={ attributes.linkText }
244+
style={ buttonStyle }
245+
value={ attributes.linkText }
246+
target={ category.permalink }
247+
/>
248+
</div>
249+
) : (
250+
<InnerBlocks
251+
template={ [
252+
[
253+
'core/button',
254+
{
255+
text: __(
256+
'Shop now',
257+
'woo-gutenberg-products-block'
258+
),
259+
url: category.permalink,
260+
align: 'center',
261+
},
262+
],
263+
] }
264+
templateLock="all"
265+
/>
266+
);
267+
};
268+
224269
const renderCategory = () => {
225270
const {
226271
className,
@@ -233,7 +278,7 @@ const FeaturedCategory = ( {
233278
const classes = classnames(
234279
'wc-block-featured-category',
235280
{
236-
'is-selected': isSelected,
281+
'is-selected': isSelected && attributes.productId !== 'preview',
237282
'is-loading': ! category && isLoading,
238283
'is-not-found': ! category && ! isLoading,
239284
'has-background-dim': dimRatio !== 0,
@@ -282,22 +327,7 @@ const FeaturedCategory = ( {
282327
/>
283328
) }
284329
<div className="wc-block-featured-category__link">
285-
<InnerBlocks
286-
template={ [
287-
[
288-
'core/button',
289-
{
290-
text: __(
291-
'Shop now',
292-
'woo-gutenberg-products-block'
293-
),
294-
url: category.permalink,
295-
align: 'center',
296-
},
297-
],
298-
] }
299-
templateLock="all"
300-
/>
330+
{ renderButton() }
301331
</div>
302332
</div>
303333
</ResizableBox>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { DEFAULT_HEIGHT } from '@woocommerce/block-settings';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import { previewCategories } from '@woocommerce/resource-previews';
10+
11+
export const example = {
12+
attributes: {
13+
contentAlign: 'center',
14+
dimRatio: 50,
15+
editMode: false,
16+
height: DEFAULT_HEIGHT,
17+
mediaSrc: '',
18+
showDesc: true,
19+
categoryId: 'preview',
20+
previewCategory: previewCategories[ 0 ],
21+
},
22+
};

assets/js/blocks/featured-category/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { DEFAULT_HEIGHT } from '@woocommerce/block-settings';
1212
import './style.scss';
1313
import './editor.scss';
1414
import Block from './block';
15+
import { example } from './example';
1516
import { IconFolderStar } from '@woocommerce/block-components/icons';
1617

1718
/**
@@ -33,6 +34,7 @@ registerBlockType( 'woocommerce/featured-category', {
3334
align: [ 'wide', 'full' ],
3435
html: false,
3536
},
37+
example,
3638
attributes: {
3739
/**
3840
* Alignment of content inside block.
@@ -125,6 +127,14 @@ registerBlockType( 'woocommerce/featured-category', {
125127
type: 'boolean',
126128
default: true,
127129
},
130+
131+
/**
132+
* Category preview.
133+
*/
134+
previewCategory: {
135+
type: 'object',
136+
default: null,
137+
},
128138
},
129139

130140
/**

assets/js/hocs/with-category.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ const withCategory = createHigherOrderComponent( ( OriginalComponent ) => {
1717
this.state = {
1818
error: null,
1919
loading: false,
20-
category: null,
20+
category:
21+
this.props.attributes.categoryId === 'preview'
22+
? this.props.attributes.previewCategory
23+
: null,
2124
};
2225
this.loadCategory = this.loadCategory.bind( this );
2326
}
@@ -38,6 +41,10 @@ const withCategory = createHigherOrderComponent( ( OriginalComponent ) => {
3841
loadCategory() {
3942
const { categoryId } = this.props.attributes;
4043

44+
if ( categoryId === 'preview' ) {
45+
return;
46+
}
47+
4148
if ( ! categoryId ) {
4249
this.setState( {
4350
category: null,

assets/js/previews/categories.js

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/previews/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { previewProducts } from './products';
22
export { previewReviews } from './reviews';
3+
export { previewCategories } from './categories';

0 commit comments

Comments
 (0)