-
Notifications
You must be signed in to change notification settings - Fork 357
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
refactor: [M3-8501] - AccessSelect Optimization: Use React Hook Form #10952
base: develop
Are you sure you want to change the base?
refactor: [M3-8501] - AccessSelect Optimization: Use React Hook Form #10952
Conversation
@harsh-akamai can you make sure to fix your unit tests before people review the PR? |
@abailly-akamai I have fixed the failing unit tests |
Coverage Report: ✅ |
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 working on this. The change makes sense and the code cleanup looks good, and is well self documented and tested
There is a state issue in the form however. The cors toggle shows always as enabled on page load, no matter it's value.
Screen.Recording.2024-09-27.at.09.12.56.mov
Looks like e2e test failures are flakes. You may want to rebase your PRA again to get the latest from develop since those have been fixed there i believe
packages/manager/src/features/ObjectStorage/BucketDetail/AccessSelect.tsx
Outdated
Show resolved
Hide resolved
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.
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.
Really nice job here! Just some minor changes and this is looking 🔥
onChange={(_, selected) => { | ||
if (selected) { | ||
field.onChange(selected.value); | ||
} | ||
}} |
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.
We can just set the value on the onChange itself
onChange={(_, selected) => { | |
if (selected) { | |
field.onChange(selected.value); | |
} | |
}} | |
onChange={(_, selected: { label: string; value: ACLType }) => { | |
if (selected) { | |
setValue('acl', selected.value); | |
} | |
}} |
<Toggle | ||
{...field} | ||
checked={field.value} | ||
disabled={bucketAccessIsFetching || objectAccessIsFetching} |
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.
disabled={bucketAccessIsFetching || objectAccessIsFetching} | |
disabled={bucketAccessIsFetching || objectAccessIsFetching} | |
onChange={(e) => setValue('cors_enabled', e.target.checked)} |
defaultValues: { | ||
acl: 'private', | ||
cors_enabled: true, | ||
}, |
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.
In correlation to changes from removing useEffect
defaultValues: { | |
acl: 'private', | |
cors_enabled: true, | |
}, | |
defaultValues |
import { capitalize } from 'src/utilities/capitalize'; | ||
import { getErrorStringOrDefault } from 'src/utilities/errorUtils'; | ||
|
||
import { bucketACLOptions, objectACLOptions } from '../utilities'; | ||
import { copy } from './AccessSelect.data'; | ||
|
||
import type { | ||
ACLType, |
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.
We'll need this back with my suggested changes
control, | ||
formState: { errors, isSubmitting }, | ||
handleSubmit, | ||
reset, |
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.
Replacing reset
with setValue
for more granular control in new approach
reset, | |
setValue, |
React.useEffect(() => { | ||
const data = variant === 'object' ? objectAccessData : bucketAccessData; | ||
if (data) { | ||
const { acl } = data; | ||
// Don't show "public-read-write" for Objects here; use "custom" instead | ||
// since "public-read-write" Objects are basically the same as "public-read". | ||
const _acl = | ||
variant === 'object' && acl === 'public-read-write' ? 'custom' : acl; | ||
const cors_enabled = isUpdateObjectStorageBucketAccessPayload(data) | ||
? data.cors_enabled ?? undefined | ||
: true; | ||
reset({ acl: _acl || undefined, cors_enabled }); | ||
} | ||
}, [bucketAccessData, objectAccessData, variant, reset]); |
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.
No idea what happened to my previous comments, so I'll just restate things.
We can remove this useEffect and use the internals to calculate the defaultValues
based on the latest API data.
const data = variant === 'object' ? objectAccessData : bucketAccessData;
const defaultValues = React.useMemo(() => {
if (data) {
const { acl } = data;
const _acl =
variant === 'object' && acl === 'public-read-write' ? 'custom' : acl;
const cors_enabled = isUpdateObjectStorageBucketAccessPayload(data)
? data.cors_enabled ?? true
: true;
return { acl: _acl as ACLType, cors_enabled };
}
return { acl: 'private' as ACLType, cors_enabled: true };
}, [data, variant]);
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 think removing useEffect might not solve the issue in this case since the useForm
hook reads the defaultValues
only on the initial render. Updating defaultValues
to the values returned from the api wouldn't update the form state. We will have to use the reset()
to update the form and I feel we will have to run reset()
inside a useEffect.
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.
Looks like passing the formValues
to the values
parameter of useForm() removes the necessity of useEffect()
. I have pushed these changes in the latest commit(9a2430f)
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.
Coming late to this PR, but wanted to prevent it from getting stale. I didn't notice any major regressions in the Access Select and test are passing. I did notice a minor regression in loading state (see video clips).
Also re-requesting Jaalah since this PR is blocked by requested changes that, I think, have all been addressed now.
} | ||
}} | ||
placeholder={ | ||
bucketAccessIsFetching || objectAccessIsFetching |
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 doesn't seem to be working as expected. We have a small regression in the loading state but I wasn't seeing why right off. We have the loading
prop and the fetching state of the two queries. Autocomplete has the loadingText
prop, but I don't think that makes a difference.
Prod | This Branch |
---|---|
autocompleteLoadingState.mov |
autocompleteNoLoadingState.mov |
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 pointing this out! The issue stems from setting default values for the form. One possible workaround is to add an option to the Options
array with the placeholder text 'Loading access..', though I am uncertain if this would be the most optimal solution for maintaining clean and efficient code.
Description 📝
The goal is to see if we can eliminate the number of useState calls we're making in this file and potentially remove the useEffect by using React Hook Form and inferring values from other values.
Changes 🔄
react-query
queries and mutations for managing stateTarget release date 🗓️
N/A
How to test 🧪
Verification steps
As an Author I have considered 🤔
Check all that apply