Skip to content

Commit

Permalink
Merge pull request #297 from TripInfoWeb/dev_gathering
Browse files Browse the repository at this point in the history
Dev gathering
  • Loading branch information
ssssksss authored Sep 13, 2024
2 parents 3968da6 + da804d6 commit c656ebb
Show file tree
Hide file tree
Showing 5 changed files with 492 additions and 529 deletions.
189 changes: 93 additions & 96 deletions src/app/gathering/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import Breadcrumbs from "@/components/common/Breadcrumb";
import GatheringRecommendationList from "@/components/gathering/read/GatheringRecommendationList";
import GatheringViewerContainer from "@/containers/gathering/read/detail/GatheringViewerContainer";
import { GatheringDetailResponseDto } from "@/types/GatheringDto";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { NextResponse } from "next/server";

interface PageProps {
params: { id: string };
}
Expand All @@ -16,85 +10,87 @@ const categories = [
];


async function getNewAccessToken(refreshToken: string): Promise<string | null> {
try {
const response = await fetch(
`${process.env.BACKEND_URL}/api/auth/oauth2/token/refresh`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Cookie: `refresh_token=${refreshToken}`,
},
},
);

if (!response.ok) {
throw new Error("리프레시 토큰을 사용한 액세스 토큰 갱신 실패");
}
const accessToken = response.headers.get("set-cookie");
return accessToken;
} catch (error) {
console.error("액세스 토큰 갱신 중 오류 발생:", error);
return null;
}
}

async function getGathering(id: number): Promise<GatheringDetailResponseDto> {
let accessToken = cookies().get("access_token")?.value;
const refreshToken = cookies().get("refresh_token")?.value;

if (!accessToken && !refreshToken) {
redirect("/auth/signin");
}


try {
let response = await fetch(
`${process.env.BACKEND_URL}/api/gatherings/${id}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Cookie: `access_token=${accessToken}`,
},
cache: "no-store",
},
);

// 액세스 토큰이 만료된 경우
if (response.status === 401 && refreshToken) {
const newAccessToken = await getNewAccessToken(refreshToken);

if (newAccessToken) {
// 새로 발급받은 액세스 토큰으로 다시 요청
response = await fetch(
`${process.env.BACKEND_URL}/api/gatherings/${id}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Cookie: `access_token=${newAccessToken}`,
},
cache: "no-store",
},
);

} else {
throw new Error("새로운 액세스 토큰 발급 실패");
}
}

if (!response.ok) {
throw new Error("네트워크 응답이 좋지 않습니다.");
}

return await response.json();
} catch (error) {
console.error("데이터를 가져오는 중 오류 발생:", error);
throw error;
}
}
// async function getNewAccessToken(refreshToken: string): Promise<string | null> {
// try {
// const response = await fetch(
// `${process.env.BACKEND_URL}/api/auth/oauth2/token/refresh`,
// {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// Cookie: `refresh_token=${refreshToken}`,
// },
// },
// );

// if (!response.ok) {
// throw new Error("리프레시 토큰을 사용한 액세스 토큰 갱신 실패");
// }
// const accessToken = response.headers.get("set-cookie");
// return accessToken;
// } catch (error) {
// console.error("액세스 토큰 갱신 중 오류 발생:", error);
// return null;
// }
// }

// async function getGathering(id: number) {
// let accessToken = cookies().get("access_token")?.value;
// const refreshToken = cookies().get("refresh_token")?.value;

// if (!accessToken && !refreshToken) {
// redirect("/auth/signin");
// }

// if (!accessToken) {
// const newAccessToken= await getNewAccessToken(refreshToken as string);
// accessToken = newAccessToken as string;
// }

// let response = await fetch(
// `${process.env.BACKEND_URL}/api/gatherings/${id}`,
// {
// method: "GET",
// headers: {
// "Content-Type": "application/json",
// Cookie: `access_token=${accessToken}`,
// },
// cache: "no-store",
// },
// );

// if (response.status != 401 && !response.ok) {
// throw new Error("서버 에러");
// }

// // 액세스 토큰이 만료된 경우
// if (response.status === 401 && refreshToken) {
// const newAccessToken = await getNewAccessToken(refreshToken);

// if (newAccessToken) {
// // 새로 발급받은 액세스 토큰으로 다시 요청
// response = await fetch(
// `${process.env.BACKEND_URL}/api/gatherings/${id}`,
// {
// method: "GET",
// headers: {
// "Content-Type": "application/json",
// Cookie: `access_token=${newAccessToken}`,
// },
// cache: "no-store",
// },
// );

// } else {
// throw new Error("새로운 액세스 토큰 발급 실패");
// }

// if (!response.ok) {
// throw new Error("네트워크 응답이 좋지 않습니다.");
// }
// }
// return await response.json();
// }

export async function generateMetadata({ params: { id } }: PageProps) {
const postId = Number(id);
Expand All @@ -108,26 +104,27 @@ export async function generateMetadata({ params: { id } }: PageProps) {
};
}


export default async function Page({ params: { id } }: PageProps) {
const postId = Number(id);

if (postId <= 0 || !Number.isSafeInteger(postId)) {
return NextResponse.json(
{ message: "페이지를 찾을 수 없습니다." },
{ status: 404 },
);
}
const gatheringData = await getGathering(postId);
return (
// if (postId <= 0 || !Number.isSafeInteger(postId)) {
// return NextResponse.json(
// { message: "페이지를 찾을 수 없습니다." },
// { status: 404 },
// );
// }
// const data = await getGathering(postId);

return (
<div
className={
"m-auto flex min-h-[calc(100vh-25rem)] w-full max-w-[60rem] flex-col pb-[2.5rem]"
}
>
{/* 제일 상단 */}
<Breadcrumbs categories={categories} />
<GatheringViewerContainer data={gatheringData} postId={postId} />
<GatheringRecommendationList data={gatheringData.gatheringRecommend} />
<GatheringViewerContainer postId={postId} />
{/* <GatheringRecommendationList data={data.gatheringRecommend} /> */}
</div>
);
}
8 changes: 4 additions & 4 deletions src/components/common/modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface ModalProps extends React.PropsWithChildren {

export const Modal = ({ isOpen, children, onClose, isHeaderBar, headerBarStyle = "bg-white" }: ModalProps) => {
const [documentBody, setDocumentBody] = useState<HTMLElement | null>(null);
const ref = useRef<HTMLButtonElement>(null);
const ref = useRef<HTMLDivElement>(null);
let flag = isOpen;
usePreventBodyScroll(isOpen);

Expand Down Expand Up @@ -63,9 +63,9 @@ export const Modal = ({ isOpen, children, onClose, isHeaderBar, headerBarStyle =
style={{ zIndex: "100" }}
>
<div className="absolute h-full w-full cursor-pointer bg-black/30"></div>
<button
<div
ref={ref}
className="-z-1 relative flex h-[calc(100vh-1rem)] flex-col items-center justify-center "
className="-z-1 relative flex h-[calc(100vh-1rem)] items-center justify-start"
onClick={(e) => {
if (e.target == ref.current) {
onClose();
Expand All @@ -88,7 +88,7 @@ export const Modal = ({ isOpen, children, onClose, isHeaderBar, headerBarStyle =
</div>
)}
{children}
</button>
</div>
</div>,
document.getElementById("modal-root") as HTMLElement,
);
Expand Down
Loading

0 comments on commit c656ebb

Please sign in to comment.