首页课程展示数据渲染

This commit is contained in:
禺狨 2023-03-24 14:13:37 +08:00
parent 95732983f7
commit 084a0652c6
2 changed files with 58 additions and 11 deletions

View File

@ -1,5 +1,5 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Tabs } from "antd"; import { Row, Col, Empty, Spin, Tabs } from "antd";
import type { TabsProps } from "antd"; import type { TabsProps } from "antd";
import { user } from "../../api/index"; import { user } from "../../api/index";
import styles from "./index.module.scss"; import styles from "./index.module.scss";
@ -10,6 +10,7 @@ import studyTime from "../../assets/images/commen/icon-studytime.png";
import { studyTimeFormat } from "../../utils/index"; import { studyTimeFormat } from "../../utils/index";
const IndexPage = () => { const IndexPage = () => {
const [loading, setLoading] = useState<boolean>(false);
const [tabKey, setTabKey] = useState(0); const [tabKey, setTabKey] = useState(0);
const [coursesList, setCoursesList] = useState<any>([]); const [coursesList, setCoursesList] = useState<any>([]);
const [learnCourseRecords, setLearnCourseRecords] = useState<any>({}); const [learnCourseRecords, setLearnCourseRecords] = useState<any>({});
@ -24,9 +25,11 @@ const IndexPage = () => {
}, [tabKey]); }, [tabKey]);
const getData = () => { const getData = () => {
setLoading(true);
user.courses(departments[0].id).then((res: any) => { user.courses(departments[0].id).then((res: any) => {
const records = res.data.learn_course_records;
setStats(res.data.stats); setStats(res.data.stats);
setLearnCourseRecords(res.data.learn_course_records); setLearnCourseRecords(records);
if (tabKey === 0) { if (tabKey === 0) {
setCoursesList(res.data.courses); setCoursesList(res.data.courses);
} else if (tabKey === 1) { } else if (tabKey === 1) {
@ -45,7 +48,27 @@ const IndexPage = () => {
} }
}); });
setCoursesList(arr); 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);
}); });
}; };
@ -129,7 +152,10 @@ const IndexPage = () => {
{studyTimeFormat(stats.learn_duration)[0] !== 0 && ( {studyTimeFormat(stats.learn_duration)[0] !== 0 && (
<> <>
<strong> {studyTimeFormat(stats.learn_duration)[0]} </strong> <strong>
{" "}
{studyTimeFormat(stats.learn_duration || 0)[0]}{" "}
</strong>
</> </>
)} )}
@ -144,17 +170,38 @@ const IndexPage = () => {
<div className={styles["tabs"]}> <div className={styles["tabs"]}>
<Tabs defaultActiveKey="0" items={items} onChange={onChange} /> <Tabs defaultActiveKey="0" items={items} onChange={onChange} />
</div> </div>
{coursesList.length === 0 && <span></span>} <Row style={{ width: 1200, margin: "0 auto", paddingTop: 14 }}>
{loading && (
<div className="float-left d-j-flex mt-50">
<Spin size="large" />
</div>
)}
{coursesList.length === 0 && (
<Col span={24}>
<Empty description="暂无课程" />
</Col>
)}
</Row>
{coursesList.length > 0 && ( {coursesList.length > 0 && (
<div className={styles["courses-list"]}> <div className={styles["courses-list"]}>
{coursesList.map((item: any) => ( {coursesList.map((item: any) => (
<div key={item.id}> <div key={item.id}>
<CoursesModel {learnCourseRecords[item.id] && (
title={item.title} <CoursesModel
thumb={item.thumb} title={item.title}
isRequired={item.is_required} thumb={item.thumb}
progress={30} isRequired={item.is_required}
></CoursesModel> progress={learnCourseRecords[item.id].progress}
></CoursesModel>
)}
{!learnCourseRecords[item.id] && (
<CoursesModel
title={item.title}
thumb={item.thumb}
isRequired={item.is_required}
progress={0}
></CoursesModel>
)}
</div> </div>
))} ))}
</div> </div>

View File

@ -17,7 +17,7 @@ export function dateFormat(dateStr: string) {
} }
export function studyTimeFormat(dateStr: number) { export function studyTimeFormat(dateStr: number) {
var d = moment.duration(dateStr, "seconds"); var d = moment.duration(dateStr/1000, "seconds");
let value = []; let value = [];
value.push(Math.floor(d.asDays())); value.push(Math.floor(d.asDays()));
value.push(d.hours()); value.push(d.hours());