-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Adding attribute edit modal for products MVP #35269
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
Conversation
packages/js/components/src/experimental-select-control/select-control.tsx
Outdated
Show resolved
Hide resolved
plugins/woocommerce-admin/client/products/sections/attributes-section.tsx
Outdated
Show resolved
Hide resolved
f5d0839 to
bfde9f4
Compare
Test Results SummaryCommit SHA: 34f5ed5
To view the full API test report, click here. To view the full E2E test report, click here. To view all test reports, visit the WooCommerce Test Reports Dashboard. |
7cfc628 to
3e7dfb9
Compare
packages/js/components/src/experimental-select-control/select-control.tsx
Outdated
Show resolved
Hide resolved
plugins/woocommerce-admin/client/products/fields/attribute-field/attribute-field.tsx
Show resolved
Hide resolved
plugins/woocommerce-admin/client/products/fields/attribute-field/attribute-field.tsx
Show resolved
Hide resolved
3e7dfb9 to
17b5809
Compare
louwie17
left a comment
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.
Hey @joelclimbsthings, thanks for tackling this!
I left some comments in the code and also noticed a couple issues while testing:
- Neither of the Attribute or Attribute term dropdowns worked for me when trying to edit it. It would neither show new attribute terms or show any attributes when I emptied the text field
- The edit modal allowed me to remove all the terms, but it kept attribute listed under the main attributes list. Upon save that attribute disappeared.
I think in that instance we should also remove the attribute from the main list, and maybe notify the user that we did, maybe upon them clickingUpdatewe show a warning -Are you sure you want to save this, removing all the attribute terms from an attribute will remove the attributeor something like that?
Also the lint task failed with 11:10 ✖ Unexpected named color "gray" color-named for the edit-attribute-modal.scss file
packages/js/components/src/experimental-select-control/select-control.tsx
Outdated
Show resolved
Hide resolved
| id: formAttr.id, | ||
| name: formAttr.name, | ||
| } | ||
| : null |
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.
Why not just pass in formAttr if it contains id and name already?
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.
I actually went this direction since I was getting a TS error:

I'm not sure why this error is appearing, since I should be ensuring that id is defined with the ternary check, but the error went away when I narrowed the object. Do you know offhand why that might be happening? I wasn't sure I wanted to spend more time troubleshooting it when this solution worked.
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.
Hmm yeah, the number | undefined part comes from the fact that we are wrapping HydratedAttributeType with Partial in the AttributeForm type, which makes things less easy to handle (my bad).
If you change the attributes type in AttributeForm to: Array< HydratedAttributeType | { id: undefined; terms: [] } > it will more accurately represent the type and allow you to use formAttr.id ? formAttr : undefined for the value prop here.
| terms: undefined, | ||
| }; | ||
| } ) | ||
| ); |
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.
This also gets run after the hydratedAttributes are initial fetched, which makes the form dirty. Should we just set this as it's own function and only call it when the user updates or removes it?
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.
Well, the intention here was just to have the hydratedAttributes act as a proxy for the attributes in the form data, only with the terms data populated. So we can just alter this variable as we would otherwise alter the form data and get the behavior we want. It feels like it would complicate it more to have another callback that may or may not be called in conjunction with onChange.
Can you elaborate on what state would cause the form to become "dirty?" We'd only be updating this when we want to update the form data.
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.
Can you elaborate on what state would cause the form to become "dirty?" We'd only be updating this when we want to update the form data.
You can notice this when looking at the Update button, when you load an existing product with attributes the Update button shows as disabled initially (meaning there are no changes) and once the onChange here is called it becomes active, allowing a user to save. This could also cause issues in the future if we decide to rely more on the fieldsChanged values.
I understand the reasoning behind this and see how you need it, but it be nice if we only trigger onChange once actual changes have been made and not just for the bootstrapping of the field.
The reason for suggesting a separate callback function is because this may be difficult to handle within a useEffect, as it would be difficult to differentiate if hydratedAttributes was updated by an actual change or just the initial loading.
...ins/woocommerce-admin/client/products/fields/attribute-input-field/attribute-input-field.tsx
Outdated
Show resolved
Hide resolved
| return allItems.filter( | ||
| ( item ) => | ||
| filteredAttributeIds.indexOf( item.id ) < 0 && | ||
| const onlyIdsFilter = ( item: NarrowedQueryAttribute ) => |
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.
I believe this filter seems to break the select control, where I cannot search for any other attribute.
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.
Do you mean in just in the edit modal? That's my intention here and what I assumed would be the behavior we want (unless I misunderstand). See my other comment above for more context.
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.
I finally got it working, it may of been a couple things. The need for the use of the SelectControlMenuSlot to have the dropdown show correctly and I was testing this on a site that had over a 1000 of attributes, so the selected items may of not come back as part of the initial list, thus not showing up, also reasoning for just using the standard SelectControl as mentioned above.
...ins/woocommerce-admin/client/products/fields/attribute-input-field/attribute-input-field.tsx
Outdated
Show resolved
Hide resolved
| attributeId?: number; | ||
| placeholder?: string; | ||
| disabled?: boolean; | ||
| label?: string; |
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.
Nice!
|
Thanks for the feedback @louwie17 , unfortunately I'll need a bit more feedback on the above for it to be actionable. I've responded to your comments, let me know what you think! |
@joelclimbsthings I left some replies to your comments, let me know if you need any extra clarification whether in Slack or here. Also as I was testing this again I noticed one more issue, for products that make use of the |
|
Thanks for your feedback @louwie17 , I believe I've resolved everything. Given Jarek's design input, some of the code was no longer needed. That said, adding support for custom attributes is a bit dicey, as it loses the uniqueness of the attribute id (always zero it seems?), and causes some type confusion. I've done my best to address it with my time today, but it feels rushed. We could also break those commits off into a separate PR to add support for custom attributes if that feels more comfortable. Edit: I borrowed your |
louwie17
left a comment
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.
This is looking really good @joelclimbsthings, thanks for the hard work on this.
I just left a couple small suggestions in code and noticed one minor styling bug (the plus signing not aligning with the text):

plugins/woocommerce-admin/client/products/fields/attribute-field/attribute-field.tsx
Outdated
Show resolved
Hide resolved
plugins/woocommerce-admin/client/products/fields/attribute-field/edit-attribute-modal.tsx
Show resolved
Hide resolved
…put value not changing
e07560e to
556d1d2
Compare
|
Thanks @louwie17 , I've rebased to resolve the conflict and addressed the issues you mentioned. Let me know what you think 🤞🏻 |
louwie17
left a comment
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.
Thanks for the final updates @joelclimbsthings, this tested well and the code looks good! Nice work on this :)
I did notice one last thing, that a can update an attribute by removing all the terms and also updating the custom attribute to be an empty name. We probably want some validation around this?
I don't mind doing this as part of a separate PR
|
@louwie17 Had to resolve some unit test issues in one more commit. Could I get another ✅ when you get the chance? |
louwie17
left a comment
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.
Thanks for fixing the tests @joelclimbsthings, this looks good!
|
Hi @joelclimbsthings, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:
|
All Submissions:
Changes proposed in this Pull Request:
Adding the edit modal for attributes as per [the design](Show or hide this attribute in the filters section on your store’s category and shop pages).
Note: One complicating issue with managing attributes has to do with the product API endpoint only including the names of the various terms, which are not unique. To address this we're now fetching the terms associated with the product from the product terms endpoint, and using them to manage the adding/editing and various functionality, updating the global form data as needed. These are stored as the
hydratedAttributeswith theattribute-fieldcomponent. I've now refactored the add-modal to address these changes as well.Closes #34332 .
How to test the changes in this Pull Request:
Other information:
pnpm changelog add --filter=<project>?FOR PR REVIEWER ONLY: