Skip to content

Commit

Permalink
Merge pull request #123 from hufs-sports-live/fix/remove-match-list-i…
Browse files Browse the repository at this point in the history
…nfinite-scroll

[FIX] 무한 스크롤 제거
  • Loading branch information
seongminn authored Dec 1, 2023
2 parents 217f7f5 + 31dea51 commit 838f0ab
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 64 deletions.
8 changes: 3 additions & 5 deletions src/api/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ export type MatchListParams = {
sport_id?: string[];
status: MatchStatus;
league_id?: string;
cursor?: number;
// cursor?: number;
};

export const getMatchList = async ({ cursor, ...params }: MatchListParams) => {
export const getMatchList = async ({ ...params }: MatchListParams) => {
const queryString = convertObjectToQueryString(params);

const { data } = await instance.get<MatchListType[]>(
`games?${queryString}&cursor=${cursor || ''}`,
);
const { data } = await instance.get<MatchListType[]>(`games?${queryString}`);

return data;
};
Expand Down
4 changes: 2 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ export default function Home() {
loadingFallback={<MatchList.Skeleton />}
>
<MatchListFetcher {...paramsObj}>
{({ matchList, ...props }) => (
{({ matchList }) => (
<div className="flex w-full flex-col gap-8">
<MatchList matchList={matchList.pages.flat()} {...props} />
<MatchList matchList={matchList} />
</div>
)}
</MatchListFetcher>
Expand Down
4 changes: 2 additions & 2 deletions src/components/match/CommentForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UseMutateFunction } from '@tanstack/react-query';
import { ChangeEvent, FormEvent, Fragment, useState } from 'react';
import { ChangeEvent, FormEvent, useState } from 'react';

import { MatchCommentPayload, MatchTeamType } from '@/types/match';
import { $ } from '@/utils/core';
Expand Down Expand Up @@ -35,7 +35,7 @@ export default function CommentForm({
) => {
e.preventDefault();

if (inputValue.trim() === '') return;
if (!payload.content.trim()) return;

mutate({ ...payload, gameTeamId: selectedTeamId });
setInputValue('');
Expand Down
26 changes: 3 additions & 23 deletions src/components/match/MatchList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,26 @@ import {
COMMON_ERROR_MESSAGE,
MATCH_LIST_API_ERROR_MESSAGE,
} from '@/constants/error';
import useIntersect from '@/hooks/useInfiniteObserver';
import { MatchListType } from '@/types/match';

type MatchListProps = {
matchList: MatchListType[];
hasNextPage: boolean;
fetchNextPage: () => void;
isFetching: boolean;
};

export default function MatchList({
matchList,
fetchNextPage,
hasNextPage,
isFetching,
}: MatchListProps) {
const { ref } = useIntersect<HTMLDivElement>(async (entry, observer) => {
if (entry.isIntersecting) {
observer.unobserve(entry.target);

if (hasNextPage && !isFetching) {
fetchNextPage();
}
}
});

export default function MatchList({ matchList }: MatchListProps) {
return (
<>
<ul>
{matchList.map(({ id, sportsName, ...match }) => (
<>
{sportsName === '루미큐브' ? (
<li key={id} className="mb-14">
<li key={match.startTime + id} className="mb-14">
<Link href={`rummikube/${id}`}>
<RummiKubMatchItem {...match} sportsName={sportsName} />
</Link>
</li>
) : (
<li key={id} className="mb-14">
<li key={match.startTime + id} className="mb-14">
<Link href={`match/${id}`}>
<MatchCard
{...match}
Expand Down Expand Up @@ -82,7 +63,6 @@ export default function MatchList({
</>
))}
</ul>
<div ref={ref}></div>
</>
);
}
Expand Down
18 changes: 3 additions & 15 deletions src/queries/useMatchList/Fetcher.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { InfiniteData } from '@tanstack/react-query';
import { ReactNode } from 'react';

import { MatchListParams } from '@/api/match';
Expand All @@ -8,27 +7,16 @@ import { useMatchList } from './query';

interface MatchListFetcherProps
extends Omit<MatchListParams, 'cursor' | 'size'> {
children: ({
matchList,
hasNextPage,
fetchNextPage,
isFetching,
}: {
matchList: InfiniteData<MatchListType[]>;
hasNextPage: boolean;
fetchNextPage: () => void;
isFetching: boolean;
}) => ReactNode;
children: ({ matchList }: { matchList: MatchListType[] }) => ReactNode;
}

export default function MatchListFetcher({
children,
...props
}: MatchListFetcherProps) {
const { matchList, error, hasNextPage, fetchNextPage, isFetching } =
useMatchList(props);
const { matchList, error } = useMatchList(props);

if (error) throw error;

return children({ matchList, hasNextPage, fetchNextPage, isFetching });
return children({ matchList });
}
27 changes: 10 additions & 17 deletions src/queries/useMatchList/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useSuspenseInfiniteQuery } from '@tanstack/react-query';
import { useSuspenseQuery } from '@tanstack/react-query';

import { getMatchList, MatchListParams } from '@/api/match';

Expand All @@ -7,25 +7,18 @@ export const useMatchList = ({
status = 'playing',
league_id,
}: Omit<MatchListParams, 'cursor' | 'size'>) => {
const { data, error, isFetching, hasNextPage, fetchNextPage } =
useSuspenseInfiniteQuery({
queryKey: ['match-list', sport_id, status, league_id],
queryFn: ({ pageParam }) =>
getMatchList({
sport_id,
status,
league_id,
cursor: pageParam,
}),
initialPageParam: 0,
getNextPageParam: lastPage => lastPage.at(-1)?.id || null,
});
const { data, error } = useSuspenseQuery({
queryKey: ['match-list', sport_id, status, league_id],
queryFn: () =>
getMatchList({
sport_id,
status,
league_id,
}),
});

return {
matchList: data,
error,
isFetching,
hasNextPage,
fetchNextPage,
};
};

0 comments on commit 838f0ab

Please sign in to comment.