actions.js 1.68 KB
Newer Older
zhanghaozhe committed
1
import { http } from '@/utils'
zhanghaozhe committed
2

zhanghaozhe committed
3

zhanghaozhe committed
4 5 6 7 8 9 10
export const RECEIVE_MY_COURSES = 'RECEIVE_MY_COURSES'
export const receiveMyCourses = payload => ({
    type: RECEIVE_MY_COURSES,
    payload
})


zhanghaozhe committed
11
const PAGE_INTERVAL = 1
zhanghaozhe committed
12
export const NUM_INTERVAL = 10
zhanghaozhe committed
13 14
export const fetchCoursesListIfNeeded = () => (dispatch, getState) => {
    const myCourses = getState().myCourses
15
    console.log(myCourses);
zhanghaozhe committed
16 17
    const {switchTab, page, noMore} = myCourses
    if (!switchTab && !noMore) {
zhanghaozhe committed
18 19
        dispatch(getMyCourses({
            page: page + PAGE_INTERVAL,
zhanghaozhe committed
20
            num: NUM_INTERVAL
zhanghaozhe committed
21 22 23 24
        }))
    }
}

zhanghaozhe committed
25
export const getMyCourses = payload => dispatch => {
zhanghaozhe committed
26
    dispatch(startFetchingCourses)
zhanghaozhe committed
27
    return http.get(`${API.home}/m/my_course/${payload.page}/${payload.num}`)
zhanghaozhe committed
28
        .then(res => {
zhanghaozhe committed
29
            const {data, code, msg} = res.data
zhanghaozhe committed
30
            if (code == 200 && data.length === 0) {
zhanghaozhe committed
31 32 33
                dispatch(nomoreCourse())
                return
            }
zhanghaozhe committed
34
            if (code == 200 && data.length % 10 !== 0) {
zhanghaozhe committed
35 36
                dispatch(nomoreCourse())
            }
zhanghaozhe committed
37
            dispatch(receiveMyCourses({
zhanghaozhe committed
38 39 40
                courseList: data,
                statusCode: code,
                msg: msg,
zhanghaozhe committed
41 42 43
                page: payload.page,
                num: payload.num
            }))
zhanghaozhe committed
44 45 46
        })
}

zhanghaozhe committed
47 48 49 50
export const SWITCH_TAB = 'SWITCH_TAB'
export const switchTab = payload => ({
    type: SWITCH_TAB,
    payload
zhanghaozhe committed
51 52
})

zhanghaozhe committed
53
export const NOMORE_COURSE = 'NOMORE_COURSES'
54 55 56
export const nomoreCourse = (payload) => ({
    type: NOMORE_COURSE,
    payload
zhanghaozhe committed
57 58 59 60 61 62
})

export const START_FETCHING_COURSES = 'START_FETCHING_COURSES'
export const startFetchingCourses = () => {
    return {type: START_FETCHING_COURSES, payload: {isLoading: true}}
}