collect-system/apps/web/src/components/models/course/detail/CourseOperationBtns/JoinButton.tsx

82 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-02-27 09:05:31 +08:00
import { useAuth } from "@web/src/providers/auth-provider";
import { useStaff } from "@nice/client";
2025-03-02 11:49:46 +08:00
import { useContext, useState } from "react";
import { useNavigate } from "react-router-dom";
import { CourseDetailContext } from "../PostDetailContext";
import toast from "react-hot-toast";
2025-02-27 09:05:31 +08:00
import {
CheckCircleOutlined,
CloseCircleOutlined,
LoginOutlined,
} from "@ant-design/icons";
2025-03-02 11:49:46 +08:00
export default function JoinButton() {
const { isAuthenticated, user } = useAuth();
2025-02-27 09:05:31 +08:00
const navigate = useNavigate();
2025-03-02 11:49:46 +08:00
const { post, canEdit, userIsLearning, setUserIsLearning } =
useContext(CourseDetailContext);
2025-02-27 09:05:31 +08:00
const { update } = useStaff();
const [isHovered, setIsHovered] = useState(false);
const toggleLearning = async () => {
if (!userIsLearning) {
await update.mutateAsync({
where: { id: user?.id },
data: {
learningPosts: {
2025-03-02 11:49:46 +08:00
connect: { id: post.id },
2025-02-27 09:05:31 +08:00
},
},
});
2025-03-02 11:49:46 +08:00
setUserIsLearning(true);
2025-02-28 10:45:13 +08:00
toast.success("加入学习成功");
2025-02-27 09:05:31 +08:00
} else {
await update.mutateAsync({
where: { id: user?.id },
data: {
learningPosts: {
disconnect: {
2025-03-02 11:49:46 +08:00
id: post.id,
2025-02-27 09:05:31 +08:00
},
},
},
});
2025-02-28 10:45:13 +08:00
toast.success("退出学习成功");
2025-03-02 11:49:46 +08:00
setUserIsLearning(false);
2025-02-27 09:05:31 +08:00
}
};
return (
<>
{isAuthenticated && (
<div
onClick={toggleLearning}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
className={`flex px-1 py-0.5 gap-1 hover:cursor-pointer transition-all ${
userIsLearning
? isHovered
? "text-red-500 border-red-500 rounded-md "
: "text-green-500 "
: "text-primary "
}`}>
{userIsLearning ? (
isHovered ? (
<CloseCircleOutlined />
) : (
<CheckCircleOutlined />
)
) : (
<LoginOutlined />
)}
<span>
{userIsLearning
? isHovered
? "退出学习"
: "正在学习"
: "加入学习"}
</span>
</div>
)}
</>
);
}