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

[Dev] lecture 관련 API 작업 #27

Open
wants to merge 4 commits into
base: feature/api-integration-20
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
55 changes: 41 additions & 14 deletions src/entities/lecture/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
import { Lecture } from "@/entities/lecture/model/lecture";
import {
GetHomeLectureList,
GetLecture,
GetLectureList,
Lecture,
} from "@/entities/lecture/model/lecture";

import apiRequest from "@/shared/api";

// TODO: BASE_PATH "/lectures"로 변경 필요
const BASE_PATH = "/class";
const API_BASE_PATH = "/lectures";
const BASE_PATH = "/api/lectures";
const HOME_BASE_PATH = "/api/home";

export const getLectureList = () =>
apiRequest.get<Lecture[]>(`${BASE_PATH}`, {});
export const getLectureList = ({
params,
payload,
}: {
params: GetLectureList["Request"]["body"]["params"];
payload: GetLectureList["Request"]["body"]["payload"];
}) =>
apiRequest.post<GetLectureList["Response"]>(`${BASE_PATH}`, payload, {
params,
});

export const getLectureInfo = (id: number) =>
apiRequest.get<Lecture>(`${BASE_PATH}/${id}`, {});
export const getHomeLectureList = ({
params,
payload,
}: {
params: GetHomeLectureList["Request"]["body"]["params"];
payload: GetHomeLectureList["Request"]["body"]["payload"];
}) =>
apiRequest.post<GetHomeLectureList["Response"]>(
`${HOME_BASE_PATH}`,
payload,
{
params,
},
);

export const getEntireLecture = () =>
apiRequest.get<{
status: string;
message: string;
data: { data: Lecture[]; hasNext: boolean };
}>(`${API_BASE_PATH}`, {});
export const getLectureInfo = ({
lectureId,
payload,
}: {
lectureId: number;
payload: GetLecture["Request"]["body"];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다 이와같이 타입을 정의하셨는데 GetLecturePayload와 같이 타입을 정의하시는게 나을 것 같아요

}) =>
apiRequest.post<GetLecture["Response"]>(`${BASE_PATH}/${lectureId}`, payload);
16 changes: 0 additions & 16 deletions src/entities/lecture/api/useEntireLecture.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/entities/lecture/api/useHomeLectureList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";

import { GetLectureListDto } from "../model/lecture";
import { HOME_LECTURE_KEYS } from "@/shared/api/keyFactory";
import { getHomeLectureList } from ".";

const useHomeLectureList = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (lectureListReq: GetLectureListDto) =>
getHomeLectureList({
payload: lectureListReq.payload,
params: lectureListReq.params,
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: HOME_LECTURE_KEYS.lists() });
queryClient.invalidateQueries({
queryKey: HOME_LECTURE_KEYS.list({ page: 1, size: 10 }),
});
},
});
};

export default useHomeLectureList;
28 changes: 21 additions & 7 deletions src/entities/lecture/api/useLectureInfo.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { GetLecture, GetLectureDto } from "../model/lecture";
import { useMutation, useQueryClient } from "@tanstack/react-query";

import { LECTURE_KEYS } from "@/shared/api/keyFactory";
import { getLectureInfo } from ".";
import { useQuery } from "@tanstack/react-query";

const useLectureInfo = (lectureId: number) => {
return useQuery({
queryKey: LECTURE_KEYS.detail({ lectureId }),
queryFn: () => getLectureInfo(lectureId),
select: (response) => response.data,
meta: {
errorMessage: "Failed to fetch Entire Class",
const queryClient = useQueryClient();

return useMutation({
mutationFn: ({
lectureId,
payload,
}: {
lectureId: number;
payload: GetLecture["Request"]["body"];
}) =>
getLectureInfo({
lectureId: lectureId,
payload: payload,
}),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: LECTURE_KEYS.detail({ lectureId }),
});
},
});
};
Expand Down
24 changes: 17 additions & 7 deletions src/entities/lecture/api/useLectureList.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";

import { GetLectureList } from "../model/lecture";
import { LECTURE_KEYS } from "@/shared/api/keyFactory";
import { getLectureList } from ".";
import { useQuery } from "@tanstack/react-query";

const useLectureList = () => {
return useQuery({
queryKey: LECTURE_KEYS.list(),
queryFn: () => getLectureList(),
select: (response) => response.data,
meta: {
errorMessage: "Failed to fetch Lecture List",
const queryClient = useQueryClient();

return useMutation({
mutationFn: (lectureListReq: GetLectureList["Request"]["body"]) =>
getLectureList({
payload: lectureListReq.payload,
params: lectureListReq.params,
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: LECTURE_KEYS.lists() });
// TODO: 무한 스크롤 처리하면서 queryKey 바꿔줘야됨
queryClient.invalidateQueries({
queryKey: LECTURE_KEYS.list({ page: 1, size: 10 }),
});
},
});
};
Expand Down
Loading