actions.js 1.65 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
zhanghaozhe committed
15 16
    const {switchTab, page, noMore} = myCourses
    if (!switchTab && !noMore) {
zhanghaozhe committed
17 18
        dispatch(getMyCourses({
            page: page + PAGE_INTERVAL,
zhanghaozhe committed
19
            num: NUM_INTERVAL
zhanghaozhe committed
20 21 22 23
        }))
    }
}

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

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

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

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