reducers.js 1.05 KB
Newer Older
zhanghaozhe committed
1
import {
zhanghaozhe committed
2 3 4 5 6 7
  RECEIVE_MY_COURSES,
  SWITCH_TAB,
  NOMORE_COURSE,
  START_FETCHING_COURSES,
} from "./actions"
import { SET_CURRENT_USER } from "src/store/userAction"
zhanghaozhe committed
8 9

const initialState = {
zhanghaozhe committed
10 11 12 13 14 15 16
  switchTab: false,
  courseList: [],
  page: 0,
  statusCode: 0,
  msg: "",
  noMore: false,
  isLoading: true,
zhanghaozhe committed
17 18 19
}

export default function myCourses(state = initialState, action) {
zhanghaozhe committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  const { type, payload = null } = action
  switch (type) {
    case RECEIVE_MY_COURSES:
      let { courseList, ...rest } = payload
      return {
        ...state,
        ...rest,
        courseList:
          Object.keys(courseList).length === 0
            ? state.courseList
            : state.courseList.concat(courseList),
        isLoading: false,
      }
    case START_FETCHING_COURSES:
      return { ...state, ...payload }
    case SWITCH_TAB:
      return { ...state, switchTab: payload }
    case NOMORE_COURSE:
      return { ...state, noMore: true }
    case SET_CURRENT_USER:
      return payload.hasError ? initialState : { ...state, page: 0 }
    default:
      return state
  }
}