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

Age validation #549

Merged
merged 5 commits into from
Apr 22, 2024
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
43 changes: 37 additions & 6 deletions FU.SPA/src/components/pages/ProfileSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function ProfileSettings() {
const { refreshUser } = useContext(UserContext);
const [isEnabled, setIsEnabled] = useState(false);
const [bioError, setBioError] = useState('');
const [dateError, setDateError] = useState('');

useEffect(() => {
async function fetchUserInfo() {
Expand All @@ -45,10 +46,10 @@ export default function ProfileSettings() {

fetchUserInfo();
}, []);

// Shows or hides the update profile button
useEffect(() => {
setIsEnabled(bio.length <= 1500);
}, [bio, isEnabled]);
setIsEnabled(bio.length <= 1500 && !dateError);
}, [bio, isEnabled, dateError]);

// Handles the errors and value changes for the bio(About) section.
const handleBioChange = (e) => {
Expand All @@ -60,6 +61,30 @@ export default function ProfileSettings() {
setBio(e);
}
};
// Handles the errors and value changes for the date of birth section.
const handleDOBChange = (e) => {
try {
const today = dayjs();
const ageEntered = today.diff(e, 'year');
console.log(ageEntered);
// allows clearing date of birth
if (e == null) {
setDateError('');
setDateOfBirth(e);
} else if (ageEntered < 13) {
setDateError('You must be at least 13 years old.');
setDateOfBirth(e);
} else if (ageEntered > 120) {
setDateError('You must enter an age less than 120 years old.');
setDateOfBirth(e);
} else {
setDateError('');
setDateOfBirth(e);
}
} catch (e) {
console.error('Error in DOB change: ', e);
}
};

const handleSubmit = async (e) => {
e.preventDefault();
Expand Down Expand Up @@ -151,10 +176,16 @@ export default function ProfileSettings() {
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
label="Date of Birth"
value={dateOfBirth} // Leave null as to not change date
value={dateOfBirth}
fullWidth
onChange={(newValue) => setDateOfBirth(newValue)}
slotProps={{ field: { clearable: true } }}
onChange={(newValue) => handleDOBChange(newValue)}
slotProps={{
field: {
clearable: true,
helperText: dateError,
error: !!dateError,
},
}}
/>
</LocalizationProvider>
<TextField
Expand Down
Loading