Skip to content

Commit

Permalink
fix: use dayjs for converting DateObj to Date
Browse files Browse the repository at this point in the history
  • Loading branch information
ooooorobo committed Sep 23, 2024
1 parent 4f5190f commit f42a30e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/entities/profile/api/__mock__/profile.mock.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ProfileSummary } from 'src/entities/profile/types/profileSummary';
import dayjs from 'dayjs';

export const profileMock: ProfileSummary = {
name: '강이름',
birthDate: new Date('1996-05-15').toString(),
birthDate: dayjs('1996/05/15').toString(),
gender: 'MALE',
height: 160,
hobbies: ['뜨개질'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { getReligionText } from '../../../lib/getReligionText';
import { AvatarList } from '../../../../../shared/ui/AvatarList/AvatarList';
import { getJobText } from '../../../lib/getJobText';
import { getLocationText } from '../../../lib/getLocationText';
import { calculateAge } from '../../../../../shared/vo/date';
import { calculateAge, convertDateObjectToDate } from '../../../../../shared/vo/date';
import { useTranslation } from 'react-i18next';
import { MyProfile } from '../../../model/myProfileStore';

export const PersonalInfoGrid = ({ profile }: { profile: MyProfile }) => {
const { t } = useTranslation();
const age = calculateAge(new Date(`${profile.birthDate.year}-${profile.birthDate.month}-${profile.birthDate.date}`));
const age = calculateAge(convertDateObjectToDate(profile.birthDate));
return (
<div className={styles.Grid}>
<div className={styles.GridRow}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import styles from 'src/processes/my_profile/PersonalInfoForm/PersonalInfoForm.module.css';
import { Select } from 'src/shared/ui/Select/Select';
import { useMemo } from 'react';
import { useMyProfileStore } from 'src/entities/profile/model/myProfileStore';
import { calculateAge } from 'src/shared/vo/date';
import { useProfileAge } from 'src/entities/profile/lib/useProfileAge';

const MINIMUM_YEAR = 1980;
const YearOptionList = Array.from({ length: new Date().getFullYear() - MINIMUM_YEAR + 1 })
Expand All @@ -20,22 +19,18 @@ export const BirthDateForm = () => {
const year = useMyProfileStore((state) => state.birthDate?.year)?.toString();
const month = useMyProfileStore((state) => state.birthDate?.month)?.toString();
const date = useMyProfileStore((state) => state.birthDate?.date)?.toString();
const age = useProfileAge();
const setYear = useMyProfileStore((state) => state.setBirthYear);
const setMonth = useMyProfileStore((state) => state.setBirthMonth);
const setDate = useMyProfileStore((state) => state.setBirthDate);
const calculatedAge = useMemo(() => {
if (!year || !month || !date) {
return '--';
}

return calculateAge(new Date(`${year}-${month}-${date}`));
}, [year, month, date]);
const hasFullDate = year && month && date;

return (
<fieldset>
<legend className={styles.Legend}>나이</legend>
<div className={styles.InputGroup}>
<span className={styles.AgeInfo}>{calculatedAge}</span>
<span className={styles.AgeInfo}>{hasFullDate ? age : '--'}</span>
<Select required value={year ?? ''} placeholder={'YYYY'} onValueChange={(value) => setYear(Number(value))}>
{YearOptionList.map((year) => (
<Select.Item key={year} value={year} text={year.toString()} />
Expand Down
2 changes: 1 addition & 1 deletion src/shared/vo/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const isValidDate = (date: Partial<DateObj>) => {
export const convertDateObjectToDate = ({ year, month, date }: Partial<DateObj>) => {
if (!year || !month || !date) return new Date();

return new Date(`${year}-${month}-${date}`);
return dayjs(`${year}/${month}/${date}`).toDate();
};

export const convertDateToDateObject = (date: Date): DateObj => {
Expand Down

0 comments on commit f42a30e

Please sign in to comment.