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

bugfix/1915-Profile-Birthdate-Validation-Message #1952

Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions src/common/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const colors = {
white: {
main: '#ffffff',
},
red: {
error: '#ED1D1D',
},
}

const borders = {
Expand Down Expand Up @@ -67,6 +70,9 @@ export const themeOptions: ThemeOptions = {
light: colors.blue.mainDark,
dark: darken(colors.blue.dark, 0.2),
},
error: {
main: colors.red.error,
},
},
shape: {
borderRadius: 3,
Expand Down
28 changes: 16 additions & 12 deletions src/components/client/auth/profile/UpdateBirthdateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ const parseDateString = (value: string, originalValue: string) => {

const maxDate = new Date(new Date().setFullYear(new Date().getFullYear() - 18))

const validationSchema: yup.SchemaOf<Pick<UpdateUserAccount, 'birthday'>> = yup
.object()
.defined()
.shape({
birthday: yup
.date()
.transform(parseDateString)
.max(maxDate, 'profile:birthdateModal.ageInvalid')
.required(),
})

function UpdateBirthdateModal({
isOpen,
handleClose,
Expand All @@ -76,6 +65,17 @@ function UpdateBirthdateModal({
const { t } = useTranslation()
const [loading, setLoading] = useState(false)

const validationSchema: yup.SchemaOf<Pick<UpdateUserAccount, 'birthday'>> = yup
.object()
.defined()
.shape({
birthday: yup
.date()
.transform(parseDateString)
.max(maxDate, t('profile:birthdateModal.ageInvalid'))
.required(),
})

const dateBefore18Years = new Date(new Date().setFullYear(new Date().getFullYear() - 18))

const initialValues: Pick<UpdateUserAccount, 'birthday'> = {
Expand Down Expand Up @@ -119,7 +119,11 @@ function UpdateBirthdateModal({
validationSchema={validationSchema}>
<Grid container spacing={3}>
<Grid item xs={12} sm={8}>
<FormDatePicker name="birthday" label={t('profile:birthdateModal.question')} />
<FormDatePicker
name="birthday"
label={t('profile:birthdateModal.question')}
maxDate={maxDate}
/>
</Grid>
<Grid item xs={6}>
<SubmitButton fullWidth label="auth:cta.send" loading={loading} />
Expand Down
28 changes: 25 additions & 3 deletions src/components/common/form/FormDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'
import { format } from 'date-fns'
import { useField, useFormikContext } from 'formik'
import { useTranslation } from 'next-i18next'
import theme from 'common/theme'

import { DATE_VALUE_FORMAT, getDateFormat } from 'common/util/date'

interface FormDatePickerProps {
name: string
label: string
maxDate?: Date
}

const errorValidationColor = theme.palette.error as unknown as string

/**
* MUI date picker to be connected with Formik. Propagates updates to the passed Formik field name
* @param name - name of the Formik field to bind
* @param label - prompt text
* @param maxDate - optional maximal selectable date
* @returns
*/
export default function FormDatePicker({ name, label }: { name: string; label: string }) {
const [field] = useField(name)
export default function FormDatePicker({ name, label, maxDate }: FormDatePickerProps) {
const [field, meta] = useField(name)
const { setFieldValue } = useFormikContext()
const { i18n } = useTranslation()

Expand Down Expand Up @@ -42,7 +52,19 @@ export default function FormDatePicker({ name, label }: { name: string; label: s
label={label}
value={new Date(field?.value)}
onChange={(newValue) => updateValue(newValue)}
slotProps={{ textField: { size: 'small' } }}
slotProps={{ textField: { size: 'small', helperText: maxDate && meta.error } }}
sx={
maxDate && meta.error
? {
'&.MuiOutlinedInput-root': {
'& fieldset, &:hover fieldset, &.Mui-focused fieldset': {
borderColor: errorValidationColor,
},
},
}
: undefined
}
maxDate={maxDate}
/>
</LocalizationProvider>
)
Expand Down
Loading