From 2601bf9124ad5a74adccc8f02f7b7d14ac42049f Mon Sep 17 00:00:00 2001 From: nangniya Date: Fri, 5 Apr 2024 16:01:03 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20=EB=82=A0=EC=A7=9C=20=ED=98=95?= =?UTF-8?q?=EC=8B=9D=20=ED=8F=AC=EB=A7=B7=20=ED=95=A8=EC=88=98=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC,=20=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20useMemo=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useCalendar.ts | 58 +++++++++++++++++++------------------ src/utils/dateFormatters.ts | 16 ++++++++++ 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/hooks/useCalendar.ts b/src/hooks/useCalendar.ts index 7a24608..519d454 100644 --- a/src/hooks/useCalendar.ts +++ b/src/hooks/useCalendar.ts @@ -1,8 +1,12 @@ import { getDaysInMonth } from 'date-fns'; -import { useEffect, useMemo, useState } from 'react'; +import { useEffect, useState } from 'react'; import { useQuery } from 'react-query'; import { getCalendarData } from '../apis/home'; import useCalendarStore from '../stores/calendarStore'; +import { + formatCalendarListDay, + formatRequestCalendarDate, +} from '../utils/dateFormatters'; interface IChatData { dates: string; @@ -15,42 +19,44 @@ export interface ICalendar { character: string | null; } -const DATE_MONTH_FIXER = 1; const CALENDER_LENGTH = 42; const DEFAULT_TRASH_VALUE = 0; const DAY_OF_WEEK = 7; -const TWO_DIGIT_FORMAT = 10; const useCalendar = () => { const { currentDate, setCurrentDate } = useCalendarStore(); const [formattedDate, setFormattedDate] = useState( - `${currentDate.getFullYear()}-${ - currentDate.getMonth() + 1 < TWO_DIGIT_FORMAT - ? '0' + (currentDate.getMonth() + 1) - : currentDate.getMonth() + 1 - }`, + formatRequestCalendarDate( + currentDate.getFullYear(), + currentDate.getMonth(), + ), ); const totalMonthDays = getDaysInMonth(currentDate); const [chatData, setChatData] = useState([]); useEffect(() => { const year = currentDate.getFullYear(); - const month = currentDate.getMonth() + 1; - setFormattedDate( - `${year}-${month < TWO_DIGIT_FORMAT ? '0' + month : month}`, - ); + const month = currentDate.getMonth(); + const date = formatRequestCalendarDate(year, month); + setFormattedDate(date); }, [currentDate]); - const { data, isLoading, error } = useQuery( - ['calendarData', formattedDate], - () => getCalendarData(formattedDate), - ); + const { data, isLoading, error } = useQuery({ + queryKey: ['calendarData', formattedDate], + queryFn: () => getCalendarData(formattedDate), + }); useEffect(() => { - if (!isLoading && !error && data) { + if (data) { setChatData(data); } - }, [data, isLoading, error]); + }, [data]); + if (isLoading) { + console.log('calendar Loading'); + } + if (error) { + console.log(error); + } const firstDayOfMonth = new Date( currentDate.getFullYear(), @@ -75,16 +81,12 @@ const useCalendar = () => { if (!acc[chunkIndex]) { acc[chunkIndex] = []; } - const currentDateStr = `${currentDate.getFullYear()}-${ - currentDate.getMonth() + DATE_MONTH_FIXER < 10 - ? '0' + (currentDate.getMonth() + DATE_MONTH_FIXER) - : currentDate.getMonth() + DATE_MONTH_FIXER - }-${cur < 10 ? '0' + cur : cur}`; - const chatInfo = useMemo(() => { - if (chatData && chatData.length > 0) { - return chatData.find((chat) => chat.dates === currentDateStr); - } - }, [chatData, currentDateStr]); + const currentDateStr = formatCalendarListDay( + currentDate.getFullYear(), + currentDate.getMonth(), + cur, + ); + const chatInfo = chatData.find((chat) => chat.dates === currentDateStr); acc[chunkIndex].push({ day: cur, character: diff --git a/src/utils/dateFormatters.ts b/src/utils/dateFormatters.ts index d216f54..cb18add 100644 --- a/src/utils/dateFormatters.ts +++ b/src/utils/dateFormatters.ts @@ -39,3 +39,19 @@ export const isNotToday = ( dayInfo.day !== today.getDate() ); }; + +//YYYY-MM 형태 +export const formatRequestCalendarDate = (year: number, month: number) => { + return `${year}-${month + 1 < 10 ? '0' + (month + 1) : month + 1}`; +}; + +//YYYY-MM-DD 형태 +export const formatCalendarListDay = ( + year: number, + month: number, + day: number, +) => { + return `${year}-${month + 1 < 10 ? '0' + (month + 1) : month + 1}-${ + day < 10 ? '0' + day : day + }`; +};