Skip to content

Commit

Permalink
Fix: 상세페이지 notFound 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
dongree committed Dec 9, 2023
1 parent a40f8d1 commit c5989fb
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 41 deletions.
16 changes: 11 additions & 5 deletions src/app/me/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getCoverletterInfoByUserId,
getCoverletterVisibleMode,
} from '@/service/coverletter';
import { notFound } from 'next/navigation';

type Props = {
params: {
Expand All @@ -35,8 +36,12 @@ export default async function MePage({ params: { slug } }: Props) {
const cookie = `${ACCESS_TOKEN}=${accessToken}; ${REFRESH_TOKEN}=${refreshToken}`;
const userId = Number(slug);

const userInfo = await getUserInfo(userId, cookie);
if (userInfo.userId === undefined) {
notFound();
}

const memosData = getMemosByUserId(userId);
const userData = getUserInfo(userId, cookie);
const historyData = getHistory(
userId,
new Date().getFullYear(),
Expand All @@ -54,7 +59,6 @@ export default async function MePage({ params: { slug } }: Props) {

const [
memos,
userInfo,
history,
{ id, isLogin, authority },
tags,
Expand All @@ -66,7 +70,6 @@ export default async function MePage({ params: { slug } }: Props) {
coverletterIsVisible,
] = await Promise.all([
memosData,
userData,
historyData,
myData,
tagData,
Expand Down Expand Up @@ -137,9 +140,12 @@ export default async function MePage({ params: { slug } }: Props) {
);
}

export async function generateMetadata({ params }: Props) {
const userId = Number(params.slug);
export async function generateMetadata({ params: { slug } }: Props) {
const userId = Number(slug);
const { nickname } = await getUserInfo(userId);
if (nickname === undefined) {
notFound();
}

return {
title: `${nickname} - 인포럼`,
Expand Down
41 changes: 24 additions & 17 deletions src/app/memos/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ACCESS_TOKEN, REFRESH_TOKEN } from '@/utils/const';
import { checkUser, getUserInfo } from '@/service/auth';
import { getQuestionsByMemoId } from '@/service/questions';
import { getNotificationsTop30 } from '@/service/notification';
import { notFound } from 'next/navigation';

type Props = {
params: {
Expand All @@ -23,36 +24,39 @@ export default async function MemoPage({ params: { slug } }: Props) {
const cookie = `${ACCESS_TOKEN}=${accessToken}; ${REFRESH_TOKEN}=${refreshToken}`;
const memoId = Number(slug);

const memoData = getMemoById(memoId, cookie);
const {
memoTitle,
memoColor,
memoText,
authorId,
likes,
authorName,
authorProfileImagePath,
memoTags,
isMine,
createdDate,
seriesId,
seriesTitle,
authorRank,
} = await getMemoById(memoId, cookie);

if (memoTitle === undefined) {
notFound();
}

const likeData = getIsLike('memos', memoId, cookie);
const commentData = getCommentsByPostTypeAndPostId('memo', memoId, cookie);
const myData = checkUser(cookie);
const questionsData = getQuestionsByMemoId(memoId);
const recommendationsData = getMemoRecommendationsById(memoId);

const [
{
memoTitle,
memoColor,
memoText,
authorId,
likes,
authorName,
authorProfileImagePath,
memoTags,
isMine,
createdDate,
seriesId,
seriesTitle,
authorRank,
},
{ isLike },
comments,
{ id, isLogin, authority },
recommendations,
questions,
] = await Promise.all([
memoData,
likeData,
commentData,
myData,
Expand Down Expand Up @@ -121,6 +125,9 @@ export default async function MemoPage({ params: { slug } }: Props) {
export async function generateMetadata({ params: { slug } }: Props) {
const memoId = Number(slug);
const { memoTitle, memoDescription } = await getMemoById(memoId);
if (memoTitle === undefined) {
notFound();
}

return {
title: memoTitle,
Expand Down
17 changes: 14 additions & 3 deletions src/app/questions/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getNotificationsTop30 } from '@/service/notification';
import { getQuestionById } from '@/service/questions';
import { ACCESS_TOKEN, REFRESH_TOKEN } from '@/utils/const';
import { cookies } from 'next/headers';
import { notFound } from 'next/navigation';

type Props = {
params: {
Expand All @@ -22,13 +23,20 @@ export default async function QuestionPage({ params: { slug } }: Props) {
const cookie = `${ACCESS_TOKEN}=${accessToken}; ${REFRESH_TOKEN}=${refreshToken}`;
const questionId = Number(slug);

const questionData = getQuestionById(questionId, cookie);
const question = await getQuestionById(questionId, cookie);
if (question.id === undefined) {
notFound();
}

const answersData = getAnswersByQuestionId(questionId, cookie);
const myData = checkUser(cookie);
const likeData = getIsLike('questions', questionId, cookie);

const [question, answers, { id, isLogin, authority }, { isLike }] =
await Promise.all([questionData, answersData, myData, likeData]);
const [answers, { id, isLogin, authority }, { isLike }] = await Promise.all([
answersData,
myData,
likeData,
]);

const { profileImageFilePath } = isLogin
? await getUserInfo(id)
Expand Down Expand Up @@ -68,6 +76,9 @@ export default async function QuestionPage({ params: { slug } }: Props) {
export async function generateMetadata({ params: { slug } }: Props) {
const questionId = Number(slug);
const { title, description } = await getQuestionById(questionId);
if (title === undefined) {
notFound();
}

return {
title: title,
Expand Down
7 changes: 7 additions & 0 deletions src/app/series/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getQuestionsByMemoId } from '@/service/questions';
import { getSeriesById } from '@/service/series';
import { ACCESS_TOKEN, REFRESH_TOKEN } from '@/utils/const';
import { cookies } from 'next/headers';
import { notFound } from 'next/navigation';

type Props = {
params: {
Expand All @@ -24,6 +25,9 @@ export default async function SeriesDetailPage({ params: { slug } }: Props) {
const seriesId = Number(slug);

const { memoInfoList, likes, title } = await getSeriesById(seriesId);
if (title === undefined) {
notFound();
}

const memoData = getMemoById(memoInfoList[0].id, cookie);
const likeData = getIsLike('series', seriesId, cookie);
Expand Down Expand Up @@ -98,6 +102,9 @@ export default async function SeriesDetailPage({ params: { slug } }: Props) {
export async function generateMetadata({ params: { slug } }: Props) {
const seriesId = Number(slug);
const { title, description } = await getSeriesById(seriesId);
if (title === undefined) {
notFound();
}

return {
title: title,
Expand Down
28 changes: 16 additions & 12 deletions src/service/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
} from '@/types';
import { URLSearchParams } from 'next/dist/compiled/@edge-runtime/primitives/url';

export async function userSignUp(userData: UserSignUpData): Promise<SignupResponse> {
export async function userSignUp(
userData: UserSignUpData
): Promise<SignupResponse> {
return fetch(`${process.env.NEXT_PUBLIC_SERVER_IP_ADDRESS_SECURE}/users`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand All @@ -27,13 +29,18 @@ export async function userSignUp(userData: UserSignUpData): Promise<SignupRespon
.catch(console.error);
}

export async function employerSignUp(userData: EmployerSignUpData): Promise<SignupResponse> {
return fetch(`${process.env.NEXT_PUBLIC_SERVER_IP_ADDRESS_SECURE}/users/employer`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(userData),
})
export async function employerSignUp(
userData: EmployerSignUpData
): Promise<SignupResponse> {
return fetch(
`${process.env.NEXT_PUBLIC_SERVER_IP_ADDRESS_SECURE}/users/employer`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(userData),
}
)
.then((res) => {
if (!res.ok) {
throw new Error('error');
Expand Down Expand Up @@ -211,10 +218,7 @@ export async function getUserInfo(
credentials: 'include',
}
)
.then((res) => {
if (!res.ok) throw new Error('error 발생!');
return res.json();
})
.then((res) => res.json())
.catch(console.error);
}

Expand Down
5 changes: 1 addition & 4 deletions src/service/memos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ export async function getMemoById(
credentials: 'include',
}
)
.then((res) => {
if (!res.ok) throw new Error('error');
return res.json();
})
.then((res) => res.json())
.catch(console.error);
}

Expand Down

0 comments on commit c5989fb

Please sign in to comment.