109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
import { toast } from "sonner";
|
|
import { useEffect } from "react";
|
|
import { useRouter, usePathname } from "next/navigation";
|
|
import { useClipboard, useConfirm } from "@frontend-opensource/use-react-hooks";
|
|
import { Button } from "@common/components";
|
|
import { AuthorityButton } from "@/entities/menu-authority";
|
|
import { useMenuStore, useUserStore } from "@/shared/lib";
|
|
|
|
type PostActionProps = {
|
|
registerId?: string;
|
|
children: React.ReactNode;
|
|
handleDelete: () => Promise<void>;
|
|
};
|
|
|
|
const basePath = process.env.NODE_ENV === "production" ? "/cms" : "";
|
|
|
|
function PostAction({ registerId, children, handleDelete }: PostActionProps) {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const { confirm } = useConfirm();
|
|
const { copyText, copied } = useClipboard();
|
|
const bbsInfo = useMenuStore((state) => state.bbsInfo);
|
|
const userInfo = useUserStore((state) => state.userInfo);
|
|
const postPageLink = `${basePath}/${bbsInfo?.bbsTypeCd.toLowerCase()}/${bbsInfo?.bbsId}`;
|
|
|
|
const actionButtonAuthrt =
|
|
userInfo?.adminRoleYn === "Y" || userInfo?.userUnqId === registerId;
|
|
|
|
const onDelete = async () => {
|
|
if (await confirm("삭제하시겠습니까?")) handleDelete();
|
|
};
|
|
|
|
const checkAnsUseAuthrt = () => {
|
|
// 관리자
|
|
if (bbsInfo?.ansUseAuthrt === "A") {
|
|
return userInfo?.adminRoleYn === "Y";
|
|
}
|
|
// 회원
|
|
if (bbsInfo?.ansUseAuthrt === "M") {
|
|
return !!userInfo;
|
|
}
|
|
// 비회원
|
|
return true;
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (copied) toast.success("URL이 클립보드에 복사되었습니다.");
|
|
}, [copied]);
|
|
|
|
return (
|
|
<>
|
|
{children}
|
|
<div className="flex justify-center gap-4 pb-12 pt-10">
|
|
<Button
|
|
size="lg"
|
|
color="emerald"
|
|
type="button"
|
|
onClick={() => copyText(window.location.href)}
|
|
>
|
|
퍼가기
|
|
</Button>
|
|
<Button
|
|
size="lg"
|
|
color="white"
|
|
type="button"
|
|
onClick={() => router.push(postPageLink)}
|
|
>
|
|
목록
|
|
</Button>
|
|
{actionButtonAuthrt && (
|
|
<AuthorityButton
|
|
authority="mdfcnAuthrtYn"
|
|
size="lg"
|
|
type="button"
|
|
onClick={() => router.push(`${pathname}/modify`)}
|
|
>
|
|
수정
|
|
</AuthorityButton>
|
|
)}
|
|
{bbsInfo?.ansUseYn === "Y" && (
|
|
<AuthorityButton
|
|
authority="regAuthrtYn"
|
|
size="lg"
|
|
type="button"
|
|
color="gray"
|
|
onClick={() => router.push(`${pathname}/answer`)}
|
|
disabled={!checkAnsUseAuthrt()}
|
|
>
|
|
답글
|
|
</AuthorityButton>
|
|
)}
|
|
{actionButtonAuthrt && (
|
|
<AuthorityButton
|
|
authority="delAuthrtYn"
|
|
size="lg"
|
|
color="red"
|
|
type="button"
|
|
onClick={onDelete}
|
|
>
|
|
삭제
|
|
</AuthorityButton>
|
|
)}
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export { PostAction };
|