Skip to content

Commit

Permalink
chore: profile registration limitation of birthday
Browse files Browse the repository at this point in the history
  • Loading branch information
whalekiller03 committed Oct 11, 2024
1 parent 5829469 commit 8e9ec72
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
14 changes: 13 additions & 1 deletion components/Profile/Edit/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect } from 'react';
import dayjs from 'dayjs';
import toast from 'react-hot-toast';
import useMediaQuery from '@mui/material/useMediaQuery';
import { useRouter } from 'next/router';
Expand Down Expand Up @@ -69,6 +70,9 @@ function EditPage() {
onChangeHandler({ key: 'facebook', value: facebook || '' });
onChangeHandler({ key: 'discord', value: discord || '' });
onChangeHandler({ key: 'line', value: line || '' });
} else if (key === 'birthDay') {
const parsedDate = dayjs(value);
onChangeHandler({ key: 'birthDay', value: parsedDate });
} else if (key === 'location') {
onChangeHandler({ key, value });
const [country, city, district] = value.split('@');
Expand Down Expand Up @@ -132,8 +136,16 @@ function EditPage() {
onChangeHandler({ key: 'birthDay', value: date })
}
renderInput={(params) => (
<TextField {...params} sx={{ width: '100%' }} label="" />
<TextField
{...params}
sx={{ width: '100%' }}
label=""
error={!!errors.birthDay}
helperText={errors.birthDay ? errors.birthDay : ''}
/>
)}
maxDate={dayjs().subtract(16, 'year')}
defaultCalendarMonth={dayjs().subtract(18, 'year')}
/>
</StyledGroup>
<StyledGroup>
Expand Down
51 changes: 32 additions & 19 deletions components/Profile/Edit/useEditProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ const schema = z.object({
.min(1, { message: '請輸入名字' })
.max(50, { message: '名字過長' })
.optional(),
birthDay: z
.any()
.refine((date) => dayjs(date).isValid(), {
message: '請選擇您的出生日期',
})
.refine((date) => dayjs().diff(date, 'year') >= 16, {
message: '您的年齡未滿16歲,目前無法於平台註冊,請詳閱島島社群條款',
})
.optional(),
isOpenLocation: z.boolean().optional(),
isOpenProfile: z.boolean().optional(),
instagram: buildValidator(
Expand All @@ -73,6 +82,10 @@ const schema = z.object({
'長度最多20個字元',
'長度最少6個字元,支援英文、數字、底線、句號',
),
isLoadingSubmit: z.boolean().optional(),
tagList: z.array(z.string()).optional(),
wantToDoList: z.array(z.string()).optional(),
roleList: z.array(z.string()).optional(),
});

const userReducer = (state, payload) => {
Expand All @@ -99,26 +112,26 @@ const useEditProfile = () => {
const [errors, setErrors] = useState({});

const validate = (state = {}, isPartial = false) => {
const [key, value] = Object.entries(state)[0];
if (key !== 'birthDay') {
const result = isPartial
? schema.partial({ [key]: true }).safeParse({ [key]: value })
: schema.safeParse({ [key]: value });

if (!result.success) {
result.error.errors.forEach((err) => {
setErrors({ [err.path[0]]: err.message });
const [key, val] = Object.entries(state)[0];
const result = isPartial
? schema
.partial({ [key]: true })
.safeParse({ [key]: key === 'birthDay' ? val?.$d : val })
: schema.safeParse({
...state,
birthDay: state.birthDay.$d,
});
}
if (isPartial && result.success) {
const obj = { ...errors };
delete obj[key];
setErrors(obj);
}

return result.success;
if (!result.success) {
result.error.errors.forEach((err) => {
setErrors({ [err.path[0]]: err.message });
});
}
if (isPartial && result.success) {
const obj = { ...errors };
delete obj[key];
setErrors(obj);
}
return true;
return result.success;
};

const onChangeHandler = ({ key, value, isMultiple }) => {
Expand Down Expand Up @@ -153,7 +166,7 @@ const useEditProfile = () => {
id,
email,
name,
birthDay,
birthDay: dayjs(birthDay).format('YYYY/MM/DD'),
gender,
roleList,
contactList: {
Expand Down

0 comments on commit 8e9ec72

Please sign in to comment.