Skip to content

Commit 0b6a4ab

Browse files
staging hotfix: [UIE-9137] - AssignSelectedRolesDrawer user selection state issue (#12748)
* fix AssignSelectedRolesDrawer user state selection * cleanup
1 parent d1e8748 commit 0b6a4ab

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

packages/manager/src/features/IAM/Roles/RolesTable/AssignSelectedRolesDrawer.tsx

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,27 @@ export const AssignSelectedRolesDrawer = ({
4545
}: Props) => {
4646
const theme = useTheme();
4747

48-
const [usernameInput, setUsernameInput] = useState<string>('');
49-
const debouncedUsernameInput = useDebouncedValue(usernameInput);
48+
const values = {
49+
roles: selectedRoles.map((r) => ({
50+
role: {
51+
access: r.access,
52+
entity_type: r.entity_type,
53+
label: r.name,
54+
value: r.name,
55+
},
56+
entities: null,
57+
})),
58+
username: null,
59+
};
5060

51-
const [username, setUsername] = useState<null | string>('');
61+
const form = useForm<AssignNewRoleFormValues>({
62+
defaultValues: values,
63+
values,
64+
});
5265

66+
const [usernameInput, setUsernameInput] = useState<string>('');
67+
const debouncedUsernameInput = useDebouncedValue(usernameInput);
68+
const username = form.watch('username');
5369
const userSearchFilter = debouncedUsernameInput
5470
? {
5571
['+or']: [
@@ -79,30 +95,12 @@ export const AssignSelectedRolesDrawer = ({
7995
}));
8096
}, [accountUsers]);
8197

98+
const { handleSubmit, reset, control, formState, setError } = form;
99+
82100
const { data: accountRoles } = useAccountRoles();
83101

84102
const { data: existingRoles } = useUserRoles(username ?? '');
85103

86-
const values = {
87-
roles: selectedRoles.map((r) => ({
88-
role: {
89-
access: r.access,
90-
entity_type: r.entity_type,
91-
label: r.name,
92-
value: r.name,
93-
},
94-
entities: null,
95-
})),
96-
username: null,
97-
};
98-
99-
const form = useForm<AssignNewRoleFormValues>({
100-
defaultValues: values,
101-
values,
102-
});
103-
104-
const { handleSubmit, reset, control, formState, setError } = form;
105-
106104
const [areDetailsHidden, setAreDetailsHidden] = useState(false);
107105

108106
const { mutateAsync: updateUserRoles, isPending } = useUserRolesMutation(
@@ -139,8 +137,7 @@ export const AssignSelectedRolesDrawer = ({
139137

140138
const handleClose = () => {
141139
reset();
142-
setUsername(null);
143-
setUsernameInput('');
140+
144141
onClose();
145142
};
146143

@@ -193,10 +190,13 @@ export const AssignSelectedRolesDrawer = ({
193190
loading={isLoadingAccountUsers || isFetchingAccountUsers}
194191
noMarginTop
195192
onChange={(_, option) => {
196-
setUsername(option?.label || null);
197-
onChange(username);
193+
onChange(option?.label || null);
194+
// Form now has the username, so we can clear the input
195+
// This will prevent refetching all users with an existing user as a filter
196+
setUsernameInput('');
198197
}}
199198
onInputChange={(_, value) => {
199+
// We set an input state separately for when we query the API
200200
setUsernameInput(value);
201201
}}
202202
options={getUserOptions() || []}

0 commit comments

Comments
 (0)