staff_data/apps/web/src/app/main/home/components/CoursesSection.tsx

150 lines
4.1 KiB
TypeScript
Raw Normal View History

2025-02-25 16:23:36 +08:00
import React, { useState, useMemo, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { Button, Card, Typography, Tag, Spin, Empty } from "antd";
2025-02-06 16:32:31 +08:00
import {
2025-02-25 16:23:36 +08:00
PlayCircleOutlined,
TeamOutlined,
ArrowRightOutlined,
} from "@ant-design/icons";
2025-02-25 16:43:42 +08:00
import {
courseDetailSelect,
CourseDto,
TaxonomySlug,
TermDto,
} from "@nice/common";
2025-02-25 16:23:36 +08:00
import { api } from "@nice/client";
import CourseCard from "../../courses/components/CourseCard";
2025-02-25 17:45:00 +08:00
import { CoursesSectionTag } from "./CoursesSectionTag";
2025-02-24 08:52:50 +08:00
interface GetTaxonomyProps {
2025-02-25 16:23:36 +08:00
categories: string[];
isLoading: boolean;
2025-02-24 08:52:50 +08:00
}
2025-02-24 19:10:38 +08:00
function useGetTaxonomy({ type }): GetTaxonomyProps {
2025-02-25 16:23:36 +08:00
const { data, isLoading }: { data: TermDto[]; isLoading: boolean } =
api.term.findMany.useQuery({
where: {
taxonomy: {
slug: type,
},
},
include: {
children: true,
},
take: 10, // 只取前10个
orderBy: {},
});
const categories = useMemo(() => {
const allCategories = isLoading
? []
: data?.map((course) => course.name);
return [...Array.from(new Set(allCategories))];
}, [data]);
return { categories, isLoading };
2025-02-24 08:52:50 +08:00
}
2025-02-25 10:58:35 +08:00
// 不同分类跳转
2025-02-25 10:01:17 +08:00
function useFetchCoursesByCategory(category: string) {
2025-02-25 16:23:36 +08:00
const isAll = category === "全部";
const { data, isLoading }: { data: CourseDto[]; isLoading: boolean } =
api.post.findMany.useQuery({
where: isAll
? {}
: {
terms: {
some: {
name: category,
},
},
},
take: 8,
2025-02-25 16:43:42 +08:00
select: courseDetailSelect,
2025-02-25 16:23:36 +08:00
});
return { data, isLoading };
2025-02-25 10:01:17 +08:00
}
2025-02-06 16:32:31 +08:00
const { Title, Text } = Typography;
interface CoursesSectionProps {
2025-02-25 16:23:36 +08:00
title: string;
description: string;
initialVisibleCoursesCount?: number;
2025-02-06 16:32:31 +08:00
}
const CoursesSection: React.FC<CoursesSectionProps> = ({
2025-02-25 16:23:36 +08:00
title,
description,
2025-02-06 16:32:31 +08:00
}) => {
2025-02-25 16:23:36 +08:00
const navigate = useNavigate();
const [selectedCategory, setSelectedCategory] = useState<string>("全部");
const gateGory: GetTaxonomyProps = useGetTaxonomy({
type: TaxonomySlug.CATEGORY,
});
const { data, isLoading: isDataLoading } =
useFetchCoursesByCategory(selectedCategory);
const filteredCourses = useMemo(() => {
return selectedCategory === "全部"
? data
: data?.filter((c) =>
c.terms.some((t) => t.name === selectedCategory)
);
}, [selectedCategory, data]);
const displayedCourses = isDataLoading ? [] : filteredCourses;
2025-02-25 17:44:33 +08:00
useEffect(()=>{
console.log(data)
})
2025-02-25 16:23:36 +08:00
return (
<section className="relative py-20 overflow-hidden bg-gradient-to-b from-gray-50 to-white">
<div className="max-w-screen-2xl mx-auto px-6 relative">
<div className="flex justify-between items-end mb-16">
<div>
<Title
level={2}
className="font-bold text-5xl mb-6 bg-gradient-to-r from-blue-600 to-purple-600 bg-clip-text text-transparent">
{title}
</Title>
<Text
type="secondary"
className="text-xl font-light text-gray-600">
{description}
</Text>
</div>
</div>
<div className="mb-12 flex flex-wrap gap-4">
{gateGory.isLoading ? (
<Spin className="m-3" />
) : (
<>
2025-02-25 17:47:09 +08:00
{['全部',...gateGory.categories].map((category,idx) => (
<CoursesSectionTag key={idx} category= {category} selectedCategory={selectedCategory} setSelectedCategory={setSelectedCategory}/>
2025-02-25 16:23:36 +08:00
))}
</>
)}
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-8">
2025-02-25 17:45:00 +08:00
{isDataLoading ? (
<Spin className="m-3" />
2025-02-25 16:23:36 +08:00
) : (
2025-02-25 19:08:30 +08:00
displayedCourses?.map((course,index) => (
<CourseCard course={course} key={index}></CourseCard>
2025-02-25 16:23:36 +08:00
))
)}
</div>
{
<div className="flex items-center gap-4 justify-between mt-12">
<div className="h-[1px] flex-grow bg-gradient-to-r from-transparent via-gray-300 to-transparent"></div>
<div className="flex justify-end">
<Button
type="link"
onClick={() => navigate("/courses")}
className="flex items-center gap-2 text-gray-600 hover:text-blue-600 font-medium transition-colors duration-300">
<ArrowRightOutlined />
</Button>
</div>
</div>
}
</div>
</section>
);
2025-02-06 16:32:31 +08:00
};
export default CoursesSection;