Skip to content

Commit

Permalink
refactor: AdjPostNav 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
yeolyi committed Mar 9, 2024
1 parent e2d6c1f commit 3132330
Show file tree
Hide file tree
Showing 32 changed files with 310 additions and 367 deletions.
16 changes: 8 additions & 8 deletions actions/noticeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@
import { revalidateTag } from 'next/cache';
import { redirect } from 'next/navigation';

import { batchDeleteNotice, batchUnpinNotice, deleteNotice } from '@/actions/noticeServer';
import { batchDeleteNotice, batchUnpinNotice, deleteNotice } from '@/apis/notice';

import { errorToStr } from '@/utils/error';
import { getPath } from '@/utils/page';
import { notice } from '@/utils/segmentNode';

const noticePath = getPath(notice);

export const batchDeleteAction = async (ids: Set<number>) => {
export const batchDeleteNoticeAction = async (ids: Set<number>) => {
try {
await batchDeleteNotice(ids);
revalidateNoticeTag();
} catch (error) {
return { message: error instanceof Error ? error.message : '알 수 없는 에러: ' + error };
return { message: errorToStr(error) };
}
};

export const batchUnpinAction = async (ids: Set<number>) => {
export const unpinNoticeAction = async (ids: Set<number>) => {
try {
await batchUnpinNotice(ids);
revalidateNoticeTag();
} catch (error) {
return { message: error instanceof Error ? error.message : '알 수 없는 에러: ' + error };
return { message: errorToStr(error) };
}
};

/** 성공시에 리턴값이 never(호출한 함수로 되돌아가지 않음)이어야하는데 이상하게 성공시에 undefined를 반환한다. */
export const noticeDeleteAction = async (id: number) => {
export const deleteNoticeAction = async (id: number) => {
try {
await deleteNotice(id);
revalidateNoticeTag();
} catch (error) {
return { message: error instanceof Error ? error.message : '알 수 없는 에러: ' + error };
return { message: errorToStr(error) };
}
redirect(noticePath);
};
Expand Down
32 changes: 0 additions & 32 deletions actions/noticeServer.ts

This file was deleted.

49 changes: 39 additions & 10 deletions apis/notice.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
import { POSTNoticeBody, PatchNoticeBody } from '@/types/notice';
'use server';

import { patchRequest, postRequest } from './network/client';
import { NoticePreviewList, Notice, POSTNoticeBody, PatchNoticeBody } from '@/types/notice';
import { PostSearchQueryParams } from '@/types/post';

import { deleteRequest, getRequest, patchRequest, postRequest } from './network/server';

const noticePath = '/notice';

// GET

export const getNoticePosts = (params: PostSearchQueryParams) =>
getRequest(noticePath, params, { next: { tags: ['notice'] } }) as Promise<NoticePreviewList>;

export const getNoticePostDetail = (id: number, params: PostSearchQueryParams) =>
getRequest(`${noticePath}/${id}`, params, { next: { tags: ['notice'] } }) as Promise<Notice>;

// POST

export const postNotice = async (body: POSTNoticeBody) => {
const formData = new FormData();

formData.append(
'request',
new Blob([JSON.stringify(body.request)], {
type: 'application/json',
}),
);
for (const attachment of body.attachments) {
formData.append('attachments', attachment);
}

await postRequest(noticePath, {
body: formData,
});
body.attachments.forEach((attachment) => formData.append('attachments', attachment));

await postRequest('/notice', { body: formData });
};

// PATCH

export const patchNotice = async (id: number, body: PatchNoticeBody) => {
const formData = new FormData();

Expand All @@ -35,7 +48,23 @@ export const patchNotice = async (id: number, body: PatchNoticeBody) => {
formData.append('newAttachments', attachment);
}

await patchRequest(`${noticePath}/${id}`, {
body: formData,
await patchRequest(`/notice/${id}`, { body: formData });
};

export const batchUnpinNotice = async (ids: Set<number>) => {
await patchRequest(noticePath, {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ idList: Array.from(ids) }),
});
};

// DELETE

export const deleteNotice = (id: number) => deleteRequest(`${noticePath}/${id}`);

export const batchDeleteNotice = async (ids: Set<number>) => {
await deleteRequest(noticePath, {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ idList: Array.from(ids) }),
});
};
2 changes: 1 addition & 1 deletion app/[locale]/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import PageLayout from '@/components/layout/pageLayout/PageLayout';
import { ADMIN_MENU, ImportantPreview, SlidePreview } from '@/types/admin';

import { getPath } from '@/utils/page';
import { replaceDashWithSpace } from '@/utils/replaceCharacter';
import { admin } from '@/utils/segmentNode';
import { replaceDashWithSpace } from '@/utils/string';

interface AdminPageProps {
searchParams: { selected?: string; page?: string };
Expand Down
27 changes: 9 additions & 18 deletions app/[locale]/community/news/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import PageLayout from '@/components/layout/pageLayout/PageLayout';

import { PostSearchQueryParams } from '@/types/post';

import { getAdjPostsInfo } from '@/utils/getAdjPostInfo';
import { getPath } from '@/utils/page';
import { news } from '@/utils/segmentNode';

Expand All @@ -21,35 +20,27 @@ interface NewsPostPageProps {
const newsPath = getPath(news);

export default async function NewsPostPage({ params, searchParams }: NewsPostPageProps) {
const currPost = await getNewsPostDetail(parseInt(params.id), searchParams);
const { prevPostPreview, nextPostPreview } = getAdjPostsInfo(currPost, searchParams, newsPath);
const date = new Date(currPost.date);
const news = await getNewsPostDetail(parseInt(params.id), searchParams);

const date = new Date(news.date);
const dateStr = `${date.getFullYear()}${
date.getMonth() + 1
}${date.getDate()}${date.toLocaleString('ko-KR', { weekday: 'long' })}`;

return (
<PageLayout title={currPost.title} titleType="small" titleMargin="mb-5">
{currPost.attachments.length !== 0 && <Attachments files={currPost.attachments} />}
<PageLayout title={news.title} titleType="small" titleMargin="mb-5">
{news.attachments.length !== 0 && <Attachments files={news.attachments} />}
<HTMLViewer
htmlContent={currPost.description}
htmlContent={news.description}
className="mt-4"
topRightContent={
currPost.imageURL
? { type: 'imageUnoptimized', url: currPost.imageURL, widthPX: 320 }
: undefined
news.imageURL ? { type: 'imageUnoptimized', url: news.imageURL, widthPX: 320 } : undefined
}
/>
<time className=" mt-12 block text-end text-sm font-bold">{dateStr}</time>
<StraightNode margin="mt-[2.4375rem]" />
<Tags tags={currPost.tags} margin="mt-3 ml-6" searchPath={newsPath} />
<AdjPostNav
prevPost={prevPostPreview}
nextPost={nextPostPreview}
postType="news"
id={params.id}
margin="mt-12"
/>
<Tags tags={news.tags} margin="mt-3 ml-6" searchPath={newsPath} />
<AdjPostNav post={news} postType="news" id={params.id} margin="mt-12" />
</PageLayout>
);
}
47 changes: 0 additions & 47 deletions app/[locale]/community/notice/NoticeList.tsx

This file was deleted.

100 changes: 0 additions & 100 deletions app/[locale]/community/notice/NoticePageContent.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions app/[locale]/community/notice/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getNoticePostDetail } from '@/actions/noticeServer';
import { getNoticePostDetail } from '@/apis/notice';

import EditNoticePageContent from '@/app/[locale]/community/notice/EditNoticePageContent';
import EditNoticePageContent from '@/app/[locale]/community/notice/components/EditNoticePageContent';

interface EditNoticePageProps {
params: { id: string };
Expand Down
Loading

0 comments on commit 3132330

Please sign in to comment.