-
Notifications
You must be signed in to change notification settings - Fork 219
Convert all products edit to TypeScript #9782
base: trunk
Are you sure you want to change the base?
Conversation
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 your contribution, @Sidsector9. I requested a review from the corresponding team. While quickly looking over the PR, I noticed one section, that should be changed. I left a comment in the corresponding section.
debouncedSpeak: PropTypes.func.isRequired, | ||
}; | ||
|
||
class Editor extends Component< EditorProps, EditorState > { |
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.
While briefly looking at your changes, I noticed that this file is still using a class component. Now that we're touching this file, we should convert it to a functional component. Could you take care of that?
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.
@nielslange I've moved from class to functional component.
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 contributing! I left a couple of comments related to the code as well as to the behavior of All Products block.
I would also suggest adding some testing steps to the PR description to showcase how did you check that this block preserves the behavior from before the refactor. It also makes it easier for us to confirm it works the same on our end, thanks!
template: [ string, object? ][]; | ||
templateLock: boolean; | ||
allowedBlocks: Array< string >; | ||
renderAppender?: undefined | boolean; |
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.
Considering the property is optional, it can be implicitly undefined
hence you can just define it as optional boolean
:
renderAppender?: undefined | boolean; | |
renderAppender?: boolean; |
blockMap = getBlockMap( 'woocommerce/all-products' ); | ||
export default function Edit( props: Props ): JSX.Element { | ||
const [ isEditing, setIsEditing ] = useState< boolean >( false ); | ||
const [ , setInnerBlocks ] = useState< BlockInstance[] | boolean >( false ); |
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.
To preserve the logic, I believe the initial state should be an empty array as per previous implementation:
state = {
isEditing: false,
innerBlocks: [],
};
Also, could you shed more light on why there's just a setInnerBlocks
function in use, but no innerBlocks
value like:
const [ , setInnerBlocks ] = useState< BlockInstance[] | boolean >( false ); | |
const [ innerBlocks, setInnerBlocks ] = useState< BlockInstance[] | boolean >( false ); |
|
||
getTitle = () => { | ||
const { innerBlocks } = useSelect( ( select ) => { |
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 see the component logic is based on the value returned from useSelect
, while it should be based on the value from useState
. I think it's related to the question from the previous comment about why the innerBlocks
from useState
is not in use.
const { innerBlocks } = this.state; | ||
replaceInnerBlocks( block.clientId, innerBlocks, false ); | ||
this.togglePreview(); | ||
replaceInnerBlocks( clientId, innerBlocks, false ); |
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.
When you use Cancel
button on the UI and go back to edit mode, the previous state is preserved. It may be related to using innerBlocks
from useSelect
rather than useState
.
Steps to reproduce:
- Go to Edit mode of All Products
- Add some additional Products Elements
- Click "Cancel"
- In the preview mode there's no newly added elements
- Go to Edit mode again
- All the added elements are there while the edit mode should reflect the preview state
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.
What do you think about renaming the file to save.jsx
considering it returns JSX component?
}, [] ); | ||
|
||
const { replaceInnerBlocks } = useDispatch( 'core/block-editor' ); | ||
const debouncedSpeak = useDebounce( speak ); |
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.
Is there a reason we cannot use the debouncedSpeak
provided through the props as it was before, but rather create a new one?
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.
@kmanijak Let me know if I've understood it incorrectly. Previously, debouncedSpeak
was the props added by the withSpokenMessages
HOC and I've replaced that with the useDebounce
hook. Is that not correct?
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.
Previously, debouncedSpeak was the props added by the withSpokenMessages HOC and I've replaced that with the useDebounce hook. Is that not correct?
Oh, I didn't spot that. But I think we could stick to keep using withSpokenMessages
which is used across multiple blocks. Is there a reason to replace it with a custom implementation?
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 tried to test the All Products block and after modifying the content All Products turns into some unexpected state:
allprod-hidden-content.mov
Also, as I mentioned in the previous iteration:
I would also suggest adding some testing steps to the PR description to showcase how did you check that this block preserves the behavior from before the refactor. It also makes it easier for us to confirm it works the same on our end, thanks!
Even though this PR doesn't add a new feature it helps us and other colleagues to make sure the changes don't introduce any regressions, so I'd appreciate if you add some testing steps to the PR description. Here are some examples of PR introducing a similar change (conversion to TypeScript) with the testing steps included:
- Refactor: Convert All Products block file from Javascript to Typescript and replace propTypes with TypeScript definitions #9802
- Convert grid-layout-control to use Typescript #9811
Please let me know if you have some questions 🙌
}, [] ); | ||
|
||
const { replaceInnerBlocks } = useDispatch( 'core/block-editor' ); | ||
const debouncedSpeak = useDebounce( speak ); |
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.
Previously, debouncedSpeak was the props added by the withSpokenMessages HOC and I've replaced that with the useDebounce hook. Is that not correct?
Oh, I didn't spot that. But I think we could stick to keep using withSpokenMessages
which is used across multiple blocks. Is there a reason to replace it with a custom implementation?
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.
What do you think about renaming this file to save.jsx
as it returns JSX content?
Hi @Sidsector9! It’s been a while since we heard from you. I’d like you to know that this weekend (8-9 December) WooCommerce Blocks repo will be migrated to WooCommerce monorepo. However, open PRs won’t be migrated. That means open PRs have to be merged by the end of this week or otherwise, you’ll have to open/recreate a new PR in a monorepo post-migration if you’re interested in finalizing this contribution. Thanks! |
Fixes woocommerce/woocommerce#42363
Testing
Automated Tests
Changes in this PR are covered by Automated Tests.
Do not include in the Testing Notes
WooCommerce Visibility