207 lines
6.1 KiB
TypeScript
207 lines
6.1 KiB
TypeScript
"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<ContentsFormData>({
|
|
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 (
|
|
<div className="card card-border !mb-0 !mt-6">
|
|
<div className="card-header">
|
|
<h3 className="text-2xl font-medium text-label">템플릿 수정</h3>
|
|
<div className="flex items-center gap-2">
|
|
<Select
|
|
value={contsVer.toString()}
|
|
onValueChange={(value) => {
|
|
setContsVer(parseInt(value));
|
|
}}
|
|
>
|
|
<SelectTrigger className="w-[28rem]">
|
|
<SelectValue placeholder="선택하세요" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{contentsVersionList?.content?.map((item) => (
|
|
<SelectItem
|
|
key={item.contsVer}
|
|
value={item.contsVer.toString()}
|
|
>
|
|
{item.contsCn}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
{approveStatus === CONTENTS_APPROVE_STATUS.DRAFT && (
|
|
<Button onClick={onRequestApprove}>승인요청</Button>
|
|
)}
|
|
{approveStatus === CONTENTS_APPROVE_STATUS.REQUESTED && (
|
|
<>
|
|
<Button onClick={onApprove}>승인</Button>
|
|
<Button color="gray" onClick={onReject}>
|
|
반려
|
|
</Button>
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<ContentsForm
|
|
form={form}
|
|
contsSn={contsSn}
|
|
isEditable={approveStatus !== CONTENTS_APPROVE_STATUS.REQUESTED}
|
|
isContsTtlDisabled={isContsTtlDisabled}
|
|
handleSave={
|
|
approveStatus === CONTENTS_APPROVE_STATUS.APPROVED
|
|
? createContents
|
|
: updateContents
|
|
}
|
|
handleDelete={deleteContents}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export { ModifyPage };
|