import { useEffect, useRef, useState } from "react"; import { Dropdown, PullToRefresh, Skeleton, Tabs } from "antd-mobile"; import { sleep } from "antd-mobile/es/utils/sleep"; import { DropdownRef } from "antd-mobile/es/components/dropdown"; import { user } from "../../api/index"; import styles from "./index.module.scss"; import { useSelector } from "react-redux"; import { useNavigate, useLocation } from "react-router-dom"; import { Footer, Empty } from "../../components"; import { CoursesModel } from "./compenents/courses-model"; import { isEmptyObject } from "../../utils/index"; type LocalUserLearnHourRecordModel = { [key: number]: UserLearnHourRecordModel; }; type LocalUserLearnHourCountModel = { [key: number]: number; }; const IndexPage = () => { const ref = useRef(null); const navigate = useNavigate(); const result = new URLSearchParams(useLocation().search); const [loading, setLoading] = useState(false); const [tabKey, setTabKey] = useState(result.get("tab") || "0"); const [coursesList, setCoursesList] = useState([]); const [categories, setCategories] = useState([]); const [categoryId, setCategoryId] = useState( Number(result.get("cid") || 0) ); const [categoryText, setCategoryText] = useState( String(result.get("catName") || "所有分类") ); const [learnCourseRecords, setLearnCourseRecords] = useState({}); const [learnCourseHourCount, setLearnCourseHourCount] = useState({}); const systemConfig = useSelector((state: any) => state.systemConfig.value); const currentDepId = useSelector( (state: any) => state.loginUser.value.currentDepId ); const items = [ { key: "0", label: `全部`, }, { key: "1", label: `必修课`, }, { key: "2", label: `选修课`, }, { key: "3", label: `已学完`, }, { key: "4", label: `未学完`, }, ]; useEffect(() => { document.title = systemConfig.systemName || "首页"; }, [systemConfig]); useEffect(() => { getParams(); }, []); useEffect(() => { setLoading(true); if (currentDepId === 0) { setLoading(false); return; } getData(); }, [currentDepId, categoryId]); const getData = () => { user.courses(currentDepId, categoryId).then((res: any) => { const records = res.data.learn_course_records; setLearnCourseRecords(records); setLearnCourseHourCount(res.data.user_course_hour_count); if (Number(tabKey) === 0) { setCoursesList(res.data.courses); } else if (Number(tabKey) === 1) { const arr: any = []; res.data.courses.map((item: any) => { if (item.is_required === 1) { arr.push(item); } }); setCoursesList(arr); } else if (Number(tabKey) === 2) { const arr: any = []; res.data.courses.map((item: any) => { if (item.is_required === 0) { arr.push(item); } }); setCoursesList(arr); } else if (Number(tabKey) === 3) { const arr: any = []; res.data.courses.map((item: any) => { if (records[item.id] && records[item.id].progress >= 10000) { arr.push(item); } }); setCoursesList(arr); } else if (Number(tabKey) === 4) { const arr: any = []; res.data.courses.map((item: any) => { if ( !records[item.id] || (records[item.id] && records[item.id].progress < 10000) ) { arr.push(item); } }); setCoursesList(arr); } setLoading(false); }); }; const getParams = () => { user.coursesCategories().then((res: any) => { const categories = res.data.categories; if (!isEmptyObject(categories)) { const new_arr: any[] = 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: any[] = checkArr(categories, categories[id][i].id); arr.push({ title: categories[id][i].name, key: categories[id][i].id, children: new_arr, }); } } return arr; }; const renderChildCategory = (data: any) => { return ( <> {data.map((item: any) => (
{ setCategoryId(item.key); setCategoryText(item.title); navigate( "/?cid=" + item.key + "&catName=" + item.title + "&tab=" + tabKey ); ref.current?.close(); }} > {item.title}
{item.children && item.children.length > 0 && renderChildCategory(item.children)}
))} ); }; return (
{ setTabKey(key); setTimeout(() => { navigate( "/?cid=" + categoryId + "&catName=" + categoryText + "&tab=" + key ); }, 250); }} style={{ "--fixed-active-line-width": "20px", "--active-line-height": "3px", "--active-title-color": "rgba(0,0,0,0.88)", "--title-font-size": "16px", }} > {items.map((item) => ( ))}
{categories.map((item: any) => (
{ setCategoryId(item.key); setCategoryText(item.title); navigate( "/?cid=" + item.key + "&catName=" + item.title + "&tab=" + tabKey ); ref.current?.close(); }} > {item.title}
{item.children && item.children.length > 0 && renderChildCategory(item.children)}
))}
{ setLoading(true); await sleep(700); getData(); }} >
{loading && Array.from({ length: 2 }).map((_, i) => (
))} {!loading && coursesList.length === 0 && } {!loading && coursesList.length > 0 && ( <> {coursesList.map((item: any) => (
))} )}
{" "}
); }; export default IndexPage;