index.js 7.72 KB
Newer Older
FE committed
1
import React, { Component } from "react"
2
import VList from '@/common/v-list-base'
zhanghaozhe committed
3
import './my-courses.scss'
zhanghaozhe committed
4 5
import { isToday, format } from "date-fns"
import { connect } from "react-redux"
zhanghaozhe committed
6
import { fetchCoursesListIfNeeded, switchTab } from "./actions"
zhanghaozhe committed
7
import InfiniteScroll from 'react-infinite-scroller'
zhanghaozhe committed
8
import { debounce } from 'lodash'
zhanghaozhe committed
9
import { Link } from 'react-router-dom'
zhanghaozhe committed
10
import { Loading } from "@/common";
zhanghaozhe committed
11

zhanghaozhe committed
12 13 14 15 16 17 18 19 20

function getStudyTime(seconds) {
    return {
        hour: Math.floor(seconds / (60 * 60)),
        min: Math.floor(seconds / 60) % 60,
        sec: seconds % 60
    }
}

zhanghaozhe committed
21 22 23 24 25
const AddCourse = React.memo(({addCourseClick}) => (
    <div className='add-course'>
        <button className='add' onClick={addCourseClick}>添加课程+</button>
    </div>
))
zhanghaozhe committed
26

zhanghaozhe committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
function Record({record: {seconds, lesson_name}}) {
    let re = /第[\s\S]+?课/,
        result = ''

    if (lesson_name) {
        let matchResult = re.exec(lesson_name)
        result += (matchResult && matchResult[0]) ? matchResult[0] : ''
    }

    if (seconds) {
        let studyTime = getStudyTime(seconds)
        let hour = studyTime.hour ? String(studyTime.hour).padStart(2, '0') + ':' : '',
            min = studyTime.min ? String(studyTime.min).padStart(2, '0') + ':' : '',
            sec = studyTime.sec ? String(studyTime.sec).padStart(2, '0') : ''

        result += hour + min + sec
    }

    return (
        <span className={'record'}>
            {
                result.length ? `学习到${result}` : null
            }
        </span>
    )
}

zhanghaozhe committed
54

zhanghaozhe committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
const Bottom = React.memo(({item}) => {
    if (item.ago || item.seconds) {
        let date = new Date(item.ago * 1000)
        let time = isToday(date) ? format(date, 'HH时mm分') : format(date, 'MM月DD日')
        return (
            <div className="des">
                <span className='time'>{time}</span>
                <Record record={item}/>
            </div>
        )
    }
    return (
        <button className='start-learn'>开始学习</button>
    )
})
zhanghaozhe committed
70

FE committed
71
class MyCourses extends Component {
zhanghaozhe committed
72

zhanghaozhe committed
73 74 75 76 77
    list

    state = {
        isLoading: true
    }
zhanghaozhe committed
78

zhanghaozhe committed
79
    handleClick = id => {
80
        this.props.history.push(`/play/video?id=${id}`)
81
    }
zhanghaozhe committed
82
    addCourseClick = () => {
83
        this.props.history.push('/classify')
zhanghaozhe committed
84
    }
zhanghaozhe committed
85

zhanghaozhe committed
86
    componentDidMount() {
zhanghaozhe committed
87
        this.props.switchTab(false)
88
        this.props.fetchCoursesListIfNeeded()
zhanghaozhe committed
89
    }
zhanghaozhe committed
90

zhanghaozhe committed
91
    componentWillUnmount() {
zhanghaozhe committed
92 93 94
        this.props.switchTab(true);
    }

zhanghaozhe committed
95
    loadFunc = debounce(() => {
zhanghaozhe committed
96
        if (this.props.courseList.length % 10 === 0) {
97
            this.props.fetchCoursesListIfNeeded()
zhanghaozhe committed
98 99 100
        }
    }, 200)

zhanghaozhe committed
101
    render() {
zhanghaozhe committed
102
        let {courseList, user} = this.props
zhanghaozhe committed
103 104 105

        return <Loading isLoading={this.props.isLoading}>
            {
zhanghaozhe committed
106
                courseList && courseList.length !== 0
zhanghaozhe committed
107 108 109
                    ?
                    <>
                        <div className="my-course-uid">
zhanghaozhe committed
110
                            {`加群请备注您的学号:${!user.hasError && this.props.user.data.uid}`}
zhanghaozhe committed
111 112 113 114 115 116 117 118 119
                        </div>
                        <InfiniteScroll
                            pageStart={0}
                            hasMore={true}
                            loadMore={this.loadFunc}
                            useWindow={false}
                        >
                            <ul ref={el => this.list = el}>
                                {
zhanghaozhe committed
120
                                    courseList.map((item, index) => {
zhanghaozhe committed
121 122 123 124 125

                                        const Info = (
                                            <div className="info">
                                                <div className='title'>{item.course_title}</div>
                                                {
xuzhenghua committed
126 127
                                                    item.is_aist &&
                                                    <div className='contact'>助教微信:{item.assist_weixin}</div>
zhanghaozhe committed
128 129
                                                }
                                                {
xuzhenghua committed
130
                                                    !item.is_aist && item.contact_type == 1 && item.course_qq &&
xuzhenghua committed
131 132 133
                                                    <div className='contact'>QQ群:{item.course_qq}</div>
                                                }
                                                {
xuzhenghua committed
134
                                                    !item.is_aist && item.contact_type == 2 && item.course_qq &&
xuzhenghua committed
135 136 137 138
                                                    <div className='contact'>班主任微信:{item.course_qq}</div>
                                                }

                                                {
zhanghaozhe committed
139 140 141 142 143 144 145
                                                    item.is_aist && item.aist_schedule &&
                                                    <div className="process-status">
                                                        <div className="process-wrapper">
                                                            <div className="process-bar"
                                                                 style={{width: `${parseFloat(item.aist_schedule)}%`}}/>
                                                        </div>
                                                        <div className="process-text">{item.aist_schedule}</div>
zhanghaozhe committed
146
                                                    </div>
zhanghaozhe committed
147 148 149 150 151 152 153 154
                                                }
                                                <Bottom item={item}/>
                                            </div>
                                        )

                                        const status = (
                                            item.is_aist && <span className='status'>返现</span>
                                        )
xuzhenghua committed
155 156 157 158
                                        const courseExpire = (
                                            item.course_expire && item.course_expire!='' &&
                                            <span className='course-expire'>{item.course_expire}</span>
                                        )
zhanghaozhe committed
159 160 161 162 163 164 165
                                        return (
                                            <VList img={item.image_name}
                                                   handleClick={this.handleClick}
                                                   {...item}
                                                   key={index}
                                                   info={Info}
                                                   status={status}
xuzhenghua committed
166
                                                   courseExpire={courseExpire}
xuzhenghua committed
167
                                                   id={item['v_course_id']}
zhanghaozhe committed
168 169 170 171 172 173 174
                                            />
                                        )
                                    })
                                }
                            </ul>
                        </InfiniteScroll>
                        {
zhanghaozhe committed
175
                            courseList.length % 10 !== 0 ?
zhanghaozhe committed
176 177 178 179 180 181 182
                                <AddCourse addCourseClick={this.addCourseClick}/>
                                : null
                        }
                    </>
                    :
                    <div className="empty">
                        <p><i className='iconfont iconfish'/></p>
wangshuo committed
183
                        <p className='empty-prompt'>您还没有课程哦,赶快去选课吧~</p>
zhanghaozhe committed
184 185 186 187 188 189
                        <p>
                            <Link className='select-course' to='/classify'>去选课</Link>
                        </p>
                    </div>
            }
        </Loading>
zhanghaozhe committed
190 191

    }
zhanghaozhe committed
192 193
}

zhanghaozhe committed
194 195 196
export default connect(
    state => ({
        courseList: state.myCourses.courseList,
zhanghaozhe committed
197 198
        user: state.user,
        isLoading: state.myCourses.isLoading
zhanghaozhe committed
199 200 201 202 203
    }),
    {
        fetchCoursesListIfNeeded,
        switchTab
    })(MyCourses)