Skip to content

Commit

Permalink
fix: pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
useruseruse committed Sep 18, 2024
1 parent 0f07c2d commit d08a38f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 46 deletions.
24 changes: 12 additions & 12 deletions src/pages/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ import ReviewWriteFeedSection from '../components/sections/main/ReviewWriteFeedS
import MainSearchSection from '../components/sections/main/MainSearchSection';
import NoticeSection from '../components/sections/main/NoticeSection';
import RateFeedSection from '../components/sections/main/RateFeedSection';
import { rootReducer } from '@/App';
import { range, throttle } from 'lodash';
import { useFeed, useNotices } from '@/queries/main';

type RootState = ReturnType<typeof rootReducer>;
import { RootState } from '@/redux';

// TODO: Feed 의 타입은 이후에 API 타입 패키지로 바꿀 예정
// TODO: 스크롤 감지 interserct observer 로 바꾸기
// TODO: 추후 스크롤 감지 interserct observer 로 바꾸기

const MainPage: React.FC = () => {
const contentRef = useRef<HTMLDivElement | null>(null);
const { t } = useTranslation();

const contentRef = useRef<HTMLDivElement | null>(null);
const { user } = useSelector((state: RootState) => state.common.user);
const { isPortrait } = useSelector((state: RootState) => state.common.media);
const { t } = useTranslation();

const COLUMN_NUM = isPortrait ? 1 : 3;
const SCROLL_WAIT_TIME = 2000;

const { data: notices } = useNotices();
const { data: feedDays, isFetchingNextPage, fetchNextPage } = useFeed(user);
Expand All @@ -49,7 +51,7 @@ const MainPage: React.FC = () => {
if (isBottomReached && !isFetchingNextPage) {
fetchNextPage();
}
}, 2000);
}, SCROLL_WAIT_TIME);

useEffect(() => {
window.addEventListener('scroll', handleScroll);
Expand Down Expand Up @@ -102,8 +104,6 @@ const MainPage: React.FC = () => {
}
};

const columnNum = isPortrait ? 1 : 3;

const feeds = [
<TodaysTimetableSection key="TODAYS_TIMETABLE" />,
<AcademicScheduleSection key="ACADEMIC_SCHEDULE" />,
Expand All @@ -126,7 +126,7 @@ const MainPage: React.FC = () => {
</section>
<section className={classNames('content')} ref={contentRef}>
<div className={classNames('page-grid', 'page-grid--main')}>
{range(columnNum).map((i) => (
{range(COLUMN_NUM).map((i) => (
<div
style={{
gridArea: `feeds-column-${i + 1}`,
Expand All @@ -135,7 +135,7 @@ const MainPage: React.FC = () => {
minWidth: 0,
}}
key={i}>
{feeds.filter((_, feedIndex) => feedIndex % columnNum === i)}
{feeds.filter((_, feedIndex) => feedIndex % COLUMN_NUM === i)}
<div style={{ position: 'absolute', width: '100%' }}>
{range(10).map((j) => (
<div className={classNames('section', 'section--feed--placeholder')} key={j} />
Expand All @@ -145,7 +145,7 @@ const MainPage: React.FC = () => {
))}
<div className={classNames('main-date')}>
{user ? (
<span onClick={fetchNextPage}>
<span onClick={() => fetchNextPage()}>
{isFetchingNextPage ? t('ui.placeholder.loading') : t('ui.button.loadMore')}
</span>
) : (
Expand Down
61 changes: 27 additions & 34 deletions src/queries/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,47 @@ import User from '@/shapes/model/session/User';
import { useInfiniteQuery, useQuery } from '@tanstack/react-query';
import axios from 'axios';

Check warning on line 3 in src/queries/main.ts

View check run for this annotation

Codecov / codecov/patch

src/queries/main.ts#L2-L3

Added lines #L2 - L3 were not covered by tests

const fetchFeeds = async (userId: number, date: Date) => {
const dateString = date.toJSON().slice(0, 10);

const response = await axios.get(`/api/users/${userId}/feeds`, {
params: { date: dateString },
metadata: {
gaCategory: 'Feed',
gaVariable: 'GET / List',
},
});
return response.data;
};

const fetchNotices = async () => {
const now = new Date();
const response = await axios.get('/api/notices', {
params: {
time: now.toJSON(),
order: ['start_time', 'id'],
},
metadata: {
gaCategory: 'Notice',
gaVariable: 'GET / List',
},
});
return response.data;
};

export const useFeed = (user: User) => {

Check warning on line 5 in src/queries/main.ts

View check run for this annotation

Codecov / codecov/patch

src/queries/main.ts#L5

Added line #L5 was not covered by tests
const userId = user?.id;
return useInfiniteQuery({

Check warning on line 7 in src/queries/main.ts

View check run for this annotation

Codecov / codecov/patch

src/queries/main.ts#L7

Added line #L7 was not covered by tests
queryKey: ['feeds', userId],
queryFn: ({ pageParam }) => {
return fetchFeeds(userId, pageParam);
queryFn: async ({ pageParam }) => {
const dateString = pageParam.toJSON().slice(0, 10); // pageParam을 date로 사용
const response = await axios.get(`/api/users/${userId}/feeds`, {

Check warning on line 11 in src/queries/main.ts

View check run for this annotation

Codecov / codecov/patch

src/queries/main.ts#L9-L11

Added lines #L9 - L11 were not covered by tests
params: { date: dateString },
metadata: {
gaCategory: 'Feed',
gaVariable: 'GET / List',
},
});
return response.data;

Check warning on line 18 in src/queries/main.ts

View check run for this annotation

Codecov / codecov/patch

src/queries/main.ts#L18

Added line #L18 was not covered by tests
},
getNextPageParam: (_, __, lastPageParam) => {
const nextPageParam = new Date();
nextPageParam.setDate(lastPageParam.getDate() - 1);
nextPageParam.setDate(lastPageParam.getDate() - 1); // 다음 페이지 날짜 계산
return nextPageParam;

Check warning on line 23 in src/queries/main.ts

View check run for this annotation

Codecov / codecov/patch

src/queries/main.ts#L20-L23

Added lines #L20 - L23 were not covered by tests
},
initialPageParam: new Date(),
enabled: !!user,
initialPageParam: new Date(), // 첫 페이지는 현재 날짜
enabled: !!user, // 유저가 있을 때만 쿼리 실행
});
};

export const useNotices = () => {
return useQuery({

Check warning on line 31 in src/queries/main.ts

View check run for this annotation

Codecov / codecov/patch

src/queries/main.ts#L30-L31

Added lines #L30 - L31 were not covered by tests
queryKey: ['notices'],
queryFn: fetchNotices,
queryFn: async () => {
const now = new Date();
const response = await axios.get('/api/notices', {

Check warning on line 35 in src/queries/main.ts

View check run for this annotation

Codecov / codecov/patch

src/queries/main.ts#L33-L35

Added lines #L33 - L35 were not covered by tests
params: {
time: now.toJSON(),
order: ['start_time', 'id'],
},
metadata: {
gaCategory: 'Notice',
gaVariable: 'GET / List',
},
});
return response.data;

Check warning on line 45 in src/queries/main.ts

View check run for this annotation

Codecov / codecov/patch

src/queries/main.ts#L45

Added line #L45 was not covered by tests
},
});
};

0 comments on commit d08a38f

Please sign in to comment.