diff --git a/apps/cs/src/pages/(post)/image/ui/modify-page.tsx b/apps/cs/src/pages/(post)/image/ui/modify-page.tsx index c2426c134..6b92afbb4 100644 --- a/apps/cs/src/pages/(post)/image/ui/modify-page.tsx +++ b/apps/cs/src/pages/(post)/image/ui/modify-page.tsx @@ -1,53 +1,9 @@ "use client"; -import { useRouter } from "next/navigation"; -import { useForm } from "react-hook-form"; -import { zodResolver } from "@hookform/resolvers/zod"; -import { useMutation, useQuery } from "@tanstack/react-query"; import { PostForm } from "@/widgets/post"; -import { - PostApi, - postUpdateFormSchema, - type PostUpdateFormData, -} from "@/entities/post"; -import { ENDPOINT } from "@/shared/config"; -import { useAlertStore, useUserStore } from "@/shared/lib"; function ModifyPage({ bbsId, pstNo }: { bbsId: string; pstNo: string }) { - const router = useRouter(); - const { setMessage: alert } = useAlertStore((state) => state); - const { userInfo } = useUserStore((state) => state); - - const { data: postInfo } = useQuery({ - queryKey: [ENDPOINT.BOARD_SERVICE.POSTS, bbsId, pstNo], - queryFn: () => PostApi.getPostInfo(parseInt(bbsId), parseInt(pstNo)), - }); - - const { mutateAsync: updatePost } = useMutation({ - mutationFn: (data: PostUpdateFormData) => - PostApi.updatePost(parseInt(bbsId), parseInt(pstNo), data), - onSuccess: () => { - alert("수정되었습니다.", router.back); - }, - }); - - const form = useForm({ - resolver: zodResolver(postUpdateFormSchema), - values: { - ...postInfo, - pstTtl: postInfo?.pstTtl ?? "", - pstPswd: userInfo?.userUnqId ?? "", - }, - }); - - return ( -
-
-

이미지 게시글 수정

-
- -
- ); + return ; } export { ModifyPage }; diff --git a/apps/cs/src/pages/(post)/movie/ui/modify-page.tsx b/apps/cs/src/pages/(post)/movie/ui/modify-page.tsx index bee473938..6b92afbb4 100644 --- a/apps/cs/src/pages/(post)/movie/ui/modify-page.tsx +++ b/apps/cs/src/pages/(post)/movie/ui/modify-page.tsx @@ -1,53 +1,9 @@ "use client"; -import { useRouter } from "next/navigation"; -import { useForm } from "react-hook-form"; -import { zodResolver } from "@hookform/resolvers/zod"; -import { useMutation, useQuery } from "@tanstack/react-query"; import { PostForm } from "@/widgets/post"; -import { - PostApi, - postUpdateFormSchema, - type PostUpdateFormData, -} from "@/entities/post"; -import { ENDPOINT } from "@/shared/config"; -import { useAlertStore, useUserStore } from "@/shared/lib"; function ModifyPage({ bbsId, pstNo }: { bbsId: string; pstNo: string }) { - const router = useRouter(); - const { setMessage: alert } = useAlertStore((state) => state); - const { userInfo } = useUserStore((state) => state); - - const { data: postInfo } = useQuery({ - queryKey: [ENDPOINT.BOARD_SERVICE.POSTS, bbsId, pstNo], - queryFn: () => PostApi.getPostInfo(parseInt(bbsId), parseInt(pstNo)), - }); - - const { mutateAsync: updatePost } = useMutation({ - mutationFn: (data: PostUpdateFormData) => - PostApi.updatePost(parseInt(bbsId), parseInt(pstNo), data), - onSuccess: () => { - alert("수정되었습니다.", router.back); - }, - }); - - const form = useForm({ - resolver: zodResolver(postUpdateFormSchema), - values: { - ...postInfo, - pstTtl: postInfo?.pstTtl ?? "", - pstPswd: userInfo?.userUnqId ?? "", - }, - }); - - return ( -
-
-

동영상 게시글 수정

-
- -
- ); + return ; } export { ModifyPage }; diff --git a/apps/cs/src/pages/(post)/normal/ui/modify-page.tsx b/apps/cs/src/pages/(post)/normal/ui/modify-page.tsx index 2e23f62a0..6b92afbb4 100644 --- a/apps/cs/src/pages/(post)/normal/ui/modify-page.tsx +++ b/apps/cs/src/pages/(post)/normal/ui/modify-page.tsx @@ -1,53 +1,9 @@ "use client"; -import { useRouter } from "next/navigation"; -import { useForm } from "react-hook-form"; -import { zodResolver } from "@hookform/resolvers/zod"; -import { useMutation, useQuery } from "@tanstack/react-query"; import { PostForm } from "@/widgets/post"; -import { - PostApi, - postUpdateFormSchema, - type PostUpdateFormData, -} from "@/entities/post"; -import { ENDPOINT } from "@/shared/config"; -import { useAlertStore, useUserStore } from "@/shared/lib"; function ModifyPage({ bbsId, pstNo }: { bbsId: string; pstNo: string }) { - const router = useRouter(); - const { setMessage: alert } = useAlertStore((state) => state); - const { userInfo } = useUserStore((state) => state); - - const { data: postInfo } = useQuery({ - queryKey: [ENDPOINT.BOARD_SERVICE.POSTS, bbsId, pstNo], - queryFn: () => PostApi.getPostInfo(parseInt(bbsId), parseInt(pstNo)), - }); - - const { mutateAsync: updatePost } = useMutation({ - mutationFn: (data: PostUpdateFormData) => - PostApi.updatePost(parseInt(bbsId), parseInt(pstNo), data), - onSuccess: () => { - alert("수정되었습니다.", router.back); - }, - }); - - const form = useForm({ - resolver: zodResolver(postUpdateFormSchema), - values: { - ...postInfo, - pstTtl: postInfo?.pstTtl ?? "", - pstPswd: userInfo?.userUnqId ?? "", - }, - }); - - return ( -
-
-

게시글 수정

-
- -
- ); + return ; } export { ModifyPage }; diff --git a/apps/cs/src/pages/(post)/notice/ui/modify-page.tsx b/apps/cs/src/pages/(post)/notice/ui/modify-page.tsx index fa12dbcce..6b92afbb4 100644 --- a/apps/cs/src/pages/(post)/notice/ui/modify-page.tsx +++ b/apps/cs/src/pages/(post)/notice/ui/modify-page.tsx @@ -1,53 +1,9 @@ "use client"; -import { useRouter } from "next/navigation"; -import { useForm } from "react-hook-form"; -import { zodResolver } from "@hookform/resolvers/zod"; -import { useMutation, useQuery } from "@tanstack/react-query"; import { PostForm } from "@/widgets/post"; -import { - PostApi, - postUpdateFormSchema, - type PostUpdateFormData, -} from "@/entities/post"; -import { ENDPOINT } from "@/shared/config"; -import { useAlertStore, useUserStore } from "@/shared/lib"; function ModifyPage({ bbsId, pstNo }: { bbsId: string; pstNo: string }) { - const router = useRouter(); - const { setMessage: alert } = useAlertStore((state) => state); - const { userInfo } = useUserStore((state) => state); - - const { data: postInfo } = useQuery({ - queryKey: [ENDPOINT.BOARD_SERVICE.POSTS, bbsId, pstNo], - queryFn: () => PostApi.getPostInfo(parseInt(bbsId), parseInt(pstNo)), - }); - - const { mutateAsync: updatePost } = useMutation({ - mutationFn: (data: PostUpdateFormData) => - PostApi.updatePost(parseInt(bbsId), parseInt(pstNo), data), - onSuccess: () => { - alert("수정되었습니다.", router.back); - }, - }); - - const form = useForm({ - resolver: zodResolver(postUpdateFormSchema), - values: { - ...postInfo, - pstTtl: postInfo?.pstTtl ?? "", - pstPswd: userInfo?.userUnqId ?? "", - }, - }); - - return ( -
-
-

공지 게시글 수정

-
- -
- ); + return ; } export { ModifyPage }; diff --git a/apps/cs/src/widgets/post/ui/post-form.tsx b/apps/cs/src/widgets/post/ui/post-form.tsx index cf3c14aab..dcd41de64 100644 --- a/apps/cs/src/widgets/post/ui/post-form.tsx +++ b/apps/cs/src/widgets/post/ui/post-form.tsx @@ -1,7 +1,8 @@ import { useRouter } from "next/navigation"; -import { useEffect, useState } from "react"; -import { useQuery } from "@tanstack/react-query"; -import type { UseFormReturn, FieldPath } from "react-hook-form"; +import { useState } from "react"; +import { useMutation, useQuery } from "@tanstack/react-query"; +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; import { useConfirm } from "@frontend-opensource/use-react-hooks"; import { Input, @@ -18,159 +19,114 @@ import { } from "@common/components"; import { BOARD_POSTING_CODE } from "@/entities/board"; import { AttachmentApi } from "@/entities/attachment"; -import type { Post, PostFormData, PostUpdateFormData } from "@/entities/post"; +import { + PostApi, + postUpdateFormSchema, + type PostFormData, + type PostUpdateFormData, +} from "@/entities/post"; import { ENDPOINT } from "@/shared/config"; -import { useUserStore, setCookie, useMenuStore } from "@/shared/lib"; +import type { BbsType } from "@/shared/store/menu-store"; +import { useUserStore, useMenuStore, useAlertStore } from "@/shared/lib"; import { Form, FormControl, FormField, FormItem } from "@/shared/ui/form"; import { PostingPeriodSelector } from "./posting-period-selector"; import { GeneralFileUploader } from "./general-file-uploader"; import { WithPreviewFileUploader } from "./with-preview-file-uploader"; -type PostFormProps = { - form: UseFormReturn; - handleSave: (data: T) => Promise; +type PostFormProps = { + bbsId: number; + pstNo: number; }; -function PostForm({ - form, - handleSave, -}: PostFormProps) { - const atflCd = form.getValues("atflCd" as FieldPath); +const getBbsTitle = (bbsTypeCd?: BbsType) => { + switch (bbsTypeCd) { + case "BBSNOTICE": + return "공지"; + case "BBSNORMAL": + return "일반"; + case "BBSFAQ": + return "FAQ"; + case "BBSIMAGE": + return "이미지"; + case "BBSMOVIE": + return "동영상"; + default: + return "게시판"; + } +}; +function PostForm({ bbsId, pstNo }: PostFormProps) { const router = useRouter(); const { confirm } = useConfirm(); const { bbsInfo } = useMenuStore((state) => state); const { userInfo } = useUserStore((state) => state); + const { setMessage: alert } = useAlertStore((state) => state); const [deleteAttachmentIds, setDeleteAttachmentIds] = useState([]); + const { data: postInfo } = useQuery({ + queryKey: [ENDPOINT.BOARD_SERVICE.POSTS, bbsId, pstNo], + queryFn: () => PostApi.getPostInfo(bbsId, pstNo), + }); + + const { mutateAsync: updatePost } = useMutation({ + mutationFn: (data: PostUpdateFormData) => + PostApi.updatePost(bbsId, pstNo, data), + onSuccess: () => { + alert("수정되었습니다.", router.back); + }, + }); + + const form = useForm({ + resolver: zodResolver(postUpdateFormSchema), + values: { + ...postInfo, + pstTtl: postInfo?.pstTtl ?? "", + pstPswd: userInfo?.userUnqId ?? "", + }, + }); + + const atflCd = form.getValues("atflCd"); const { data: attachmentData } = useQuery({ queryKey: [ENDPOINT.ATTACH_SERVICE.ATTACHMENTS, atflCd], queryFn: () => AttachmentApi.getAttachmentsInfo(atflCd as string), enabled: !!atflCd, }); - const onSubmit = async (data: T) => { + const onSubmit = async (data: PostFormData | PostUpdateFormData) => { if (await confirm("저장하시겠습니까?")) { if (deleteAttachmentIds.length > 0) { await Promise.all( deleteAttachmentIds.map(AttachmentApi.deleteAttachmentByAtflId), ); } - await handleSave(data); + await updatePost(data); } }; - useEffect(() => { - setCookie("atfl-cd-key", ""); - }, []); - return ( -
- -
- - - - - - - - 제목 - - } - render={({ field }) => ( - - - - - - )} - /> - - - - 작성자 - - } - render={({ field }) => ( - - - - - - )} - /> - - - {!userInfo?.userUnqId && ( +
+
+

+ {getBbsTitle(bbsInfo?.bbsTypeCd)} 게시글 수정 +

+
+ + +
+
+ + + + + - 비밀번호 + 제목 } - render={({ field }) => ( - - - - - - )} - /> - - - )} - - 내용 - - } - render={({ field }) => ( - - -