首页课程分类选择

This commit is contained in:
禺狨
2023-05-08 16:57:54 +08:00
parent dec09df6b7
commit 453bf9d8c8
7 changed files with 185 additions and 10 deletions

View File

@@ -64,6 +64,7 @@
margin-top: 50px;
display: flex;
align-items: center;
position: relative;
.tab-item {
width: 64px;
height: 48px;
@@ -108,6 +109,19 @@
animation: scaleTransX 0.3s;
}
}
.dropButton {
height: 40px;
font-size: 14px;
font-weight: 400;
color: rgba(0, 0, 0, 0.45);
line-height: 40px;
display: flex;
align-items: center;
position: absolute;
right: 0;
top: 0;
cursor: pointer;
}
}
@keyframes scaleTransX {

View File

@@ -1,6 +1,6 @@
import { useEffect, useState } from "react";
import { Row, Col, Spin, Image } from "antd";
import type { TabsProps } from "antd";
import { Row, Col, Spin, Tree, Popover, Space, Image } from "antd";
import type { MenuProps } from "antd";
import { user } from "../../api/index";
import styles from "./index.module.scss";
import { useSelector } from "react-redux";
@@ -11,11 +11,20 @@ import studyTime from "../../assets/images/commen/icon-studytime.png";
import iconRoute from "../../assets/images/commen/icon-route.png";
import { studyTimeFormat } from "../../utils/index";
interface Option {
key: string | number;
title: any;
children?: Option[];
}
const IndexPage = () => {
const systemConfig = useSelector((state: any) => state.systemConfig.value);
const [loading, setLoading] = useState<boolean>(false);
const [tabKey, setTabKey] = useState(0);
const [coursesList, setCoursesList] = useState<any>([]);
const [categories, setCategories] = useState<any>([]);
const [categoryId, setCategoryId] = useState<number>(0);
const [categoryText, setCategoryText] = useState<string>("所有分类");
const [learnCourseRecords, setLearnCourseRecords] = useState<any>({});
const [learnCourseHourCount, setLearnCourseHourCount] = useState<any>({});
const [stats, setStats] = useState<any>({});
@@ -27,12 +36,16 @@ const IndexPage = () => {
(state: any) => state.loginUser.value.currentDepId
);
useEffect(() => {
getParams();
}, []);
useEffect(() => {
if (currentDepId === 0) {
return;
}
getData();
}, [tabKey, currentDepId]);
}, [tabKey, currentDepId, categoryId]);
useEffect(() => {
document.title = systemConfig.systemName || "首页";
@@ -40,7 +53,7 @@ const IndexPage = () => {
const getData = () => {
setLoading(true);
user.courses(currentDepId).then((res: any) => {
user.courses(currentDepId, categoryId).then((res: any) => {
const records = res.data.learn_course_records;
setStats(res.data.stats);
setLearnCourseRecords(records);
@@ -87,6 +100,40 @@ const IndexPage = () => {
});
};
const getParams = () => {
user.coursesCategories().then((res: any) => {
const categories = res.data.categories;
if (JSON.stringify(categories) !== "{}") {
const new_arr: Option[] = checkArr(categories, 0);
new_arr.unshift({
key: 0,
title: "所有分类",
});
setCategories(new_arr);
}
});
};
const checkArr = (categories: any[], id: number) => {
const arr = [];
for (let i = 0; i < categories[id].length; i++) {
if (!categories[categories[id][i].id]) {
arr.push({
title: categories[id][i].name,
key: categories[id][i].id,
});
} else {
const new_arr: Option[] = checkArr(categories, categories[id][i].id);
arr.push({
title: categories[id][i].name,
key: categories[id][i].id,
children: new_arr,
});
}
}
return arr;
};
const items = [
{
key: 0,
@@ -114,6 +161,29 @@ const IndexPage = () => {
setTabKey(key);
};
const onSelect = (selectedKeys: any, info: any) => {
setCategoryId(selectedKeys[0]);
setCategoryText(info.node.title);
};
const dropItem = (
<div
style={{
maxHeight: 600,
overflowX: "hidden",
overflowY: "auto",
}}
>
<Tree
switcherIcon={null}
onSelect={onSelect}
treeData={categories}
blockNode
defaultExpandAll={true}
/>
</div>
);
return (
<div className="main-body">
<div className="content">
@@ -238,6 +308,15 @@ const IndexPage = () => {
)}
</div>
))}
<Popover content={dropItem} placement="bottomRight">
<Space className={styles["dropButton"]}>
{categoryText}
<i
className="iconfont icon-icon-xiala"
style={{ fontSize: 16 }}
/>
</Space>
</Popover>
</div>
{loading && (
<Row
@@ -278,7 +357,9 @@ const IndexPage = () => {
title={item.title}
thumb={item.thumb}
isRequired={item.is_required}
progress={learnCourseRecords[item.id].progress / 100}
progress={Math.floor(
learnCourseRecords[item.id].progress / 100
)}
></CoursesModel>
)}