54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import { useRouter } from "next/navigation";
|
|
import { useForm } from "react-hook-form";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { 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, useMutationWrapper } from "@/shared/lib";
|
|
|
|
function ModifyPage({ bbsId, pstNo }: { bbsId: string; pstNo: string }) {
|
|
const router = useRouter();
|
|
const alert = useAlertStore((state) => state.setMessage);
|
|
const userInfo = useUserStore((state) => state.userInfo);
|
|
|
|
const { data: postInfo } = useQuery({
|
|
queryKey: [ENDPOINT.BOARD_SERVICE.POSTS, bbsId, pstNo],
|
|
queryFn: () => PostApi.getPostInfo(parseInt(bbsId), parseInt(pstNo)),
|
|
});
|
|
|
|
const { mutateAsync: updatePost } = useMutationWrapper({
|
|
mutationFn: (data: PostUpdateFormData) =>
|
|
PostApi.updatePost(parseInt(bbsId), parseInt(pstNo), data),
|
|
onSuccess: () => {
|
|
alert("수정되었습니다.", router.back);
|
|
},
|
|
});
|
|
|
|
const form = useForm<PostUpdateFormData>({
|
|
resolver: zodResolver(postUpdateFormSchema),
|
|
values: {
|
|
...postInfo,
|
|
pstTtl: postInfo?.pstTtl ?? "",
|
|
pstPswd: userInfo?.userUnqId ?? "",
|
|
},
|
|
});
|
|
|
|
return (
|
|
<div className="card card-border !mt-6">
|
|
<div className="card-header">
|
|
<h3 className="text-2xl font-medium text-label">게시글 수정</h3>
|
|
</div>
|
|
<PostForm form={form} handleSave={updatePost} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export { ModifyPage };
|