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: useLectureQuery #97

Merged
merged 1 commit into from
Sep 7, 2024
Merged
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
13 changes: 6 additions & 7 deletions src/components/Lecture/IsTestInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import { User } from 'api';
import { Button, SearchTestInfoList, Spinner } from 'components';
import { fakeEvaluationList } from 'constants/placeholderData';
import useLectureQuery from 'hooks/useLectureQuery';
import { useSearchParams } from 'react-router-dom';
import { isLoginStorage } from 'utils/loginStorage';

interface IsTestInfoProps {
selectId: string;
setWritten: React.Dispatch<React.SetStateAction<boolean>>;
}

const IsTestInfo = ({ selectId, setWritten }: IsTestInfoProps) => {
const IsTestInfo = () => {
const { testInfo } = useLectureQuery();
const isLogin = isLoginStorage();
const { data, isLoading, isFetchingNextPage, ref } = testInfo(selectId, setWritten);
const { data, isLoading, isFetchingNextPage, ref } = testInfo;

const [searchParams] = useSearchParams();
const selectId = searchParams.get('id') || '';

if (!isLogin) {
return <SearchTestInfoList page={fakeEvaluationList} isLogin={false} />;
Expand Down
4 changes: 2 additions & 2 deletions src/components/Lecture/LectureDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import useLectureQuery from 'hooks/useLectureQuery';

const LectureDetail = () => {
const { detail } = useLectureQuery();
const { data, isLogin } = detail();
const { data } = detail;

return <LectureInfoBox isLogin={isLogin} current={data?.data ? data.data : fakeLectureInfo} />;
return <LectureInfoBox current={data?.data ? data.data : fakeLectureInfo} />;
};

export default LectureDetail;
6 changes: 4 additions & 2 deletions src/components/Lecture/LectureInfoBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import { useNavigate } from 'react-router-dom';
import type { SetNumber, SetTeamNumber } from 'types/common';
import type { LectureDetailItem } from 'types/lecture';
import { floatFix } from 'utils/floatFix';
import { isLoginStorage } from 'utils/loginStorage';

interface LectureInfoBoxProps {
current: LectureDetailItem;
isLogin?: boolean;
}

const LectureInfoBox = ({ current, isLogin = false }: LectureInfoBoxProps) => {
const LectureInfoBox = ({ current }: LectureInfoBoxProps) => {
const navigate = useNavigate();
const teamSet = Math.floor(current.lectureTeamAvg ?? 0) as SetTeamNumber;
const homeworkSet = Math.floor(current.lectureHomeworkAvg ?? 0) as SetNumber;
const difficultySet = Math.floor(current.lectureDifficultyAvg ?? 0) as SetNumber;

const isLogin = isLoginStorage();

return (
<Content id="top">
<div style={{ filter: current.id === -1 ? 'blur(5px)' : undefined }}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/List/LectureList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface LectureListProps {

const LectureList = ({ count, pages }: LectureListProps) => {
const { search } = useLectureQuery();
const { nextLoading, value, ref } = search();
const { nextLoading, value, ref } = search;

return count ? (
<>
Expand Down
2 changes: 1 addition & 1 deletion src/components/List/MainList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const MainList = () => {

return (
<LectureContainer
data={getMainLecture !== undefined ? getMainLecture?.data : fakeLectureList}
data={getMainLecture !== undefined ? getMainLecture?.data?.data : fakeLectureList}
/>
);
};
Expand Down
12 changes: 4 additions & 8 deletions src/components/List/SearchEvaluationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Fragment, useState } from 'react';
import StarRatings from 'react-star-ratings';
import type { Review } from 'types/evaluate';
import { floatFix } from 'utils/floatFix';
import { isLoginStorage } from 'utils/loginStorage';

export const FakeList = () => {
return (
Expand All @@ -20,15 +21,10 @@ export const FakeList = () => {
);
};

interface SearchEvaluationListProps {
selectId: string;
setWritten: React.Dispatch<React.SetStateAction<boolean>>;
isLogin: boolean;
}

const SearchEvaluationList = ({ selectId, setWritten, isLogin }: SearchEvaluationListProps) => {
const SearchEvaluationList = () => {
const { evaluation } = useLectureQuery();
const { data, isLoading, isFetchingNextPage, ref } = evaluation(selectId, setWritten);
const { data, isLoading, isFetchingNextPage, ref } = evaluation;
const isLogin = isLoginStorage();

if (isLoading) return <Spinner id="nextPage" />;

Expand Down
231 changes: 107 additions & 124 deletions src/hooks/useLectureQuery.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Lecture } from 'api';
import { lectureState } from 'app/recoilStore';
import { CACHE_TIME } from 'constants/cacheTime';
import type { Category } from 'pages/LectureInfo';
import { useEffect } from 'react';
import { useInView } from 'react-intersection-observer';
import { useInfiniteQuery, useQuery } from 'react-query';
import { useSearchParams } from 'react-router-dom';
import { useSetRecoilState } from 'recoil';
import { isLoginStorage } from 'utils/loginStorage';

// 애러가 날 수 있는 hook
const useLectureQuery = () => {
const [searchParams] = useSearchParams();
const setLectureInfo = useSetRecoilState(lectureState);
Expand All @@ -19,136 +19,119 @@ const useLectureQuery = () => {
const majorType = searchParams.get('majorType') || '전체';
const major = majorType === '전체' ? '' : majorType;
const value = searchValue === 'all' ? '' : searchValue;

const selectCategory = (searchParams.get('category') as Category) || '강의평가';
const isLogin = isLoginStorage();

// 메인 쿼리(key: 정렬,전공)
const { data: getMainLecture } = useQuery(
['main', option, major],
() => lecture.main(option, 1, major),
{ keepPreviousData: true, suspense: true },
const getMainLecture = useQuery(['main', option, major], () => lecture.main(option, 1, major), {
keepPreviousData: true,
suspense: true,
});

const { ref: searchRef, inView: searchInView } = useInView();
const search = useInfiniteQuery(
['search', value, option, major],
({ pageParam = 1 }) => lecture.search(value, pageParam, option, major),
{
getNextPageParam: (lastPage) =>
lastPage && !lastPage.isLast ? lastPage.nextPage : undefined,
keepPreviousData: true,
},
);

// 검색 쿼리(key: 검색어,정렬,전공)
const search = () => {
const { ref, inView } = useInView();
const {
data,
isLoading: searchLoading,
fetchNextPage: getNextSearch,
isFetchingNextPage: nextLoading,
} = useInfiniteQuery(
['search', value, option, major],
({ pageParam = 1 }) => lecture.search(value, pageParam, option, major),
{
getNextPageParam: (lastPage) => {
if (lastPage && !lastPage.isLast) return lastPage.nextPage;

return undefined;
},
keepPreviousData: true,
},
);
useEffect(() => {
if (inView) {
getNextSearch();
}
}, [inView, getNextSearch]);

return { data, searchLoading, nextLoading, value, ref };
};

// 강의 상세 쿼리(key: 강의id)
const detail = () => {
const { data, isLoading } = useQuery(
['lecture', 'detail', selectId],
() => lecture.detail(selectId),
{
cacheTime: CACHE_TIME.MINUTE_0,
staleTime: CACHE_TIME.MINUTE_0,
enabled: isLogin,
onSuccess: (lecture) => {
setLectureInfo({
id: Number(selectId),
lectureName: lecture!.data.lectureName,
professor: lecture!.data.professor,
semesterList: lecture!.data.semesterList,
selectedSemester: '선택',
satisfaction: 0.5,
honey: 0.5,
learning: 0.5,
team: 0,
homework: 0,
difficulty: 0,
examInfo: '',
examType: '선택',
examDifficulty: '',
content: '',
majorType: '',
totalAvg: 0,
});
},
},
);

return { data, isLoading, isLogin: isLogin };
};

// 강의평가 쿼리(key: 강의id)
const evaluation = (id: string, setWritten: React.Dispatch<React.SetStateAction<boolean>>) => {
const { ref, inView } = useInView();
const { data, isFetchingNextPage, isLoading, fetchNextPage } = useInfiniteQuery(
['lecture', 'evaluationList', id],
({ pageParam = 1 }) => lecture.evaluation(id, pageParam),
{
getNextPageParam: (lastPage) => {
if (lastPage && !lastPage.isLast) return lastPage.nextPage;

return undefined;
},
onSuccess: (data) => setWritten(!!data.pages[0]?.written),
cacheTime: CACHE_TIME.MINUTE_0,
staleTime: CACHE_TIME.MINUTE_0,
enabled: isLogin,
},
);
useEffect(() => {
if (inView && isLogin) {
fetchNextPage();
}
}, [inView, fetchNextPage]);

return { data, isFetchingNextPage, isLoading, ref };
};

// 시험정보 쿼리(key: 강의id)
const testInfo = (id: string, setWritten: React.Dispatch<React.SetStateAction<boolean>>) => {
const { ref, inView } = useInView();
const { data, isFetchingNextPage, isLoading, fetchNextPage } = useInfiniteQuery(
['lecture', 'examList', id],
({ pageParam = 1 }) => lecture.examInfo(id, pageParam),
{
getNextPageParam: (lastPage) => {
if (lastPage && !lastPage.isLast) return lastPage.nextPage;
const detail = useQuery(['lecture', 'detail', selectId], () => lecture.detail(selectId), {
cacheTime: CACHE_TIME.MINUTE_0,
staleTime: CACHE_TIME.MINUTE_0,
enabled: isLogin && selectId !== '',
onSuccess: (lecture) => {
setLectureInfo({
id: Number(selectId),
lectureName: lecture!.data.lectureName,
professor: lecture!.data.professor,
semesterList: lecture!.data.semesterList,
selectedSemester: '선택',
satisfaction: 0.5,
honey: 0.5,
learning: 0.5,
team: 0,
homework: 0,
difficulty: 0,
examInfo: '',
examType: '선택',
examDifficulty: '',
content: '',
majorType: '',
totalAvg: 0,
});
},
});

const { ref: evaluationRef, inView: evaluationInView } = useInView();
const evaluation = useInfiniteQuery(
['lecture', 'evaluationList', selectId],
({ pageParam = 1 }) => lecture.evaluation(selectId, pageParam),
{
getNextPageParam: (lastPage) =>
lastPage && !lastPage.isLast ? lastPage.nextPage : undefined,
cacheTime: CACHE_TIME.MINUTE_0,
staleTime: CACHE_TIME.MINUTE_0,
enabled: isLogin && selectId !== '' && selectCategory === '강의평가',
},
);

return undefined;
},
onSuccess: (data) => setWritten(!!data.pages[0]?.data.written),
cacheTime: CACHE_TIME.MINUTE_0,
staleTime: CACHE_TIME.MINUTE_0,
enabled: isLogin,
},
);
useEffect(() => {
if (inView && isLogin) {
fetchNextPage();
}
}, [inView, fetchNextPage]);
const { ref: testInfoRef, inView: testInfoInView } = useInView();
const testInfo = useInfiniteQuery(
['lecture', 'examList', selectId],
({ pageParam = 1 }) => lecture.examInfo(selectId, pageParam),
{
getNextPageParam: (lastPage) =>
lastPage && !lastPage.isLast ? lastPage.nextPage : undefined,
cacheTime: CACHE_TIME.MINUTE_0,
staleTime: CACHE_TIME.MINUTE_0,
enabled: isLogin && selectId !== '' && selectCategory === '시험정보',
},
);

return { data, isFetchingNextPage, isLoading, ref };
useEffect(() => {
if (searchInView) {
search.fetchNextPage();
}
}, [searchInView, search.fetchNextPage, search]);

useEffect(() => {
if (evaluationInView && isLogin) {
evaluation.fetchNextPage();
}
}, [evaluationInView, evaluation.fetchNextPage, isLogin, evaluation]);

useEffect(() => {
if (testInfoInView && isLogin) {
testInfo.fetchNextPage();
}
}, [testInfoInView, testInfo.fetchNextPage, isLogin, testInfo]);

return {
getMainLecture,
search: {
data: search.data,
searchLoading: search.isLoading,
nextLoading: search.isFetchingNextPage,
value,
ref: searchRef,
},
detail,
evaluation: {
data: evaluation.data,
isFetchingNextPage: evaluation.isFetchingNextPage,
isLoading: evaluation.isLoading,
ref: evaluationRef,
},
testInfo: {
data: testInfo.data,
isFetchingNextPage: testInfo.isFetchingNextPage,
isLoading: testInfo.isLoading,
ref: testInfoRef,
},
};

return { getMainLecture, search, detail, evaluation, testInfo };
};

export default useLectureQuery;
Loading
Loading