Skip to content
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

Update styling for the Add User form #1355

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion app/src/components/dialog/EditDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const EditDialog = <T extends FormikValues>(props: PropsWithChildren<IEdi
initialValues={props.component.initialValues}
enableReinitialize={true}
validationSchema={props.component.validationSchema}
validateOnBlur={props.component.validateOnBlur ?? true}
validateOnBlur={props.component.validateOnBlur ?? false}
mauberti-bc marked this conversation as resolved.
Show resolved Hide resolved
validateOnChange={props.component.validateOnChange ?? false}
onSubmit={(values) => {
props.onSave(values);
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
61 changes: 40 additions & 21 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 { 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 [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 handleAddSystemUsersSave = async (values: IAddSystemUsersForm) => {
setOpenAddUserDialog(false);

const systemUser = values.systemUser;

Check warning on line 290 in app/src/features/admin/users/ActiveUsersList.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/features/admin/users/ActiveUsersList.tsx#L290

Added line #L290 was not covered by tests

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(

Check warning on line 293 in app/src/features/admin/users/ActiveUsersList.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/features/admin/users/ActiveUsersList.tsx#L293

Added line #L293 was not covered by tests
systemUser.userIdentifier,
systemUser.identitySource,
systemUser.displayName,
systemUser.email,
systemUser.systemRole
);

// Refresh users list
refresh();
Expand All @@ -297,7 +305,7 @@
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 @@
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,18 +392,25 @@
</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
Expand Down
Loading
Loading