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

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


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