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

Issue 442/basic information form #533

Merged
merged 56 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
5514856
Establishing UI foundation
andycwilliams Oct 31, 2023
1c08e7a
Adding field clearing functionality
andycwilliams Oct 31, 2023
73754fa
Beginning JSDoc for component
andycwilliams Oct 31, 2023
9b8cfcc
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 7, 2023
d617a48
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 8, 2023
69a2443
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 12, 2023
bf92d76
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 13, 2023
375c3e7
Testing WIP changes from #515
andycwilliams Nov 14, 2023
9da1545
Applying HMIS terminology
andycwilliams Nov 14, 2023
1e5be1f
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 14, 2023
446f19f
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 16, 2023
5eee3e8
Organizing for draft pull request
andycwilliams Nov 20, 2023
587df65
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 20, 2023
bb31619
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 21, 2023
b37161b
Filling out component test
andycwilliams Nov 21, 2023
5c3df18
Working on tests
andycwilliams Nov 21, 2023
c9bce76
Resolving a few errors, working on more
andycwilliams Nov 21, 2023
02fcff3
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 22, 2023
ac5a9a9
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 22, 2023
0c8f978
Updating date picker functionality
andycwilliams Nov 26, 2023
ec4d5cf
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 26, 2023
d886160
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 28, 2023
7854e89
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 29, 2023
7462f68
Fixing margin, still working on test
andycwilliams Nov 29, 2023
fc194e1
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Nov 30, 2023
7776ac5
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Dec 8, 2023
f82472f
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Dec 12, 2023
a414027
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Dec 20, 2023
6879a85
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Feb 3, 2024
68e7872
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Feb 15, 2024
fb53ae1
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Feb 21, 2024
e981037
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Feb 27, 2024
6da2248
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Mar 11, 2024
f9380b8
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Mar 13, 2024
4816f96
Fixing date input not being recognized
andycwilliams Mar 13, 2024
2f28c17
Update src/components/CivicProfileForms/BasicInfo.jsx
andycwilliams Mar 13, 2024
1e13721
Resolving some testing errors
andycwilliams Mar 13, 2024
2f8b8b2
Merge branch 'issue-442/basic-information-form' of https://github.com…
andycwilliams Mar 13, 2024
e983684
Returning helper text
andycwilliams Mar 16, 2024
ae466e2
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Mar 23, 2024
6903809
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Mar 27, 2024
011e334
Refactoring Basic Info form
andycwilliams Apr 3, 2024
c342b40
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Apr 3, 2024
df4278d
Refactoring handleChange
andycwilliams Apr 5, 2024
5a4a72b
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Apr 11, 2024
35f633e
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Apr 15, 2024
e3bbdc1
Merge branch 'issue-442/basic-information-form' of https://github.com…
andycwilliams Apr 15, 2024
b8aacc8
Merge branch 'Development' into issue-442/basic-information-form
leekahung Apr 17, 2024
362d961
Adjust form input responsiveness
andycwilliams Apr 18, 2024
52e5050
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Apr 18, 2024
73b0474
Merge branch 'issue-442/basic-information-form' of https://github.com…
andycwilliams Apr 18, 2024
ef025be
Resolved issue of submitted form data not displaying properly
andycwilliams Apr 20, 2024
d72e6ea
Merge branch 'Development' into issue-442/basic-information-form
andycwilliams Apr 24, 2024
719ca65
Adding default value to Gender field
andycwilliams Apr 24, 2024
343beee
Temporarily commenting out unsuccessful tests
andycwilliams Apr 24, 2024
fbb810b
Adding notification and default field value
andycwilliams Apr 24, 2024
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
182 changes: 178 additions & 4 deletions src/components/CivicProfileForms/BasicInfo.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,182 @@
// React Imports
import React from 'react';
// MUI Imports
import Typography from '@mui/material/Typography';
import React, { useState, useEffect } from 'react';
// Material UI Imports
import Button from '@mui/material/Button';
import CheckIcon from '@mui/icons-material/Check';
import ClearIcon from '@mui/icons-material/Clear';
import FormControl from '@mui/material/FormControl';
import FormHelperText from '@mui/material/FormHelperText';
import Grid from '@mui/material/Grid';
import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import Select from '@mui/material/Select';
import TextField from '@mui/material/TextField';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { DatePicker } from '@mui/x-date-pickers/DatePicker';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import dayjs from 'dayjs';
// Hook Imports
import { useCivicProfile } from '@hooks';
// Component Imports
import { FormSection } from '../Form';

const BasicInfo = () => <Typography>Basic Info</Typography>;
/**
* BasicInfo Component - A form to fill out basic user info
*
* @memberof CivicProfileForms
* @name BasicInfo
* @returns {React.JSX.Element} The BasicInfo Component
*/
const BasicInfo = () => {
const { data, add, isSuccess, storedDataset, refetch } = useCivicProfile();
const [formData, setFormData] = useState({
firstName: '',
lastName: '',
dateOfBirth: null,
gender: ''
Copy link
Contributor

@leekahung leekahung Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too big of an issue since the feature appears to be working as intended, however, MUI is complaining about the input fields if it's not using one of the values from the option elements.

Screenshot 2024-04-23 at 7 45 27 PM Screenshot 2024-04-23 at 7 45 41 PM

Think we could simply set the defaultValue, state, and clear value for gender as the number 9 to work around the problem. So Line 36, Line 75, and Line 133.

});

useEffect(() => {
if (isSuccess) {
setFormData((prevFormData) => ({ ...prevFormData, ...data }));
}
}, [isSuccess, data]);
useEffect(() => {
if (!storedDataset) {
refetch();
}
}, [storedDataset]);

const handleChange = (e) => {
if (e.$isDayjsObject) {
setFormData((prevFormData) => ({
...prevFormData,
dateOfBirth: e.toDate()
}));
} else if (e.target) {
const { name, value } = e.target;
setFormData((prevFormData) => ({ ...prevFormData, [name]: value }));
}
};

const handleSubmit = (e) => {
e.preventDefault();
if (!isSuccess || !storedDataset) {
return;
}
add(formData);
};

const handleClear = () => {
setFormData((prevFormData) => ({
...prevFormData,
firstName: '',
lastName: '',
dateOfBirth: null,
gender: ''
}));
};

return (
<FormSection title="Basic Information">
<form aria-labelledby="add-contact-form" onSubmit={handleSubmit} autoComplete="off">
<Grid container spacing={2}>
<Grid item xs={12} md={6}>
<TextField
id="hmis-basic-info-first-name"
name="firstName"
label="Legal first name"
onChange={handleChange}
value={formData.firstName}
fullWidth
autoFocus
/>
</Grid>
<Grid item xs={12} md={6}>
<TextField
id="hmis-basic-info-last-name"
name="lastName"
label="Legal last name"
onChange={handleChange}
value={formData.lastName}
fullWidth
/>
</Grid>
<Grid item xs={12} md={6}>
<FormControl fullWidth>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
id="hmis-basic-info-date-of-birth"
name="dateOfBirth"
label="Date of birth"
onChange={handleChange}
value={formData.dateOfBirth ? dayjs(formData.dateOfBirth) : null}
format="YYYY-MM-DD"
type="date"
disableFuture
openTo="year"
views={['year', 'month', 'day']}
/>
</LocalizationProvider>
<FormHelperText>YYYY-MM-DD</FormHelperText>
</FormControl>
</Grid>
<Grid item xs={12} md={6}>
<FormControl fullWidth>
<InputLabel id="hmis-basic-info-gender">Gender</InputLabel>
<Select
id="hmis-basic-info-gender-select"
name="gender"
label="Gender"
onChange={handleChange}
value={formData.gender}
labelId="hmis-basic-info-gender"
defaultValue=""
>
<MenuItem value={0}>Female</MenuItem>
<MenuItem value={1}>Male</MenuItem>
<MenuItem value={2}>Transgender male to female</MenuItem>
<MenuItem value={3}>Transgender female to male</MenuItem>
<MenuItem value={4}>Don&apos;t identify as male, female or transgender</MenuItem>
<MenuItem value={8}>Don&apos;t know</MenuItem>
<MenuItem value={9}>Decline to answer</MenuItem>
</Select>
</FormControl>
</Grid>
<Grid item xs={12} md={6}>
<Button
variant="outlined"
type="button"
label="Clear button"
color="error"
startIcon={<ClearIcon />}
fullWidth
sx={{ borderRadius: '20px' }}
onClick={handleClear}
aria-label="Clear button"
>
Clear
</Button>
</Grid>
<Grid item xs={12} md={6}>
<Button
variant="contained"
type="submit"
label="Submit button"
color="primary"
startIcon={<CheckIcon />}
fullWidth
sx={{ borderRadius: '20px' }}
disabled={!isSuccess}
aria-label="Submit button"
>
Submit
</Button>
</Grid>
</Grid>
</form>
</FormSection>
);
};

export default BasicInfo;
1 change: 1 addition & 0 deletions src/components/CivicProfileForms/HousingInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const HousingInfo = () => {
const { name, value } = event.target;
setFormData((prevFormData) => ({ ...prevFormData, [name]: value }));
};

const handleSubmit = (e) => {
e.preventDefault();
if (!isSuccess || !storedDataset) {
Expand Down
81 changes: 77 additions & 4 deletions test/components/CivicProfileForms/BasicInfo.test.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,84 @@
import React from 'react';
import { render } from '@testing-library/react';
import { expect, it, describe } from 'vitest';
import { vi, expect, it, describe } from 'vitest';
import { BasicInfo } from '@components/CivicProfileForms';
import { useCivicProfile } from '@hooks';
import userEvent from '@testing-library/user-event';

describe('Basic Info Form', () => {
vi.mock('@hooks', async () => {
const actual = await vi.importActual('@hooks');

return {
...actual,
useCivicProfile: vi.fn()
};
});

describe('Basic info form', () => {
it('renders', () => {
const { getByText } = render(<BasicInfo />);
expect(getByText('Basic Info')).not.toBeNull();
useCivicProfile.mockReturnValue({ data: {}, isSuccess: true, refetch: vi.fn() });
const { getByRole } = render(<BasicInfo />);
const legalFirstName = getByRole('textbox', { name: 'Legal first name' });
expect(legalFirstName).not.toBeNull();
});

it('clears all input fields when you click the clear button', async () => {
// const user = userEvent.setup();
const mockClear = vi.fn();
const basicInfoProfile = {
legalFirstName: 'Jane',
legalLastName: 'Doe',
legalDOB: '1980-12-15',
legalGender: 1
};
useCivicProfile.mockReturnValue({
add: mockClear,
isSuccess: true,
storedDataset: {},
refetch: vi.fn()
});
const { getByRole } = render(<BasicInfo />);
const legalFirstName = getByRole('textbox', { name: 'Legal first name' });
const legalLastName = getByRole('textbox', { name: 'Legal last name' });
const legalDOB = getByRole('textbox', { name: 'Choose date' });
const legalGender = getByRole('combobox', { name: 'Gender' });
const clearButton = getByRole('button', { name: 'Clear button' });

await userEvent.type(legalFirstName, basicInfoProfile.legalFirstName);
await userEvent.type(legalLastName, basicInfoProfile.legalLastName);
await userEvent.type(legalDOB, basicInfoProfile.legalDOB);
await userEvent.type(legalGender, `${basicInfoProfile.legalGender}{enter}`);

await userEvent.click(clearButton);

expect(legalFirstName.value).toBe('');
expect(legalLastName.value).toBe('');
expect(legalDOB.value).toBe(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this test is failing because the input field for the date picker is not your typical text field. You can't use the same approach to test the data picker. The same would be said for legalGender which is a select field.

If you'd like, we can open up another issue to write unit tests for these inputs.

expect(legalGender.value).toBe('');
});

// TODO: Resolve test not passing
// it('submits a basic info profile update when you click the submit button', async () => {
// const user = userEvent.setup();
// const mockAdd = vi.fn();
// const basicInfoProfile = {
// legalFirstName: 'Jane',
// legalLastName: 'Doe',
// legalDOB: '1980-12-15',
// legalGender: 1
// };
// useCivicProfile.mockReturnValue({ add: mockAdd, isSuccess: true });
// const { getByRole } = render(<BasicInfo />);
// const legalFirstName = getByRole('textbox', { name: 'Legal first name' });
// const legalLastName = getByRole('textbox', { name: 'Legal last name' });
// const legalDOB = getByRole('textbox', { name: 'Choose date' });
// const legalGender = getByRole('combobox', { name: 'Gender' });
// const submitButton = getByRole('button', { name: 'Submit button' });
// await user.type(legalFirstName, basicInfoProfile.legalFirstName);
// await user.type(legalLastName, basicInfoProfile.legalLastName);
// await user.type(legalDOB, basicInfoProfile.legalDOB);
// await user.type(legalGender, `${basicInfoProfile.legalGender}{enter}`);
// await user.click(submitButton);
// expect(mockAdd).toBeCalledWith(basicInfoProfile);
// });
});
Loading