Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product Block Editor: Map SelectWithTextInput and DateTime inputs to the custom product blocks #2178

Conversation

eason9487
Copy link
Member

@eason9487 eason9487 commented Dec 7, 2023

Changes proposed in this Pull Request:

To be compatible with Product Block Editor, this PR makes the conversions for the last two attribute inputs:

  • Implement the custom blocks for the product select-with-text field and product date-time field.
  • Map SelectWithTextInput and DateTime inputs to the above custom product blocks.
  • Set the minimum required Woo version of AttributesBlock class to 8.4 as it's the minimum version that the __experimentalTextControl component is exported from @woocommerce/product-editor.

Screenshots:

📷 Field is converted from text block to select-with-text block

image

image

📷 Date-time block

image

image

image

Detailed test instructions:

📌 Prepare test environment

  1. Please refer to the test preparation in PR 2151 with Woo 8.4.0.
  2. npm install
  3. npm start

📌 Test the select-with-text block

  1. Install WooCommerce Brands but don't activate it.
  2. Go to edit a simple product, e.g. the Beanie with Logo imported from WC's sample products.
  3. Switch to the Organization tab and find the Google Listings & Ads section.
  4. The brand field should be shown in a text block.
  5. Activate WooCommerce Brands and refresh the product editor page.
  6. Check if the brand field is shown in a select-with-text block with three options:
    1. "Default"
    2. "From WooCommerce Brands"
    3. "Enter a custom value"
  7. Change the selected option to see if the UI works well.
  8. After selecting the "Enter a custom value" option, a text field should be shown below the selection.
  9. Operate the selection and text fields.
  10. Click on the Update button at the top-right of the page.
  11. Refresh the page or switch back to the classical product editor to see if the changed option or text is saved.
  12. The GTIN and MPN will be converted to use the select-with-text block when Yoast WooCommerce SEO is installed and activated.

📌 Test the date-time block

  1. Go to Settings > General. Set the site's timezone to another one different from your machine's system time, which is also the local timezone of browsers.
  2. Go to edit a simple product that has not yet set an availability date.
  3. Switch to the Organization tab and find the Google Listings & Ads section.
  4. Check if there is an Availability Date field shown in a date-time block.
  5. Operate the date-time block to set a date.
  6. Click on the Update button at the top-right of the page.
  7. Refresh the page to see if the availability date is saved.
  8. Switch back to the classical product editor to see if the values on the availability date field are the same.
  9. Set the site's timezone to another or the same as your machine.
  10. Refresh and check both classical and blockified product editors to see if the date values are shifted as per the delta of the two timezones.

Additional details:

TODOs will be cleaned up in a separate PR.

Changelog entry

@eason9487 eason9487 self-assigned this Dec 7, 2023
@github-actions github-actions bot added changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement. labels Dec 7, 2023
Copy link

codecov bot commented Dec 7, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (5e1f98d) 60.8% compared to head (0fb9cf0) 60.9%.
Report is 2 commits behind head on feature/support-product-block-editor.

Additional details and impacted files

Impacted file tree graph

@@                           Coverage Diff                            @@
##             feature/support-product-block-editor   #2178     +/-   ##
========================================================================
+ Coverage                                    60.8%   60.9%   +0.1%     
- Complexity                                   4151    4153      +2     
========================================================================
  Files                                         453     453             
  Lines                                       17824   17843     +19     
========================================================================
+ Hits                                        10843   10865     +22     
+ Misses                                       6981    6978      -3     
Flag Coverage Δ
php-unit-tests 60.9% <100.0%> (+0.1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
src/Admin/Input/DateTime.php 73.5% <100.0%> (ø)
src/Admin/Input/SelectWithTextInput.php 100.0% <100.0%> (ø)
src/Admin/Product/Attributes/AttributesBlock.php 94.2% <100.0%> (-0.7%) ⬇️

... and 1 file with indirect coverage changes

@eason9487 eason9487 requested a review from a team December 7, 2023 09:37
Copy link
Contributor

@puntope puntope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @eason9487 for working on this. Overall looking very good.

I left some comments in the code and a warning in regards the functionallity.

- In block.json, it needs to list `"postType"` in the `usesContext` array to ask for the current post type ('product' or 'product_variation') from the context provider.
- Forwarding the `context.postType` to `useProductEntityProp` hook is required in order to be compatible with both simple and variation product block templates.

#### Derived value for initialization
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Can we elaborate or reword this explanation? Maybe my lack of knowledge of the concepts but not sure if I understand what those "derived values" are. I assume is just the data itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the React ecosystem, the derived value/state is commonly referred to the value calculated based on another value. I'm guessing that it probably comes from the wording in the React API getDerivedStateFromProps.

Added in 0965ef9.

Comment on lines 9 to 10
import DatetimeFieldEdit from './product-date-time-field/edit';
import datetimeFieldMetadata from './product-date-time-field/block.json';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should be DateTimeFieldEdit and dateTimeFieldMetadata because there is a - between date and time in the folder name. Another option may be to call the folder product-datetime-field.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed in b94f33a.

"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "google-listings-and-ads/product-date-time-field",
"title": "Product datetime control",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💅 Minor: can we keep it consistent with the block name conventions you are using?

So in this case:

"Product datetime field."

I don't mind control or field, but I think is better to call them in one single way. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted in 776ffb8.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💅 WDYT of renaming value, setValue, setNextValue to dateTime, setDateTime, setNextDateTime

So it's less generic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of value and setValue naming is to maintain consistency in the state and updater names that actually operate to the product data (wp-store) in each custom block.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ I found some inconsistencies between both legacy and new editor:

In the legacy Editor you can set the date without time and it saves the time as 00:00.

Screen.Recording.2023-12-15.at.15.28.51.mov

In the new editor, you cannot update til you update the time. If you edit only the date and also another field to allow the update button, the changes are not saved.

Screen.Recording.2023-12-15.at.15.24.56.mov

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding this! Adjusted in b916bd3.

"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "google-listings-and-ads/product-select-with-text-field",
"title": "Product select control with text control",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💅 In line with the other block. WDYT to keep is as just Fields?

"Product Select with Text Field"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted in 776ffb8.

Comment on lines +47 to +51
if ( nextOptionValue === customInputValue ) {
setValue( text );
} else {
setValue( nextOptionValue );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💅 This can be reduced to:

setValue( nextOptionValue === customInputValue ? text : nextOptionValue );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lead toward keeping it as it is.


const FALLBACK_VALUE = '';

export default function Edit( { attributes, context } ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💅 Can we add a bit of documentation of what customInputValue attribute is for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added JSDoc for all custom blocks by d8252b8.

Base automatically changed from add/product-block-editor-custom-block-infra to feature/support-product-block-editor December 25, 2023 10:58
@eason9487
Copy link
Member Author

@puntope, thanks for the code review. This PR is ready for the new round. Could you take another look?

…block-editor-custom-select-with-text-and-date-time
Comment on lines +24 to +30
async function resolveValidationMessage( inputRef ) {
const input = inputRef.current;

if ( ! input.validity.valid ) {
return input.validationMessage;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need validation for the datetime? I wasn't able to set an invalid date time in the inputs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't test all browsers, but Chrome and FireFox both allow entering date by keyboard, which may result in an invalid value.

Kapture.2024-01-03.at.11.23.23.mp4

"請輸入有效的日期" and "請輸入有效的時間" mean "Please enter a valid date" and "Please enter a valid time".

Copy link
Contributor

@puntope puntope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM Thanks for the changes

I left tho a question in regards the validation.

@eason9487 eason9487 merged commit d50c151 into feature/support-product-block-editor Jan 3, 2024
15 checks passed
@eason9487 eason9487 deleted the add/product-block-editor-custom-select-with-text-and-date-time branch January 3, 2024 03:36
@eason9487 eason9487 mentioned this pull request Feb 27, 2024
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants