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

Add state selection and regional admin option to form in Users.tsx #107

Merged
Merged
Changes from all 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
66 changes: 58 additions & 8 deletions frontend/src/pages/Users/Users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import {
DialogTitle,
FormControlLabel,
IconButton,
MenuItem,
Paper,
Radio,
RadioGroup,
Select,
TextField,
Typography
} from '@mui/material';
import { SelectChangeEvent } from '@mui/material/Select';
import { DataGrid, GridColDef, GridRenderCellParams } from '@mui/x-data-grid';
import { Add, CheckCircleOutline, Delete } from '@mui/icons-material';
import CustomToolbar from 'components/DataGrid/CustomToolbar';
Expand All @@ -23,6 +26,7 @@ import InfoDialog from 'components/Dialog/InfoDialog';
import { ImportExport } from 'components';
import { initializeUser, Organization, User } from 'types';
import { useAuthContext } from 'context';
import { STATE_OPTIONS } from '../../constants/constants';
// @ts-ignore:next-line
import { formatDistanceToNow, parseISO } from 'date-fns';

Expand Down Expand Up @@ -50,13 +54,15 @@ type UserFormValues = {
email: string;
organization?: Organization;
userType: string;
state: string;
};

const initialUserFormValues = {
firstName: '',
lastName: '',
email: '',
userType: ''
userType: '',
state: ''
};

type CloseReason = 'backdropClick' | 'escapeKeyDown' | 'closeButtonClick';
Expand All @@ -73,7 +79,8 @@ export const Users: React.FC = () => {
firstName: false,
lastName: false,
email: false,
userType: false
userType: false,
state: false
});
const [errorStates, setErrorStates] = useState<ErrorStates>({
getUsersError: '',
Expand Down Expand Up @@ -181,7 +188,8 @@ export const Users: React.FC = () => {
firstName: false,
lastName: false,
email: false,
userType: false
userType: false,
state: false
});
};

Expand Down Expand Up @@ -209,14 +217,16 @@ export const Users: React.FC = () => {
firstName: values.firstName,
lastName: values.lastName,
email: values.email,
userType: values.userType
userType: values.userType,
state: values.state
};
const { firstName, lastName, email, userType } = values;
const { firstName, lastName, email, userType, state } = values;
const newFormErrors = {
firstName: !firstName,
lastName: !lastName,
email: !email,
userType: !userType
userType: !userType,
state: !state
};
setFormErrors(newFormErrors);
if (Object.values(newFormErrors).some((error) => error)) {
Expand Down Expand Up @@ -253,6 +263,13 @@ export const Users: React.FC = () => {
}));
};

const handleChange = (event: SelectChangeEvent) => {
setValues((values) => ({
...values,
[event.target.name]: event.target.value
}));
};

const textFieldStyling = {
'& .MuiOutlinedInput-root': {
'&.Mui-focused fieldset': {
Expand Down Expand Up @@ -357,6 +374,33 @@ export const Users: React.FC = () => {
value={values.email}
onChange={onTextChange}
/>
<Typography mt={1}>State</Typography>
<Select
displayEmpty
size="small"
id="state"
value={values.state}
name="state"
error={formErrors.state}
onChange={handleChange}
fullWidth
renderValue={
values.state !== ''
? undefined
: () => <Typography color="#bdbdbd">Select a State</Typography>
}
>
{STATE_OPTIONS.map((state: string, index: number) => (
<MenuItem key={index} value={state}>
{state}
</MenuItem>
))}
</Select>
{formErrors.state && (
<Typography pl={2} variant="caption" color="error.main">
State is required
</Typography>
)}
<Typography mt={2}>User Type</Typography>
<RadioGroup
aria-label="User Type"
Expand All @@ -374,14 +418,19 @@ export const Users: React.FC = () => {
control={<Radio color="primary" />}
label="Global View"
/>
<FormControlLabel
value="regionalAdmin"
control={<Radio color="primary" />}
label="Regional Administrator"
/>
<FormControlLabel
value="globalAdmin"
control={<Radio color="primary" />}
label="Global Administrator"
/>
</RadioGroup>
{formErrors.userType && (
<Typography variant="caption" color="error.main">
<Typography pl={2} variant="caption" color="error.main">
User Type is required
</Typography>
)}
Expand All @@ -401,7 +450,8 @@ export const Users: React.FC = () => {
firstName: false,
lastName: false,
email: false,
userType: false
userType: false,
state: false
});
setValues(initialUserFormValues);
}}
Expand Down
Loading