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

fix: [LINKER-146] 타임라인 > 새로고침 시 api 무한 호출 이슈 #86

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions services/web/src/api/timeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { GetTimelineRes } from '@app/friend/timeline/types/schedule';
import { ky } from '@linker/ky';

export const getPrevTimeline = () => {
const res = ky.get<GetTimelineRes>('/v1/schedules/near-term?limit=32&type=PREV');

return res;
};
export const getUpcomingTimeline = () => {
const res = ky.get<GetTimelineRes>('/v1/schedules/near-term?limit=32&type=UPCOMING');

return res;
};

export const getSearchSchedule = (from: string, to: string, limit: number) => {
const response = ky.get<GetTimelineRes>(
`/v1/schedules/search?from=${from}&to=${to}&limit=${limit}`,
);

return response;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react-hooks/exhaustive-deps */
'use client';

import { Calendar, Spacing } from '@linker/lds';
Expand All @@ -6,19 +7,25 @@ import { colors } from '@linker/styles';
import { format, startOfMonth, endOfMonth } from 'date-fns';
import { useSetAtom } from 'jotai';
import { useRouter } from 'next/navigation';
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';

import { timelineItemWrapper, timelineMonthWrapper } from './TimelineDefault.css';
import { useGetPrevSchedule, useGetUpComingSchedule } from '../../hooks/useGetNearSchedule';
import { useGetSearchSchedule } from '../../hooks/useGetSearchSchedule';
import { selectDateAtom } from '../../stores/store';
import { TimelineItemProps } from '../../types/schedule';
import { GetTimelineRes, TimelineItemProps } from '../../types/schedule';
import TimelineItem from '../TimelineItem/TimelineItem';
import TimelineNull from '../TimelineNull/TimelineNull';

interface TimelineDefaultProps {
concatSchedules: TimelineItemProps[];
prevData: GetTimelineRes;
upcomingData: GetTimelineRes;
}

const TimelineDefault = ({ concatSchedules }: TimelineDefaultProps) => {
const TimelineDefault = ({ prevData, upcomingData }: TimelineDefaultProps) => {
const { data: prevDataRes } = useGetPrevSchedule(prevData);
const { data: upcomingDataRes } = useGetUpComingSchedule(upcomingData);

const router = useRouter();
const setAtomDate = useSetAtom(selectDateAtom);

Expand All @@ -29,21 +36,28 @@ const TimelineDefault = ({ concatSchedules }: TimelineDefaultProps) => {
const [dropdownClick, setDropdownClick] = useState(-1);
const [calendarDotData, setCalendarDotData] = useState<Array<Date | string>>([]);

const concatSchedules: TimelineItemProps[] = [
...new Set([...prevDataRes.schedules, ...upcomingDataRes.schedules]),
];

const startDateYear = format(concatSchedules[0].startDateTime, 'yyyy');
const firstDayOfMonth = format(startOfMonth(date), 'yyyy-MM-dd 00:00:00');
const lastDayOfMonth = format(endOfMonth(date), 'yyyy-MM-dd 23:59:59');
const { data } = useGetSearchSchedule(firstDayOfMonth, lastDayOfMonth, 32);

// 받아온 데이터들 중 다른 연도가 있는지
// 연도가 하나라도 다른게 판단이 되면 diffYear가 true가됨
const hasDifferentYear = concatSchedules.some((item) => {
const formattedYear = format(item.startDateTime, 'yyyy');
const hasDifferentYear = useMemo(() => {
return concatSchedules.some((item) => {
const formattedYear = format(item.startDateTime, 'yyyy');

return formattedYear === startDateYear;
});
}, []);

return formattedYear === startDateYear;
});
// 연도가 다른 원소의 첫번째 인덱스를 리턴
const diffIdx = concatSchedules.findIndex((item, index) => {
return format(item.startDateTime, 'yyyy').toString() !== startDateYear.toString();
return format(item.startDateTime, 'yyyy') !== startDateYear;
});

useEffect(() => {
Expand All @@ -53,21 +67,27 @@ const TimelineDefault = ({ concatSchedules }: TimelineDefaultProps) => {
// nextYear에는 diffIdx부터 끝까지의 원소 저장
setNextYear(concatSchedules.slice(diffIdx));
}, [diffIdx, hasDifferentYear, concatSchedules]);

useEffect(() => {
setAtomDate(format(date, 'yyyy-MM-dd'));
}, [date, router, setAtomDate]);

useEffect(() => {
if (selectDate) {
router.push('/my/timeline/search');
}
}, [selectDate, router]);

useEffect(() => {
let tempData: Array<string | Date> = [];
const tempData: Array<string | Date> = data.schedules.map((item) => item.startDateTime);

tempData = [...tempData, ...data.schedules.map((item) => item.startDateTime)];
setCalendarDotData(tempData);
}, []);

if (concatSchedules.length === 0) {
return <TimelineNull />;
}

return (
<>
<Calendar
Expand Down
22 changes: 6 additions & 16 deletions services/web/src/app/my/timeline/hooks/useGetNearSchedule.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
'use client';
import { kyClient } from '@linker/ky';
import { getPrevTimeline, getUpcomingTimeline } from '@api/timeline';
import { useSuspenseQuery } from '@tanstack/react-query';

import { GetTimelineRes } from '../types/schedule';

const getPrevTimeline = () => {
const res = kyClient.get<GetTimelineRes>('/v1/schedules/near-term?limit=32&type=PREV');

return res;
};
const getUpcomingTimeline = () => {
const res = kyClient.get<GetTimelineRes>('/v1/schedules/near-term?limit=32&type=UPCOMING');

return res;
};

const useGetPrevSchedule = () => {
const useGetPrevSchedule = (data: GetTimelineRes) => {
return useSuspenseQuery<GetTimelineRes>({
queryKey: useGetPrevSchedule.getKey(),
queryFn: () => getPrevTimeline(),
initialData: data,
});
};
const useGetUpComingSchedule = () => {
const useGetUpComingSchedule = (data: GetTimelineRes) => {
return useSuspenseQuery<GetTimelineRes>({
queryKey: useGetUpComingSchedule.getKey(),
queryFn: () => getUpcomingTimeline(),
initialData: data,
});
};

useGetPrevSchedule.getKey = () => {
return ['/api/v1/schedules/near-term'];
};
useGetUpComingSchedule.getKey = () => {
return ['/api/v1/schedules/near-term'];
return ['/api/v1/schedules/near-term/upcoming'];
};

export { useGetPrevSchedule, useGetUpComingSchedule };
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
'use client';
import { kyClient } from '@linker/ky';
import { getSearchSchedule } from '@api/timeline';
import { useSuspenseQuery } from '@tanstack/react-query';

import { GetTimelineRes } from '../types/schedule';

const getSearchSchedule = (from: string, to: string, limit: number) => {
const response = kyClient.get<GetTimelineRes>(
`/v1/schedules/search?from=${from}&to=${to}&limit=${limit}`,
);

return response;
};

const useGetSearchSchedule = (from: string, to: string, limit: number) => {
return useSuspenseQuery<GetTimelineRes>({
queryKey: useGetSearchSchedule.getKey(from, to, limit),
Expand Down
22 changes: 5 additions & 17 deletions services/web/src/app/my/timeline/page.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
'use client';
import { getPrevTimeline, getUpcomingTimeline } from '@api/timeline';

import TimelineDefault from './component/TimelineDefault/TimelineDefault';
import TimelineNull from './component/TimelineNull/TimelineNull';
import { useGetPrevSchedule, useGetUpComingSchedule } from './hooks/useGetNearSchedule';

export default function TimelinePage() {
const { data: prevData } = useGetPrevSchedule();
const { data: upcomingData } = useGetUpComingSchedule();
const concatSchedules = [];

concatSchedules.push(prevData.schedules);
concatSchedules.push(upcomingData.schedules);

const uniqueSchedules = [...new Set(concatSchedules)];
export default async function TimelinePage() {
const prevData = await getPrevTimeline();
const upcomingData = await getUpcomingTimeline();

return (
<>
{uniqueSchedules.length === 0 ? (
<TimelineNull />
) : (
<TimelineDefault concatSchedules={uniqueSchedules[0]} />
)}
<TimelineDefault prevData={prevData} upcomingData={upcomingData} />
</>
);
}
Loading