woocommerce/plugins/woocommerce-blocks/assets/js/blocks
Manish Menaria 2433664aa8
Product Collection - Show product picker in Editor when collection requires a product but it doesn't exist (#50164)
* Show product picker control in the editor when a product context is required but not provided

Enhanced the Product Collection block by introducing the `selectedReference` attribute and implementing a product picker control. This control appears in the editor when a product context is required but not provided in the current template/page/post.

1. **block.json**: Added `selectedReference` attribute of type `object`.
2. **constants.ts**: Included `selectedReference` in the `queryContextIncludes` array.
3. **EditorProductPicker.tsx**: Created a new component for selecting products within the editor.
4. **editor.scss**: Added styles for the new Editor Product Picker component.
5. **index.tsx**: Updated logic to determine the component to render, incorporating the new Editor Product Picker.
6. **types.ts**: Defined types for `selectedReference` and updated `ProductCollectionAttributes` interface.

This enhancement allows merchants to manually select a product for collections that require a product context, ensuring the block displays correctly even when the product context is not available in the current template/page/post.

- **Product Picker Control**: Utilizes the existing `ProductControl` component for selecting products. This component is displayed in the editor when a collection requires a product context but it doesn't exist in the current template/page/post.

* Update label on ProductControl component

* Implement dynamic UI state management for product collection block

- Introduced `ProductCollectionUIStatesInEditor` enum to define various UI states for the product collection block.
- Added `getProductCollectionUIStateInEditor` utility function to determine the appropriate UI state based on context.
- Updated `Edit` component to use `getProductCollectionUIStateInEditor` for dynamic state management.
- Refactored `ProductCollectionContent` to utilize the new Editor UI state management.

* Fix: Product picker isn't showing

* Fix: Preview label state isn't showing

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

* Refactor WooCommerceBlockLocation type

- Introduced specific interfaces for WooCommerceBlockLocation, including ProductLocation, ArchiveLocation, CartLocation, OrderLocation, and SiteLocation, to improve type safety and code clarity.
- Updated createLocationObject function to return a BaseLocation type.
- Refactored useSetPreviewState hook in product-collection utils:
  - Extracted termId from location.sourceData for cleaner and more readable code.
  - Replaced direct access of location.sourceData?.termId with termId variable.

* Remove fallback to 0 in case there may be a product with id 0

* Use optional chaining to avoid undefined errors

* Rename  to

* Change order of arguments in  function

* Pass boolean prop instead of making further recognition of the UI state in ProductCollectionContent

* Destructure  props in  component

* Rename  to

* Update names in  enum

* Rename  to  and change the structure to single number.

* Rename location to

* Add a method to choose a product in the product picker in Editor

* Add E2E tests

* Fix failing e2e tests by changing location to productCollectionLocation

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

* Don't allow selecting product variations

* Minor code refactoring

* Fix: Product control isn't showing products

**Before**
```tsx
const getRenderItemFunc = () => {
		if ( renderItem ) {
			return renderItem;
		} else if ( showVariations ) {
			return renderItemWithVariations;
		}
    return () => null;
	};
```

As you can see above, `return () => null;` is returning a function which is causing the issue. This will render nothing in the list. I changed this to `return undefined;`. This way, we will use default render item function.

* Translate text in ProductPicker component

* Improve E2E test

* Use createInterpolateElement to safely render strong HTML tag

* Fix E2E tests

* Fix E2E tests

* Product Collection: Inspector control to change selected product (#50590)

* Add Linked Product Control to Product Collection Block Inspector Controls

- Introduced a new `LinkedProductControl` component in the Product Collection block's Inspector Controls.
- This control allows users to link a specific product to the product collection via a dropdown with a search capability.
- Added corresponding styles to `editor.scss`.
- Integrated a `useGetProduct` hook in the `utils.tsx` to fetch and manage the state of the linked product data, including handling loading states and errors.
- Updated the Inspector Controls to include the new Linked Product Control component, enhancing the block's customization options for users.

* Add E2E tests

* Hide product picker when product context is available

* Improve logic to hide Linked Product Control

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

* Remove hasError state from useGetProduct hook

* Rename isShowLinkedProductControl to showLinkedProductControl

* Convert jsxProductButton to ProductButton component

* Refactor jsxPopoverContent to LinkedProductPopoverContent component

* Improve UI of Linked Product Control

* Address PR feedback

* Fix E2E tests

---------

Co-authored-by: github-actions <github-actions@github.com>

* Rename isUsesReferencePreviewMode to isUsingReferencePreviewMode

* Change order of conditions in getProductCollectionUIStateInEditor

---------

Co-authored-by: github-actions <github-actions@github.com>
2024-09-02 12:39:33 +05:30
..
active-filters Remove extra bottom padding in filters in the editor (#51012) 2024-08-29 10:26:11 +02:00
attribute-filter Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
breadcrumbs Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
cart Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
cart-checkout-shared Small updates to the express checkout area (#50644) 2024-08-28 11:16:55 +01:00
catalog-sorting Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
checkout Fix most of lint warnings (#50869) 2024-08-23 14:30:14 +02:00
classic-shortcode Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
classic-template Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
coming-soon Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
customer-account Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
featured-items Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
filter-wrapper Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
handpicked-products Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
migration-products-to-product-collection Product Collection: Remove automatic migration code (#50440) 2024-08-13 11:29:07 +02:00
mini-cart Fix most of lint warnings (#50869) 2024-08-23 14:30:14 +02:00
order-confirmation Order confirmation: Update mobile order summary styling so items appear side by side (#50722) 2024-08-23 19:22:59 +01:00
page-content-wrapper Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
price-filter Fix typos. (#50047) 2024-08-23 18:26:09 +01:00
product-best-sellers Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
product-categories Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
product-category Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
product-collection Product Collection - Show product picker in Editor when collection requires a product but it doesn't exist (#50164) 2024-09-02 12:39:33 +05:30
product-filters [Experimental] Update folder structure for new product filter blocks (#51029) 2024-08-29 15:23:40 +00:00
product-gallery Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
product-new Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
product-on-sale Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
product-query Hide Products (Beta) from inserter (#48204) 2024-06-12 15:45:27 +02:00
product-results-count Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
product-search Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
product-tag Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
product-template Product Collection - Show product picker in Editor when collection requires a product but it doesn't exist (#50164) 2024-09-02 12:39:33 +05:30
product-top-rated Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
products Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
products-by-attribute Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
rating-filter Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
reviews Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
shared/styles Add logic of Upgrade Notice after upgrading Products to Product Collection (https://github.com/woocommerce/woocommerce-blocks/pull/10267) 2023-08-18 08:25:15 +02:00
single-product Fix variation selector display issues on the front end (#51023) 2024-08-30 17:47:20 -03:00
stock-filter Remove extra bottom padding in filters in the editor (#51012) 2024-08-29 10:26:11 +02:00
store-notices Update all blocks to V3 (#48720) 2024-08-20 10:14:11 +00:00
README.md Remove propTypes definitions from Newest Products block (https://github.com/woocommerce/woocommerce-blocks/pull/9613) 2023-05-29 13:12:36 +07:00

README.md

Blocks

Our blocks are generally made up of up to 4 files:

|- block.js
|- editor.scss
|- index.js
|- style.scss

The only required file is index.js, this sets up the block using registerBlockType. Each block has edit and save functions.

The scss files are split so that things in style are added to the editor and frontend, while styles in editor are only added to the editor. Most of our blocks should use core components that won't need CSS though.

Editing

A simple edit function can live in index.js, but most blocks are a little more complicated, so the edit function instead returns a Block component, which lives in block.js. By using a component, we can use React lifecycle methods to fetch data or save state.

The Newest Products block is a good example to read over, this is a simple block that fetches the products and renders them using the ProductPreview component.

We include settings in the sidebar, called the Inspector in gutenberg. See an example of this.

Other blocks have the concept of an "edit state", like when you need to pick a product in the Featured Product block, or pick a category in the Products by Category block.