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

[#103] Feat: 업로드한 짤 페이지에서 짤 get api 구현 및 적용 #104

Merged
merged 5 commits into from
Mar 13, 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
10 changes: 8 additions & 2 deletions src/apis/zzal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
PostUploadZzalRequest,
GetMyLikedZzalsResponse,
GetMyUploadedZzalsResponse,
GetZzalDetailsResponse,
GetZzalResponse,
} from "@/types/zzal.dto";
Expand All @@ -26,9 +27,14 @@ export const deleteMyZzal = (imageId: number) => {
return http.delete<number>({ url: `/v1/image/${imageId}` });
};

export const getMyLikedZzals = (offset: number) =>
export const getMyLikedZzals = (page: number) =>
http.get<GetMyLikedZzalsResponse>({
url: `/v1/image/like?page=${offset}&size=${PAGINATION_LIMIT}`,
url: `/v1/image/like?page=${page}&size=${PAGINATION_LIMIT}`,
});

export const getMyUploadedZzals = (page: number) =>
http.get<GetMyUploadedZzalsResponse>({
url: `/v1/image/upload?page=${page}&size=${PAGINATION_LIMIT}`,
});

export const getZzalDetails = (imageId: number) =>
Expand Down
33 changes: 33 additions & 0 deletions src/hooks/api/zzal/useGetMyUploadedZzals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useSuspenseInfiniteQuery } from "@tanstack/react-query";
import { getMyUploadedZzals } from "@/apis/zzal";

const useGetMyUploadedZzals = () => {
const { data, hasNextPage, isFetchingNextPage, fetchNextPage, ...rest } =
useSuspenseInfiniteQuery({
queryKey: ["uploadedZzals"],
queryFn: ({ pageParam = 0 }) => getMyUploadedZzals(pageParam),
getNextPageParam: (lastPage, _allPages, lastPageParam) => {
if (!lastPage) return;

return lastPageParam + 1;
},
initialPageParam: 0,
});

const handleFetchNextPage = () => {
if (hasNextPage && !isFetchingNextPage) {
fetchNextPage();
}
};

return {
zzals: data?.pages.flatMap((page) => page),
handleFetchNextPage,
hasNextPage,
isFetchingNextPage,
fetchNextPage,
...rest,
};
};

export default useGetMyUploadedZzals;
22 changes: 21 additions & 1 deletion src/routes/_layout-with-chat/my-uploaded-zzals/route.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { useRef } from "react";
import { createLazyFileRoute } from "@tanstack/react-router";
import TagBadge from "@/components/common/TagBadge";
import useGetTopTagsFromUploaded from "@/hooks/api/tag/useGetTopTagsFromUploaded";
import useGetMyUploadedZzals from "@/hooks/api/zzal/useGetMyUploadedZzals";
import useIntersectionObserver from "@/hooks/common/useIntersectionObserver";
import TagSearchForm from "@/components/common/SearchTag/TagSearchForm";
import TagBadge from "@/components/common/TagBadge";
import MasonryLayout from "@/components/common/MasonryLayout";
import ZzalCard from "@/components/common/ZzalCard";

const MyUploadedZzals = () => {
const { topTags } = useGetTopTagsFromUploaded();
const { zzals, handleFetchNextPage } = useGetMyUploadedZzals();
const fetchMoreRef = useRef(null);

useIntersectionObserver({
target: fetchMoreRef,
handleIntersect: handleFetchNextPage,
});

return (
<div className="flex w-full flex-col items-center">
Expand All @@ -13,6 +26,13 @@ const MyUploadedZzals = () => {
<TagBadge key={tagId} content={tagName} isClickable className="mr-5pxr" />
))}
</div>
<TagSearchForm />
<MasonryLayout className="mt-15pxr">
{zzals.map(({ imageId, path, title }) => (
<ZzalCard className="mb-10pxr" key={imageId} src={path} alt={title} />
))}
</MasonryLayout>
<div ref={fetchMoreRef} />
</div>
);
};
Expand Down
7 changes: 7 additions & 0 deletions src/types/zzal.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export interface GetMyLikedZzalsResponse {
title: string;
}

export interface GetMyUploadedZzalsResponse {
imageId: number;
title: string;
path: string;
}

export interface GetZzalResponse {
pageParams: number[];
pages: GetZzalPagesResponse[][];
Expand All @@ -23,6 +29,7 @@ export interface GetZzalPagesResponse {
path: string;
imageLikeYn: boolean;
}

export interface GetZzalDetailsResponse {
imageId: number;
imageTitle: string;
Expand Down
Loading