index.js 7.96 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

xuzhenghua committed
79 80 81 82 83 84 85 86
    handleClick = (id, item) => {
        const {history} = this.props
        const {mode, course_id} = item
        if(mode && mode == 6){
            history.push(`/python?id=${course_id}`)
            return
        }
        history.push(`/play/video?id=${id}`)
87
    }
zhanghaozhe committed
88
    addCourseClick = () => {
89
        this.props.history.push('/classify')
zhanghaozhe committed
90
    }
zhanghaozhe committed
91

zhanghaozhe committed
92
    componentDidMount() {
zhanghaozhe committed
93
        this.props.switchTab(false)
94
        this.props.fetchCoursesListIfNeeded()
zhanghaozhe committed
95
    }
zhanghaozhe committed
96

zhanghaozhe committed
97
    componentWillUnmount() {
zhanghaozhe committed
98 99 100
        this.props.switchTab(true);
    }

zhanghaozhe committed
101
    loadFunc = debounce(() => {
zhanghaozhe committed
102
        if (this.props.courseList.length % 10 === 0) {
103
            this.props.fetchCoursesListIfNeeded()
zhanghaozhe committed
104 105 106
        }
    }, 200)

zhanghaozhe committed
107
    render() {
zhanghaozhe committed
108
        let {courseList, user} = this.props
zhanghaozhe committed
109 110 111

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

                                        const Info = (
                                            <div className="info">
                                                <div className='title'>{item.course_title}</div>
                                                {
xuzhenghua committed
132 133
                                                    item.is_aist &&
                                                    <div className='contact'>助教微信:{item.assist_weixin}</div>
zhanghaozhe committed
134 135
                                                }
                                                {
xuzhenghua committed
136
                                                    !item.is_aist && item.contact_type == 1 && item.course_qq &&
xuzhenghua committed
137 138 139
                                                    <div className='contact'>QQ群:{item.course_qq}</div>
                                                }
                                                {
xuzhenghua committed
140
                                                    !item.is_aist && item.contact_type == 2 && item.course_qq &&
xuzhenghua committed
141 142 143 144
                                                    <div className='contact'>班主任微信:{item.course_qq}</div>
                                                }

                                                {
zhanghaozhe committed
145 146 147 148 149 150 151
                                                    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
152
                                                    </div>
zhanghaozhe committed
153 154 155 156 157 158 159 160
                                                }
                                                <Bottom item={item}/>
                                            </div>
                                        )

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

    }
zhanghaozhe committed
199 200
}

zhanghaozhe committed
201 202 203
export default connect(
    state => ({
        courseList: state.myCourses.courseList,
zhanghaozhe committed
204 205
        user: state.user,
        isLoading: state.myCourses.isLoading
zhanghaozhe committed
206 207 208 209
    }),
    {
        fetchCoursesListIfNeeded,
        switchTab
xuzhenghua committed
210
    })(MyCourses)