import { useEffect, useState } from "react"; import { Row, Col, Empty, Spin, Tabs } from "antd"; import type { TabsProps } from "antd"; import { user } from "../../api/index"; import styles from "./index.module.scss"; import { useSelector } from "react-redux"; import { CoursesModel } from "./compenents/courses-model"; import myLesoon from "../../assets/images/commen/icon-mylesoon.png"; import studyTime from "../../assets/images/commen/icon-studytime.png"; import { studyTimeFormat } from "../../utils/index"; const IndexPage = () => { const [loading, setLoading] = useState(false); const [tabKey, setTabKey] = useState(0); const [coursesList, setCoursesList] = useState([]); const [learnCourseRecords, setLearnCourseRecords] = useState({}); const [stats, setStats] = useState({}); const departments = useSelector( (state: any) => state.loginUser.value.departments ); useEffect(() => { getData(); }, [tabKey]); const getData = () => { setLoading(true); user.courses(departments[0].id).then((res: any) => { const records = res.data.learn_course_records; setStats(res.data.stats); setLearnCourseRecords(records); if (tabKey === 0) { setCoursesList(res.data.courses); } else if (tabKey === 1) { const arr: any = []; res.data.courses.map((item: any) => { if (item.is_required === 1) { arr.push(item); } }); setCoursesList(arr); } else if (tabKey === 2) { const arr: any = []; res.data.courses.map((item: any) => { if (item.is_required === 0) { arr.push(item); } }); setCoursesList(arr); } else if (tabKey === 3) { const arr: any = []; res.data.courses.map((item: any) => { if (records[item.id] && records[item.id].progress === 100) { arr.push(item); } }); setCoursesList(arr); } else if (tabKey === 4) { const arr: any = []; res.data.courses.map((item: any) => { if ( !records[item.id] || (records[item.id] && records[item.id].progress !== 100) ) { arr.push(item); } }); setCoursesList(arr); } setLoading(false); }); }; const items: TabsProps["items"] = [ { key: "0", label: `全部`, }, { key: "1", label: `必修课`, }, { key: "2", label: `选修课`, }, { key: "3", label: `已学完`, }, { key: "4", label: `未学完`, }, ]; const onChange = (key: string) => { setTabKey(Number(key)); }; return ( <>
我的课程
必修课:已完成 {stats.required_finished_hour_count} / {stats.required_hour_count}
选修课:已完成 {stats.nun_required_finished_hour_count} / {stats.nun_required_hour_count}
学习时长
今日: {studyTimeFormat(stats.today_learn_duration)[0] !== 0 && ( <> {" "} {studyTimeFormat(stats.today_learn_duration)[0]}{" "} 天 )} {" "} {studyTimeFormat(stats.today_learn_duration)[1]}{" "} 小时 {" "} {studyTimeFormat(stats.today_learn_duration)[2]}{" "} 分钟
累计: {studyTimeFormat(stats.learn_duration)[0] !== 0 && ( <> {" "} {studyTimeFormat(stats.learn_duration || 0)[0]}{" "} 天 )} {studyTimeFormat(stats.learn_duration || 0)[1]} 小时 {studyTimeFormat(stats.learn_duration || 0)[2]} 分钟
{loading && (
)} {coursesList.length === 0 && ( )}
{coursesList.length > 0 && (
{coursesList.map((item: any) => (
{learnCourseRecords[item.id] && ( )} {!learnCourseRecords[item.id] && ( )}
))}
)}
~莫道桑榆晚,为霞尚满天~
); }; export default IndexPage;