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

Updated the calender to not accomodate future dates #2865

Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 31 additions & 0 deletions src/screens/UserPortal/Settings/Settings.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,34 @@ describe('Testing Settings Screen [User Portal]', () => {
});
});
});

it('prevents selecting future dates for birth date', async () => {
await act(async () => {
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Settings />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);
});

const birthDateInput = screen.getByLabelText(
'Birth Date',
) as HTMLInputElement;
const today = new Date().toISOString().split('T')[0];

// Trying future date
fireEvent.change(birthDateInput, { target: { value: '2100-01-01' } });
palisadoes marked this conversation as resolved.
Show resolved Hide resolved

// Checking if value is not updated to future date
expect(birthDateInput.value).not.toBe('2100-01-01');
palisadoes marked this conversation as resolved.
Show resolved Hide resolved

// Checking if value set correctly
fireEvent.change(birthDateInput, { target: { value: today } });
expect(birthDateInput.value).toBe(today);
});
14 changes: 14 additions & 0 deletions src/screens/UserPortal/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ export default function settings(): JSX.Element {
* @param value - The new value for the field.
*/
const handleFieldChange = (fieldName: string, value: string): void => {
// If the field is 'birthDate', validate the date
if (fieldName === 'birthDate') {
const today = new Date();
const selectedDate = new Date(value);

// Prevent updating the state if the selected date is in the future
if (selectedDate > today) {
console.error('Future dates are not allowed for the birth date.');
return; // Exit without updating the state
}
}

// Update state if the value passes validation
setisUpdated(true);
setUserDetails((prevState) => ({
...prevState,
Expand Down Expand Up @@ -449,6 +462,7 @@ export default function settings(): JSX.Element {
handleFieldChange('birthDate', e.target.value)
}
className={`${styles.cardControl}`}
max={new Date().toISOString().split('T')[0]}
/>
</Col>
</Row>
Expand Down
Loading