"use client"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { useConfirm } from "@frontend-opensource/use-react-hooks"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem, Button, } from "@common/components"; import { CONTENTS_APPROVE_STATUS, ContentsApi, contentsFormSchema, type ContentsFormData, } from "@/entities/content"; import { ENDPOINT } from "@/shared/config"; import { useAlertStore, useMutationWrapper } from "@/shared/lib"; import { ContentsForm } from "./contents-form"; function ModifyPage({ sequential }: { sequential: string }) { const router = useRouter(); const { confirm } = useConfirm(); const queryClient = useQueryClient(); const alert = useAlertStore((state) => state.setMessage); const contsSn = parseInt(sequential); const [contsVer, setContsVer] = useState(0); const { data: contentsVersionList } = useQuery({ queryKey: [ENDPOINT.CMS_SERVICE.CONTENTS_DETAILS, contsSn], queryFn: () => ContentsApi.getContentsVersionList({ contsSn: contsSn, page: 0, size: 100, }), }); const { data: contentsDetail } = useQuery({ queryKey: [ENDPOINT.CMS_SERVICE.CONTENTS_DETAILS, contsVer], queryFn: () => ContentsApi.getLatestContentsInfo(contsSn, contsVer), enabled: !!contsVer, }); const { mutateAsync: createContents } = useMutationWrapper({ mutationFn: (data: ContentsFormData) => ContentsApi.createContents(data), onSuccess: () => { setContsVer(0); alert("수정되었습니다.", () => { queryClient.invalidateQueries({ queryKey: [ENDPOINT.CMS_SERVICE.CONTENTS_DETAILS, contsSn], }); router.back(); }); }, }); const { mutateAsync: updateContents } = useMutationWrapper({ mutationFn: (data: ContentsFormData) => ContentsApi.updateContents(contsSn, contsVer, data), onSuccess: () => { alert("수정되었습니다.", router.back); }, }); const { mutateAsync: deleteContents } = useMutationWrapper({ mutationFn: () => ContentsApi.deleteContents(contsSn, contsVer), onSuccess: () => { setContsVer(0); alert("삭제되었습니다.", () => { queryClient.invalidateQueries({ queryKey: [ENDPOINT.CMS_SERVICE.CONTENTS_DETAILS, contsSn], }); router.back(); }); }, }); const { mutateAsync: requestApprove } = useMutationWrapper({ mutationFn: () => ContentsApi.requestApprove(contsSn, contsVer), onSuccess: () => { alert("승인 요청이 완료되었습니다."); queryClient.invalidateQueries({ queryKey: [ENDPOINT.CMS_SERVICE.CONTENTS_DETAILS], }); }, }); const { mutateAsync: approveContents } = useMutationWrapper({ mutationFn: () => ContentsApi.approveContents(contsSn, contsVer), onSuccess: () => { alert("승인되었습니다."); queryClient.invalidateQueries({ queryKey: [ENDPOINT.CMS_SERVICE.CONTENTS_DETAILS], }); }, }); const { mutateAsync: rejectContents } = useMutationWrapper({ mutationFn: () => ContentsApi.rejectContents(contsSn, contsVer), onSuccess: () => { alert("반려되었습니다."); queryClient.invalidateQueries({ queryKey: [ENDPOINT.CMS_SERVICE.CONTENTS_DETAILS], }); }, }); const form = useForm({ resolver: zodResolver(contentsFormSchema), values: contentsDetail, }); const approveStatus = contentsDetail?.aprvCd; const isContsTtlDisabled = (contentsVersionList?.content?.[0]?.contsVer ?? 0) > 1; const onRequestApprove = async () => { if ( await confirm( "승인 요청하면 더이상 내용을 수정할 수 없습니다.\n요청하시겠습니까?", ) ) requestApprove(); }; const onApprove = async () => { if (await confirm("승인하시겠습니까?")) approveContents(); }; const onReject = async () => { if (await confirm("반려하시겠습니까?")) rejectContents(); }; useEffect(() => { if (contsVer) return; if (!contentsVersionList?.content?.length) return; const latestContent = contentsVersionList?.content[0]; if (!latestContent) return; setContsVer(latestContent?.contsVer ?? 0); }, [contentsVersionList?.content]); return (

템플릿 수정

{approveStatus === CONTENTS_APPROVE_STATUS.DRAFT && ( )} {approveStatus === CONTENTS_APPROVE_STATUS.REQUESTED && ( <> )}
); } export { ModifyPage };