OBPIH-6108 Default Preference Type section (fixes after QA)#4536
Conversation
…preference sections has changed
| export default AdditionalDetails; | ||
|
|
||
| export const additionalDetailsFormErrors = PropTypes.shape({ |
There was a problem hiding this comment.
Eslint doesn't shout at it? I remember there was a restriction about using default export and normal export in one file
There was a problem hiding this comment.
I had no such lint errors? Do we have this rule in our eslint config?
| ...omitEmptyValues(values.basicDetails), | ||
| ...omitEmptyValues(values.additionalDetails), | ||
| ...omitEmptyValues(values.defaultPreferenceType), | ||
| ...omitEmptyValues(values.packageSpecification), | ||
| ...omitEmptyValues(values.fixedPrice), |
There was a problem hiding this comment.
It could be better to use object restructuring. The value is repeated many times... but as I know the onSubmit will be refactored, so I don't know if this change is needed at this point.
There was a problem hiding this comment.
I agree with @alannadolny and was about to point out the same thing, but anyway this method will be refactored, so there is no need to spend time on that now imho.
| import PropTypes from 'prop-types'; | ||
|
|
||
| // eslint-disable-next-line import/prefer-default-export | ||
| export const FormErrorPropType = PropTypes.shape({ | ||
| message: PropTypes.string, | ||
| }); |
There was a problem hiding this comment.
so if this file will be used for defining our custom propTypes, you can use default export like that:
| import PropTypes from 'prop-types'; | |
| // eslint-disable-next-line import/prefer-default-export | |
| export const FormErrorPropType = PropTypes.shape({ | |
| message: PropTypes.string, | |
| }); | |
| import PropTypes from 'prop-types'; | |
| const FormErrorPropType = PropTypes.shape({ | |
| message: PropTypes.string, | |
| }); | |
| export default propTypes = { | |
| // Add here newly created custom propTypes | |
| FormErrorPropType, | |
| }; |
There was a problem hiding this comment.
this was intentional from my side.
The above example with default export will not get optimized by webpack treeshaking algorithm which should omit the unused code from being included in the bundle.
So in such cases, it is best to export it individually.
| const defaultPreferenceType = useWatch({ control, name: 'defaultPreferenceType' }); | ||
| useEffect(() => { | ||
| trigger('defaultPreferenceType.preferenceType'); | ||
| }, [defaultPreferenceType]); |
There was a problem hiding this comment.
I don't know if it's expected, but if defaultPreferenceType is an object, it would trigger the useEffect whenever the component re-renders, not when the defaultPreferenceType changes. It is because objects are compared by reference in JS, not by its values.
I think currently it might run more times than we would expect it to run.
There was a problem hiding this comment.
this was the point, we want to trigger this validation when any of the values in the default preference type subsection changes.
I tested dit with console logs on this useEffect and everything seems to work as expected.
There was a problem hiding this comment.
I know, but I meant that even if none values change, and something else triggers the re-render, this useEffect would anyway be triggered, because a reference of the object would change.
| ...omitEmptyValues(values.basicDetails), | ||
| ...omitEmptyValues(values.additionalDetails), | ||
| ...omitEmptyValues(values.defaultPreferenceType), | ||
| ...omitEmptyValues(values.packageSpecification), | ||
| ...omitEmptyValues(values.fixedPrice), |
There was a problem hiding this comment.
I agree with @alannadolny and was about to point out the same thing, but anyway this method will be refactored, so there is no need to spend time on that now imho.
src/js/hooks/productSupplier/form/useProductSupplierValidation.js
Outdated
Show resolved
Hide resolved
| ) { | ||
| return !!subsectionData.preferenceType; | ||
| } | ||
| return true; |
There was a problem hiding this comment.
I kinda don't get what it does. I thought that it should return true (so that it requires the preferenceType to be chosen) when any of other values are specified.
So wouldn't this line mean, that if none of other values are specified, we are still requiring the preference type to be chosen?
There was a problem hiding this comment.
if any other field is filled in then preferenceType is required.
the boolean values return true if field is valid and false if it is invalid, which means that
true - field is not required (everything is alright)
false - field should be required (validation failed)
There was a problem hiding this comment.
could you add this comment above the method? I think the name requirePreferenceType suggests that if it returns true, then it means that we require a preference type.
There was a problem hiding this comment.
added comments and changed the name, I hope there is no more confusion
|
A general comment is that I feel uncomfortable with such refactors at the end of the feature development. There were many edge cases where something in schema was/was not needed or it was intentionally done as |
- added comments and renamed validation function for Preference type - moved validation trigger to onBlur on other fields rather than useEffect
5ed5d6c to
cdc6bc4
Compare
* OBPIH-6108 Refactor validation schema sections into separate schemas * OBPIH-6108 export error types from subsections * OBPIH-6108 Move Each price calculation into separate hook * OBPIH-6108 trigger validation on preference type fields when default preference sections has changed * OBPIH-6108 refactor default values for product sources create form * OBPIH-6108 Fix issue with clearing default prefference type select * OBPIH-6108 Fixes after review - added comments and renamed validation function for Preference type - moved validation trigger to onBlur on other fields rather than useEffect
There is a bit of a refactor that I did on the validation schema that allowed me to add a custom
refine()validation on the whole section.Additionally, I believe that separating our validations schema into smaller subsections will be a lot more readable and maintainable. I believe we already talked about this and since there aren't any tickets that will have conflicts with these changes Id decided to proceed with this refactor.
So what has changed?
getProductSource, submitted payload and field name attributes accordingly.FormErrorPropTypewhich is repeated in all of our error props.undefinedinstead ofnullbecause we have.coerce.date()defined on validation schema for date which parsesnullto some date value like1970useCalculateEachPricewhich I believe will declutter the main sources form hook