Skip to content

Commit

Permalink
Update styling for the Add User form (#1355)
Browse files Browse the repository at this point in the history
* update styling of add user form

* fix tests

* fix: tweaked some props for edit dialog

---------

Co-authored-by: Mac Deluca <[email protected]>
Co-authored-by: Mac Deluca <[email protected]>
  • Loading branch information
3 people authored Sep 4, 2024
1 parent bb198fa commit bb4f7dd
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 191 deletions.
4 changes: 3 additions & 1 deletion app/src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ const AppRouter: React.FC = () => {
<AuthenticatedRouteGuard>
<SystemRoleRouteGuard validRoles={[SYSTEM_ROLE.SYSTEM_ADMIN]}>
<DialogContextProvider>
<AdminUsersRouter />
<CodesContextProvider>
<AdminUsersRouter />
</CodesContextProvider>
</DialogContextProvider>
</SystemRoleRouteGuard>
</AuthenticatedRouteGuard>
Expand Down
10 changes: 9 additions & 1 deletion app/src/components/fields/CustomTextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export interface ICustomTextField {
* @memberof ICustomTextField
*/
label: string;
/**
* Placeholder for the text field
*
* @type {string}
* @memberof ICustomTextField
*/
placeholder?: string;
/**
* Name of the text field, typically this is used to identify the field in the formik context.
*
Expand All @@ -34,13 +41,14 @@ export interface ICustomTextField {
const CustomTextField = (props: React.PropsWithChildren<ICustomTextField>) => {
const { touched, errors, values, handleChange, handleBlur } = useFormikContext<any>();

const { name, label, other } = props;
const { name, label, other, placeholder } = props;

return (
<TextField
name={name}
label={label}
id={name}
placeholder={placeholder}
inputProps={{ 'data-testid': name, maxLength: props.maxLength || undefined }} // targets the internal input rather than the react component
onChange={handleChange}
onBlur={handleBlur}
Expand Down
20 changes: 17 additions & 3 deletions app/src/features/admin/users/ActiveUsersList.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { AuthStateContext } from 'contexts/authStateContext';
import { CodesContext, ICodesContext } from 'contexts/codesContext';
import { createMemoryHistory } from 'history';
import { useBiohubApi } from 'hooks/useBioHubApi';
import { DataLoader } from 'hooks/useDataLoader';
import { Router } from 'react-router';
import { getMockAuthState, SystemAdminAuthState } from 'test-helpers/auth-helpers';
import { codes } from 'test-helpers/code-helpers';
Expand All @@ -19,17 +21,29 @@ const mockUseApi = {
},
admin: {
addSystemUser: jest.fn()
},
codes: {
getAllCodeSets: jest.fn()
}
};

const mockCodesContext: ICodesContext = {
codesDataLoader: {
data: codes,
load: () => {}
} as DataLoader<any, any, any>
};

const renderContainer = (props: IActiveUsersListProps) => {
const authState = getMockAuthState({ base: SystemAdminAuthState });

return render(
<AuthStateContext.Provider value={authState}>
<Router history={history}>
<ActiveUsersList {...props} />
</Router>
<CodesContext.Provider value={mockCodesContext}>
<Router history={history}>
<ActiveUsersList {...props} />
</Router>
</CodesContext.Provider>
</AuthStateContext.Provider>
);
};
Expand Down
64 changes: 42 additions & 22 deletions app/src/features/admin/users/ActiveUsersList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import { DialogContext, ISnackbarProps } from 'contexts/dialogContext';
import { APIError } from 'hooks/api/useAxios';
import { useAuthStateContext } from 'hooks/useAuthStateContext';
import { useBiohubApi } from 'hooks/useBioHubApi';
import { useCodesContext } from 'hooks/useContext';
import { IGetAllCodeSetsResponse } from 'interfaces/useCodesApi.interface';
import { ISystemUser } from 'interfaces/useUserApi.interface';
import { useContext, useState } from 'react';
import { useContext, useEffect, useState } from 'react';
import { useHistory } from 'react-router';
import { Link as RouterLink } from 'react-router-dom';
import { getCodesName } from 'utils/Utils';
import AddSystemUsersForm, {
AddSystemUsersFormInitialValues,
AddSystemUsersFormYupSchema,
Expand Down Expand Up @@ -50,6 +52,12 @@ const ActiveUsersList = (props: IActiveUsersListProps) => {

const [openAddUserDialog, setOpenAddUserDialog] = useState(false);

const codesContext = useCodesContext();

useEffect(() => {
codesContext.codesDataLoader.load();
}, [codesContext.codesDataLoader]);

const activeUsersColumnDefs: GridColDef<ISystemUser>[] = [
{
field: 'user_identifier',
Expand Down Expand Up @@ -279,16 +287,16 @@ const ActiveUsersList = (props: IActiveUsersListProps) => {
const handleAddSystemUsersSave = async (values: IAddSystemUsersForm) => {
setOpenAddUserDialog(false);

const systemUser = values.systemUser;

try {
for (const systemUser of values.systemUsers) {
await biohubApi.admin.addSystemUser(
systemUser.userIdentifier,
systemUser.identitySource,
systemUser.displayName,
systemUser.email,
systemUser.systemRole
);
}
await biohubApi.admin.addSystemUser(
systemUser.userIdentifier,
systemUser.identitySource,
systemUser.displayName,
systemUser.email,
systemUser.systemRole
);

// Refresh users list
refresh();
Expand All @@ -297,7 +305,7 @@ const ActiveUsersList = (props: IActiveUsersListProps) => {
open: true,
snackbarMessage: (
<Typography variant="body2" component="div">
{values.systemUsers.length} system {values.systemUsers.length > 1 ? 'users' : 'user'} added.
Successfully added {systemUser.displayName}
</Typography>
)
});
Expand All @@ -307,8 +315,12 @@ const ActiveUsersList = (props: IActiveUsersListProps) => {
if (apiError.status === 409) {
dialogContext.setErrorDialog({
open: true,
dialogTitle: 'Failed to create users',
dialogText: 'One of the users you added already exists.',
dialogTitle: 'User already exists',
dialogText: `${systemUser.displayName} already exists as a ${getCodesName(
codesContext.codesDataLoader.data,
'system_roles',
systemUser.systemRole
)}`,
onClose: () => {
dialogContext.setErrorDialog({ open: false });
},
Expand Down Expand Up @@ -380,21 +392,29 @@ const ActiveUsersList = (props: IActiveUsersListProps) => {
</Paper>

<EditDialog
dialogTitle={'Add Users'}
dialogTitle={'Add User'}
open={openAddUserDialog}
dialogSaveButtonLabel={'Add'}
size="sm"
component={{
element: (
<AddSystemUsersForm
systemRoles={
codes?.system_roles?.map((item) => {
return { value: item.id, label: item.name };
}) || []
}
/>
<>
<Typography color="textSecondary" mb={3}>
This form creates a new user that will be linked to an IDIR/BCeID when an account with a matching
username, email, and account type logs in.
</Typography>
<AddSystemUsersForm
systemRoles={
codes?.system_roles?.map((item) => {
return { value: item.id, label: item.name };
}) || []
}
/>
</>
),
initialValues: AddSystemUsersFormInitialValues,
validationSchema: AddSystemUsersFormYupSchema
validationSchema: AddSystemUsersFormYupSchema,
validateOnBlur: false
}}
onCancel={() => setOpenAddUserDialog(false)}
onSave={(values) => {
Expand Down
Loading

0 comments on commit bb4f7dd

Please sign in to comment.