Skip to content

Commit 6f3edb3

Browse files
committed
fix: #OBS-I494: updated for add user and failed scenario
1 parent 56ad17a commit 6f3edb3

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

web-console-v2/src/pages/UserManagement/AddUser.tsx

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { UserRequest } from './UserManagement';
44
import { useUserList } from 'services/user';
55
import { User } from './UserManagement';
66
import { useAlert } from 'contexts/AlertContextProvider';
7+
import Alert from '@mui/material/Alert';
78

89
interface AddUserProps {
910
open: boolean;
1011
onClose: () => void;
11-
onSubmit: (newUser: UserRequest) => void;
12+
onSubmit: (newUser: UserRequest) => Promise<void>;
1213
currentUser: User;
1314
}
1415

@@ -35,6 +36,7 @@ const AddUser: React.FC<AddUserProps> = ({ open, onClose, onSubmit, currentUser
3536
const [isEmailTaken, setIsEmailTaken] = useState<boolean | null>(null);
3637
const { data: users } = useUserList();
3738
const { showAlert } = useAlert();
39+
const [error, setError] = useState<boolean | null>(null);
3840

3941
useEffect(() => {
4042
const userName = newUser?.user_name.replace(/\s+/g, '_');
@@ -70,16 +72,17 @@ const AddUser: React.FC<AddUserProps> = ({ open, onClose, onSubmit, currentUser
7072
};
7173

7274
const handleSubmit = () => {
73-
try {
74-
setTimeout(() => {
75-
onSubmit(newUser);
76-
onClose();
77-
resetForm();
78-
}, 1000);
79-
} catch (error) {
80-
showAlert('Failed to create user', 'error');
81-
}
75+
onSubmit(newUser)
76+
.then(() => {
77+
onClose();
78+
resetForm();
79+
})
80+
.catch(() => {
81+
showAlert('Failed to create user', 'error');
82+
setError(true);
83+
});
8284
};
85+
8386

8487
const resetForm = () => {
8588
setNewUser({
@@ -90,15 +93,18 @@ const AddUser: React.FC<AddUserProps> = ({ open, onClose, onSubmit, currentUser
9093
});
9194
};
9295

93-
const isEmailValid = newUser.email_address ? emailRegex.test(newUser.email_address) : true;
96+
const isEmailValid = newUser?.email_address ? emailRegex.test(newUser?.email_address) : true;
97+
const isFirstNameValid = !newUser.first_name || newUser.first_name.length >= 3;
98+
const isLastNameValid = !newUser.last_name || newUser.last_name.length >= 3;
9499
const isFormValid =
95-
newUser.user_name &&
96-
newUser.email_address &&
97-
newUser.password &&
98-
newUser.roles.length > 0 &&
100+
newUser?.user_name &&
101+
newUser?.email_address &&
102+
newUser?.password &&
103+
newUser?.roles.length > 0 &&
99104
isUsernameTaken === false &&
100-
isEmailTaken === false &&
101-
isEmailValid;
105+
isEmailValid &&
106+
isFirstNameValid &&
107+
isLastNameValid;
102108

103109
const availableRoles = currentUser?.is_owner ? rolesOptions : rolesOptions.filter(role => role.value !== 'admin');
104110

@@ -107,9 +113,19 @@ const AddUser: React.FC<AddUserProps> = ({ open, onClose, onSubmit, currentUser
107113
onClose();
108114
};
109115

116+
const handleDialogClose = (event: React.SyntheticEvent, reason: string) => {
117+
if (reason && (reason !== 'backdropClick')) {
118+
onClose();
119+
}
120+
};
121+
110122
return (
111-
<Dialog open={open} onClose={handleCancel}>
123+
<Dialog
124+
open={open}
125+
onClose={handleDialogClose}
126+
>
112127
<DialogTitle>Create New User</DialogTitle>
128+
{error && <Alert severity="error">Failed to create User</Alert>}
113129
<DialogContent>
114130
<TextField
115131
label="User Name"
@@ -131,6 +147,8 @@ const AddUser: React.FC<AddUserProps> = ({ open, onClose, onSubmit, currentUser
131147
value={newUser.first_name}
132148
onChange={handleChange}
133149
margin="normal"
150+
error={ !isFirstNameValid}
151+
helperText={!isFirstNameValid ? 'If provided, first name must be at least 3 characters' : ''}
134152
/>
135153
<TextField
136154
label="Last Name"
@@ -140,6 +158,8 @@ const AddUser: React.FC<AddUserProps> = ({ open, onClose, onSubmit, currentUser
140158
value={newUser.last_name}
141159
onChange={handleChange}
142160
margin="normal"
161+
error={!isLastNameValid}
162+
helperText={!isLastNameValid ? 'If provided, last name must be at least 3 characters' : ''}
143163
/>
144164
<TextField
145165
label="Email"

web-console-v2/src/pages/UserManagement/UserManagement.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import AddUser from './AddUser';
1010
import ChangeRoleDialog from './ChangeRoleDialog';
1111
import AlertDialog from 'components/AlertDialog/AlertDialog';
1212
import { useAlert } from 'contexts/AlertContextProvider';
13+
import _ from 'lodash';
1314

1415
export interface User {
1516
id: string;
@@ -94,28 +95,28 @@ const UserManagement = () => {
9495
setOpenDialog(false);
9596
};
9697

97-
const handleAddUser = (newUser: UserRequest) => {
98-
try {
98+
const handleAddUser = (newUser: UserRequest): Promise<void> => {
99+
return new Promise<void>((resolve, reject) => {
99100
setLoading(true);
101+
const filteredPayload = _.pickBy(newUser, _.identity);
100102
createUser(
101-
{ payload: newUser },
103+
{ payload: filteredPayload },
102104
{
103105
onSuccess: () => {
104106
refetch();
105107
setOpenDialog(false);
106108
setLoading(false);
107109
showAlert('User created successfully', 'success');
110+
resolve();
108111
},
109112
onError: (error) => {
110113
setLoading(false);
111114
showAlert('Failed to create user', 'error');
115+
reject(error);
112116
},
113117
}
114118
);
115-
}
116-
catch (error) {
117-
showAlert('Failed to create user', 'error');
118-
}
119+
});
119120
};
120121

121122
const handleDeactivateUser = (userName: string) => {

0 commit comments

Comments
 (0)