This repository was archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
Product Details Block > Fix the review form rating #8431
Open
nefeline
wants to merge
28
commits into
trunk
Choose a base branch
from
fix/duplicated-star-ratings
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
d46e480
Add minimum structure for Single Product Details block
thealexandrelara e743ebe
Add tests for Single Product Details block
thealexandrelara f87de21
wip: create block structure and add initial styles
thealexandrelara 561f6f8
Add block details to the SingleProductDetails.php file
thealexandrelara d07b9b5
Render tabs title with empty content
thealexandrelara 9c90f71
Use woocommerce_output_product_data_tabs function to retrieve tabs data
thealexandrelara eb0ce4a
Remove customizations for the Single Product Details block
thealexandrelara 22be4e9
Merge branch 'trunk' into feat/add-single-product-details-block
thealexandrelara 2a4baad
Remove unnecessary console.log from the Edit.tsx file
thealexandrelara 8e05b71
Remove block classname from block wrapper
thealexandrelara f70ab0c
Remove unnecessary WooCommerce tabs filter from the BlockTemplatesCon…
thealexandrelara 4ea37cb
Merge branch 'trunk' into feat/add-single-product-details-block
thealexandrelara ad70695
Remove attributes property from the block registration
thealexandrelara fd83c09
Remove isExperimental flag for the Single Product Details block
thealexandrelara 3661133
Remove get_classes_and_styles_by_attributes method from SingleProduct…
thealexandrelara 8a9196e
Prevent Single Product Details block from apppearing in Pages or Posts
thealexandrelara edcac21
Fix PHP Coding Standards warnings
thealexandrelara 528b9ce
Merge branch 'trunk' into feat/add-single-product-details-block
gigitux 0ba35e1
update block name
gigitux af09b77
Merge branch 'feat/add-single-product-details-block' of https://githu…
gigitux 93bcb28
fix SCSS linter error
gigitux 7742ffe
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux 96fab80
move blocks into product-elements folder and rename to product-details
gigitux 001031c
Ensure the review form is properly rendered when the Product Details …
nefeline dfe1e17
Merge branch 'trunk' into fix/duplicated-star-ratings
nefeline 5aa5d50
Merge branch 'trunk' into fix/duplicated-star-ratings
nefeline d8acb43
Hide extra review stars via CSS
nefeline 96617ea
udpate doc
nefeline File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
assets/js/atomic/blocks/product-elements/product-details/block.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "woocommerce/product-details", | ||
"version": "1.0.0", | ||
"icon": "info", | ||
"title": "Product Details", | ||
"description": "A block that allows your customers to see details and reviews about the product.", | ||
"category": "woocommerce", | ||
"keywords": [ "WooCommerce" ], | ||
"supports": {}, | ||
"attributes": {}, | ||
"textdomain": "woo-gutenberg-products-block", | ||
"apiVersion": 2, | ||
"$schema": "https://schemas.wp.org/trunk/block.json" | ||
} |
95 changes: 95 additions & 0 deletions
95
assets/js/atomic/blocks/product-elements/product-details/block.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
interface SingleProductTab { | ||
id: string; | ||
title: string; | ||
active: boolean; | ||
content: string | undefined; | ||
} | ||
|
||
const ProductTabTitle = ( { | ||
id, | ||
title, | ||
active, | ||
}: Pick< SingleProductTab, 'id' | 'title' | 'active' > ) => { | ||
return ( | ||
<li | ||
className={ classnames( `${ id }_tab`, { | ||
active, | ||
} ) } | ||
id={ `tab-title-${ id }` } | ||
role="tab" | ||
aria-controls={ `tab-${ id }` } | ||
> | ||
<a href={ `#tab-${ id }` }>{ title }</a> | ||
</li> | ||
); | ||
}; | ||
|
||
const ProductTabContent = ( { | ||
id, | ||
content, | ||
}: Pick< SingleProductTab, 'id' | 'content' > ) => { | ||
return ( | ||
<div | ||
className={ `${ id }_tab` } | ||
id={ `tab-title-${ id }` } | ||
role="tab" | ||
aria-controls={ `tab-${ id }` } | ||
> | ||
{ content } | ||
</div> | ||
); | ||
}; | ||
|
||
export const SingleProductDetails = () => { | ||
const blockProps = useBlockProps(); | ||
const productTabs = [ | ||
{ | ||
id: 'description', | ||
title: 'Description', | ||
active: true, | ||
content: __( | ||
'This block lists description, attributes and reviews for a single product.', | ||
'woo-gutenberg-products-block' | ||
), | ||
}, | ||
{ | ||
id: 'additional_information', | ||
title: 'Additional Information', | ||
active: false, | ||
}, | ||
{ id: 'reviews', title: 'Reviews', active: false }, | ||
]; | ||
const tabsTitle = productTabs.map( ( { id, title, active } ) => ( | ||
<ProductTabTitle | ||
key={ id } | ||
id={ id } | ||
title={ title } | ||
active={ active } | ||
/> | ||
) ); | ||
const tabsContent = productTabs.map( ( { id, content } ) => ( | ||
<ProductTabContent key={ id } id={ id } content={ content } /> | ||
) ); | ||
|
||
return ( | ||
<div { ...blockProps }> | ||
<ul className="tabs" role="tablist"> | ||
{ tabsTitle } | ||
</ul> | ||
{ tabsContent } | ||
</div> | ||
); | ||
}; | ||
|
||
export default SingleProductDetails; |
31 changes: 31 additions & 0 deletions
31
assets/js/atomic/blocks/product-elements/product-details/edit.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
import { Disabled } from '@wordpress/components'; | ||
import type { BlockEditProps } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Block from './block'; | ||
import { Attributes } from './types'; | ||
|
||
const Edit = ( { attributes }: BlockEditProps< Attributes > ) => { | ||
const { className } = attributes; | ||
const blockProps = useBlockProps( { | ||
className, | ||
} ); | ||
|
||
return ( | ||
<> | ||
<div { ...blockProps }> | ||
<Disabled> | ||
<Block /> | ||
</Disabled> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default Edit; |
24 changes: 24 additions & 0 deletions
24
assets/js/atomic/blocks/product-elements/product-details/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { registerBlockSingleProductTemplate } from '@woocommerce/atomic-utils'; | ||
import { registerBlockType, unregisterBlockType } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
|
||
registerBlockSingleProductTemplate( { | ||
registerBlockFn: () => { | ||
// @ts-expect-error: `registerBlockType` is a function that is typed in WordPress core. | ||
registerBlockType( metadata, { | ||
edit, | ||
} ); | ||
}, | ||
unregisterBlockFn: () => { | ||
unregisterBlockType( metadata.name ); | ||
}, | ||
blockName: metadata.name, | ||
} ); |
46 changes: 46 additions & 0 deletions
46
assets/js/atomic/blocks/product-elements/product-details/style.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
.wp-block-woocommerce-product-details { | ||
ul.tabs { | ||
list-style: none; | ||
padding: 0 0 0 1em; | ||
margin: 0 0 1.618em; | ||
overflow: hidden; | ||
position: relative; | ||
border-bottom: 1px solid $gray-200; | ||
|
||
li { | ||
border: 1px solid $gray-200; | ||
background-color: $white; | ||
display: inline-block; | ||
position: relative; | ||
z-index: 0; | ||
border-radius: 4px 4px 0 0; | ||
margin: 0; | ||
padding: 0.5em 1em; | ||
opacity: 0.5; | ||
|
||
a { | ||
display: inline-block; | ||
font-weight: 700; | ||
color: $black; | ||
text-decoration: none; | ||
|
||
&:hover { | ||
text-decoration: none; | ||
color: color.adjust($black, $lightness: 10%); | ||
} | ||
} | ||
|
||
&.active { | ||
background: $gray-100; | ||
z-index: 2; | ||
border-bottom-color: $gray-200; | ||
opacity: 1; | ||
|
||
a { | ||
color: inherit; | ||
text-shadow: inherit; | ||
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
assets/js/atomic/blocks/product-elements/product-details/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Attributes { | ||
className?: string; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Automattic\WooCommerce\Blocks\BlockTypes; | ||
|
||
/** | ||
* ProductDetails class. | ||
*/ | ||
class ProductDetails extends AbstractBlock { | ||
/** | ||
* Block name. | ||
* | ||
* @var string | ||
*/ | ||
protected $block_name = 'product-details'; | ||
|
||
/** | ||
* Render the block. | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block content. | ||
* @param WP_Block $block Block instance. | ||
* | ||
* @return string Rendered block output. | ||
*/ | ||
protected function render( $attributes, $content, $block ) { | ||
$tabs = $this->render_tabs(); | ||
|
||
$classname = $attributes['className'] ?? ''; | ||
|
||
return sprintf( | ||
'<div class="wp-block-woocommerce-single-product-details %1$s"> | ||
%2$s | ||
</div>', | ||
esc_attr( $classname ), | ||
$tabs | ||
); | ||
} | ||
|
||
/** | ||
* Gets the tabs with their content to be rendered by the block. | ||
* | ||
* @return string The tabs html to be rendered by the block | ||
*/ | ||
protected function render_tabs() { | ||
// This filter ensures the review form is properly rendered when the Product Details block is used alongside | ||
// the Classic Single Product Block. It can be removed as soon as the Classic Single Product Block is deprecated. | ||
add_filter( 'woocommerce_product_review_comment_form_args', array( $this, 'review_comment_form' ), 0 ); | ||
|
||
ob_start(); | ||
|
||
while ( have_posts() ) { | ||
the_post(); | ||
woocommerce_output_product_data_tabs(); | ||
} | ||
|
||
return ob_get_clean(); | ||
} | ||
|
||
/** | ||
* Ensures the review form is properly rendered when the Product Details block is used alongside the Classic Single Product Block. | ||
* | ||
* This method can be removed as soon as the Classic Single Product Block is deprecated. | ||
* | ||
* @param array $comment_form The comment form arguments. | ||
* | ||
* @return array The updated comment form arguments. | ||
*/ | ||
public function review_comment_form( $comment_form ) { | ||
$comment_form['comment_field'] = '<div class="comment-form-rating"><label for="rating">' . esc_html__( 'Your rating', 'woo-gutenberg-products-block' ) . ( wc_review_ratings_required() ? ' <span class="required">*</span>' : '' ) . '</label><select name="rating" id="rating-select" required> | ||
<option value="">' . esc_html__( 'Rate…', 'woo-gutenberg-products-block' ) . '</option> | ||
<option value="5">' . esc_html__( 'Perfect', 'woo-gutenberg-products-block' ) . '</option> | ||
<option value="4">' . esc_html__( 'Good', 'woo-gutenberg-products-block' ) . '</option> | ||
<option value="3">' . esc_html__( 'Average', 'woo-gutenberg-products-block' ) . '</option> | ||
<option value="2">' . esc_html__( 'Not that bad', 'woo-gutenberg-products-block' ) . '</option> | ||
<option value="1">' . esc_html__( 'Very poor', 'woo-gutenberg-products-block' ) . '</option> | ||
</select></div>'; | ||
|
||
$comment_form['comment_field'] .= '<p class="comment-form-comment"><label for="comment">' . esc_html__( 'Your review', 'woo-gutenberg-products-block' ) . ' <span class="required">*</span></label><textarea id="comment" name="comment" cols="45" rows="8" required></textarea></p>'; | ||
|
||
return $comment_form; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
tests/e2e/config/custom-matchers/__fixtures__/single-product-details.fixture.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"title":"Product Details Block","pageContent":"<!-- wp:woocommerce/single-product-details /-->"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { getAllBlocks, switchUserToAdmin } from '@wordpress/e2e-test-utils'; | ||
import { visitBlockPage } from '@woocommerce/blocks-test-utils'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { insertBlockDontWaitForInsertClose } from '../../utils.js'; | ||
|
||
const block = { | ||
name: 'Product Details', | ||
slug: 'woocommerce/single-product-details', | ||
class: '.wc-block-single-product-details', | ||
}; | ||
|
||
describe( `${ block.name } Block`, () => { | ||
beforeAll( async () => { | ||
await switchUserToAdmin(); | ||
await visitBlockPage( `${ block.name } Block` ); | ||
} ); | ||
|
||
it( 'can be inserted more than once', async () => { | ||
await insertBlockDontWaitForInsertClose( block.name ); | ||
expect( await getAllBlocks() ).toHaveLength( 2 ); | ||
} ); | ||
|
||
it( 'renders without crashing', async () => { | ||
await expect( page ).toRenderBlock( block ); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can fix the stars issue, I think that we should add this logic too.