actions.js 1.57 KB
Newer Older
zhanghaozhe committed
1
import { http } from "src/utils"
zhanghaozhe committed
2

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

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

zhanghaozhe committed
24 25 26 27 28 29
export const getMyCourses = (payload) => (dispatch) => {
  dispatch(startFetchingCourses)
  return http
    .get(`${API.home}/m/my_course/${payload.page}/${payload.num}`)
    .then((res) => {
      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 37 38 39 40 41 42 43
        dispatch(nomoreCourse())
      }
      dispatch(
        receiveMyCourses({
          courseList: data,
          statusCode: code,
          msg: msg,
          page: payload.page,
          num: payload.num,
zhanghaozhe committed
44
        })
zhanghaozhe committed
45 46
      )
    })
zhanghaozhe committed
47 48
}

zhanghaozhe committed
49 50 51 52
export const SWITCH_TAB = "SWITCH_TAB"
export const switchTab = (payload) => ({
  type: SWITCH_TAB,
  payload,
zhanghaozhe committed
53 54
})

zhanghaozhe committed
55
export const NOMORE_COURSE = "NOMORE_COURSES"
56
export const nomoreCourse = (payload) => ({
zhanghaozhe committed
57 58
  type: NOMORE_COURSE,
  payload,
zhanghaozhe committed
59 60
})

zhanghaozhe committed
61
export const START_FETCHING_COURSES = "START_FETCHING_COURSES"
zhanghaozhe committed
62
export const startFetchingCourses = () => {
zhanghaozhe committed
63 64
  return { type: START_FETCHING_COURSES, payload: { isLoading: true } }
}