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

[Refactor] date-fns 라이브러리 제거 #469

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"@vanilla-extract/css": "^1.15.1",
"@vanilla-extract/css-utils": "^0.1.4",
"axios": "^1.6.8",
"date-fns": "^3.6.0",
"firebase": "^10.12.4",
"lottie-react": "^2.4.0",
"nanoid": "^5.0.7",
Expand Down Expand Up @@ -86,4 +85,4 @@
"vite": "^5.2.0",
"vitest": "^2.0.5"
}
}
}
2 changes: 1 addition & 1 deletion src/common/apis/tokenInstance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isBefore } from '@utils/dateFormatter';
import axios from 'axios';
import { isBefore } from 'date-fns/isBefore';

const tokenInstance = axios.create({
baseURL: import.meta.env.VITE_BASE_URL,
Expand Down
2 changes: 1 addition & 1 deletion src/common/components/Input/components/Timer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { differenceInSeconds } from 'date-fns/differenceInSeconds';
import { useEffect, useState } from 'react';

import { useDeviceType } from 'contexts/DeviceTypeProvider';

import { timerVar } from './style.css';
import { TimerProps } from './types';
import formatTimer from './utils/formatTimer';
import { differenceInSeconds } from '@utils/dateFormatter';

const INITIAL_TIME = 300;

Expand Down
3 changes: 1 addition & 2 deletions src/common/hooks/useDate.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { isAfter } from 'date-fns/isAfter';
import { isBefore } from 'date-fns/isBefore';
import { useEffect } from 'react';

import { useRecruitingInfo } from 'contexts/RecruitingInfoProvider';

import useGetRecruitingInfo from './useGetRecruitingInfo';
import { isAfter, isBefore } from '@utils/dateFormatter';

const useDate = () => {
const { handleSaveRecruitingInfo } = useRecruitingInfo();
Expand Down
41 changes: 41 additions & 0 deletions src/common/utils/dateFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export const isBefore = (date: Date | string, dateToCompare: Date | string): boolean => {
if (typeof date === 'string') date = new Date(date);
if (typeof dateToCompare === 'string') dateToCompare = new Date(dateToCompare);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

계속 반복되니

const toDate = (date: Date | string): Date => {
  return typeof date === 'string' ? new Date(date) : date;
};

따로 분리해줘도 좋을 거 같아요

Copy link
Member

@eonseok-jeon eonseok-jeon Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가로 유효하지 않은 date 값이 들어왔을 때 예외 처리도 toDate에서 같이 해주면 좋을 거 같아요~

Copy link
Member Author

@lydiacho lydiacho Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용자가 확인해야할 내용은 아니니 페이지 터뜨리지 않게 throw Error말고 console error로 처리했어요.
어떠신가요!

const toDate = (date: Date | string): Date => {
  const newDate = typeof date === 'string' ? new Date(date) : date;
  if (isNaN(newDate.getTime())) {
    console.error(`${date} is invalid date.`);
  }
  return newDate;
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

차피 서버에서 데이터 받아오는 거기 때문에 사용자에게 에러를 보게될 일은 없을 거 같아요
코드를 유지보수 하면서 서버에 받아온 데이터를 저희가 처리하는 과정에서 에러가 발생될 수 있는데 이때 console로만 찍으면 이를 인지하지 못하고 넘어갈 수 있겠다는 우려가 되네요
그래서 throw가 더 좋을 거 같단 생각입니다 :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 잘 이해하지 못했는데, 왜 사용자가 에러를 보지 못하게 되나요??
에러가 throw 되면 저희 구현상 에러페이지로 빠지게 되는 상태인데, 만약 사용자가 접속했을 때 서버로 받아온 날짜 데이터에 하나라도 비정상적인 값 (혹은 undefined)가 들어오면 에러페이지로 이동하게 되는것 아닌가요??

실제로 지금 비정상적인 서버 환경에서 throw Error로 처리했더니 아예 무조건 에러페이지로만 가버려서 UI를 보면서 개발을 할 수가 없더라구요 ㅠ ㅠ 그래서 저는 최소한의 UI는 확인해볼 수 있게 콘솔로만 남겼던겁니다!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 잘 이해하지 못했는데, 왜 사용자가 에러를 보지 못하게 되나요??
에러가 throw 되면 저희 구현상 에러페이지로 빠지게 되는 상태인데, 만약 사용자가 접속했을 때 서버로 받아온 날짜 데이터에 하나라도 비정상적인 값 (혹은 undefined)가 들어오면 에러페이지로 이동하게 되는것 아닌가요??

서버에서 잘못된 값을 보내면 안 되니까요! 정상적인 데이터만 서버에서 넘겨줄 테니 에러가 발생하지 않을 거예요
하나, 서버에서 잘못된 값을 보냈다고 해도, 이상하게 처리되어서 잘못된 숫자가 찍히게 되어 사용자에게 혼동을 주기 보다 차라리 에러 터트리는 게 더 나을 거 같다는 생각이에요 :)

실제로 지금 비정상적인 서버 환경에서 throw Error로 처리했더니 아예 무조건 에러페이지로만 가버려서 UI를 보면서 개발을 할 수가 없더라구요 ㅠ ㅠ 그래서 저는 최소한의 UI는 확인해볼 수 있게 콘솔로만 남겼던겁니다!!

이건 사실 이 뿐만 아니라 어느 에러가 발생해도 다 똑같아요
에러가 발생할 시 500 페이지로 이동하도록 공통적으로 처리해놔서 그런데요
개발하는 동안은 이를 잠시 주석 처리 하는 등 하면 될 거 같다는 생각입니다~

근데 뭐,, 중요한 거 아니니까 편한대로 해주세여~

Copy link
Member Author

@lydiacho lydiacho Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아아 그부분은 저도 동의해요 전 아예 에러페이지로 이동 안한다는 의미라고 이해했어요
그럼 throw로 바꾸고 주석처리해둘게요!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인했습니다! 고생하셨어요 :)

return date.getTime() < dateToCompare.getTime();
};

export const isAfter = (date: Date | string, dateToCompare: Date | string): boolean => {
if (typeof date === 'string') date = new Date(date);
if (typeof dateToCompare === 'string') dateToCompare = new Date(dateToCompare);
return date.getTime() > dateToCompare.getTime();
};

export const differenceInSeconds = (laterDate: Date | string, earlierDate: Date | string): number => {
if (typeof laterDate === 'string') laterDate = new Date(laterDate);
if (typeof earlierDate === 'string') earlierDate = new Date(earlierDate);
return Math.floor((laterDate.getTime() - earlierDate.getTime()) / 1000);
};

export const subMinutes = (date: Date | string, amount: number): Date => {
if (typeof date === 'string') date = new Date(date);
const newDate = new Date();
newDate.setTime(date.getTime() - amount * 60 * 1000);
return newDate;
};
lydiacho marked this conversation as resolved.
Show resolved Hide resolved

export const format = (date: Date | string, formatStr: string): string => {
if (typeof date === 'string') date = new Date(date);
const days = ['월', '화', '수', '목', '금', '토', '일'];
const formatter: { [key: string]: string } = {
M: (date.getMonth() + 1).toString(),
dd: date.getDate().toString().padStart(2, '0'),
E: days[date.getDay() - 1],
EEEE: days[date.getDay() - 1] + '요일',
lydiacho marked this conversation as resolved.
Show resolved Hide resolved
aaa: date.getHours() < 12 ? '오전' : '오후',
HH: date.getHours().toString().padStart(2, '0'),
hh: (date.getHours() % 12 || 12).toString().padStart(2, '0'),
mm: date.getMinutes().toString().padStart(2, '0'),
};

return formatStr.replace(/M|dd|E|EEE|aaa|HH|hh|mm/g, (substr) => formatter[substr]);
};
21 changes: 6 additions & 15 deletions src/views/ApplyPage/components/ApplyInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { format } from 'date-fns/format';
import { ko } from 'date-fns/locale/ko';
import { subMinutes } from 'date-fns/subMinutes';
import { memo } from 'react';

import Callout from '@components/Callout';
Expand All @@ -18,6 +15,7 @@ import {
infoWrapperVar,
} from './style.css';
import { APPLY_INFO } from '../../constant';
import { format, subMinutes } from '@utils/dateFormatter';

const ApplyInfo = memo(({ isReview = false }: { isReview?: boolean }) => {
const { deviceType } = useDeviceType();
Expand All @@ -34,22 +32,15 @@ const ApplyInfo = memo(({ isReview = false }: { isReview?: boolean }) => {

if (!applicationStart) return;

const formattedApplicationStart = format(new Date(applicationStart || ''), 'M월 dd일 (E) aaa HH시 mm분', {
locale: ko,
});
const formattedApplicationEnd = format(subMinutes(new Date(applicationEnd || ''), 1), 'M월 dd일 (E) aaa HH시 mm분', {
locale: ko,
});
const formattedApplicationStart = format(new Date(applicationStart || ''), 'M월 dd일 (E) aaa HH시 mm분');
const formattedApplicationEnd = format(subMinutes(new Date(applicationEnd || ''), 1), 'M월 dd일 (E) aaa HH시 mm분');
const formattedApplicationConfirmStart = format(
new Date(applicationPassConfirmStart || ''),
'M월 dd일 (E) aaa HH시 mm분',
{
locale: ko,
},
);
const formattedInterviewStart = format(new Date(interviewStart || ''), 'M월 dd일 (E)', { locale: ko });
const formattedInterviewEnd = format(new Date(interviewEnd || ''), 'M월 dd일 (E)', { locale: ko });
const formattedFinalPassConfirmStart = format(new Date(finalPassConfirmStart || ''), 'M월 dd일 (E)', { locale: ko });
const formattedInterviewStart = format(new Date(interviewStart || ''), 'M월 dd일 (E)');
const formattedInterviewEnd = format(new Date(interviewEnd || ''), 'M월 dd일 (E)');
const formattedFinalPassConfirmStart = format(new Date(finalPassConfirmStart || ''), 'M월 dd일 (E)');

return (
<section className={infoContainerVar[deviceType]}>
Expand Down
7 changes: 2 additions & 5 deletions src/views/ResultPage/components/FinalResult.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { track } from '@amplitude/analytics-browser';
import { format } from 'date-fns/format';
import { ko } from 'date-fns/locale/ko';
import { useEffect } from 'react';

import Title from '@components/Title';
Expand All @@ -23,6 +21,7 @@ import IconMakersLogo from '../assets/IconMakersLogo';
import imgSoptLogo from '../assets/imgSoptLogo.png';
import imgSoptLogoWebp from '../assets/imgSoptLogo.webp';
import useGetFinalResult from '../hooks/useGetFinalResult';
import { format } from '@utils/dateFormatter';

const Content = ({ pass }: { pass?: boolean }) => {
const { deviceType } = useDeviceType();
Expand All @@ -33,9 +32,7 @@ const Content = ({ pass }: { pass?: boolean }) => {
if (!name) return;

const finalDate = new Date(finalPassConfirmStart || '');
const formattedFinalPassConfirmStart = format(finalDate, 'M월 dd일 EEEE', {
locale: ko,
});
const formattedFinalPassConfirmStart = format(finalDate, 'M월 dd일 EEEE');

const SOPT_NAME = isMakers ? `SOPT ${soptName}` : soptName;
return (
Expand Down
15 changes: 5 additions & 10 deletions src/views/ResultPage/components/ScreeningResult.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { track } from '@amplitude/analytics-browser';
import { format } from 'date-fns/format';
import { ko } from 'date-fns/locale/ko';
import { useEffect } from 'react';

import Title from '@components/Title';
Expand All @@ -23,6 +21,7 @@ import IconMakersLogo from '../assets/IconMakersLogo';
import imgSoptLogo from '../assets/imgSoptLogo.png';
import imgSoptLogoWebp from '../assets/imgSoptLogo.webp';
import useGetScreeningResult from '../hooks/useGetScreeningResult';
import { format } from '@utils/dateFormatter';

const Content = ({ pass }: { pass?: boolean }) => {
const { deviceType } = useDeviceType();
Expand All @@ -36,14 +35,10 @@ const Content = ({ pass }: { pass?: boolean }) => {
const applicationPassConfirmNextDay = new Date(applicationDate);
applicationPassConfirmNextDay.setDate(applicationDate.getDate() + 1);

const formattedInterviewStart = format(new Date(interviewStart || ''), 'M월 dd일', { locale: ko });
const formattedInterviewEnd = format(new Date(interviewEnd || ''), 'M월 dd일', { locale: ko });
const formattedApplicationPassConfirmStart = format(applicationDate, 'M월 dd일 EEEE', {
locale: ko,
});
const formattedApplicationPassConfirmNextDay = format(new Date(applicationPassConfirmNextDay || ''), 'M월 dd일', {
locale: ko,
});
const formattedInterviewStart = format(new Date(interviewStart || ''), 'M월 dd일');
const formattedInterviewEnd = format(new Date(interviewEnd || ''), 'M월 dd일');
const formattedApplicationPassConfirmStart = format(applicationDate, 'M월 dd일 EEEE');
const formattedApplicationPassConfirmNextDay = format(new Date(applicationPassConfirmNextDay || ''), 'M월 dd일');

const SOPT_NAME = isMakers ? `SOPT ${soptName}` : soptName;
return (
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2514,11 +2514,6 @@ data-view-byte-offset@^1.0.0:
es-errors "^1.3.0"
is-data-view "^1.0.1"

date-fns@^3.6.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-3.6.0.tgz#f20ca4fe94f8b754951b24240676e8618c0206bf"
integrity sha512-fRHTG8g/Gif+kSh50gaGEdToemgfj74aRX3swtiouboip5JDLAyDE9F11nHMIcvOaXeOC6D7SpNhi7uFyB7Uww==

debug@4, debug@^4.3.5:
version "4.3.6"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.6.tgz#2ab2c38fbaffebf8aa95fdfe6d88438c7a13c52b"
Expand Down