import {
    RECEIVE_MY_COURSES,
    SWITCH_TAB,
    NOMORE_COURSE,
    START_FETCHING_COURSES
} from './actions'

const initialState = {
    switchTab: false,
    courseList: [],
    page: 0,
    statusCode: 0,
    msg: '',
    noMore: false,
    isLoading: true
}


export default function myCourses(state = initialState, action) {
    switch (action.type) {
        case RECEIVE_MY_COURSES:
            let {courseList, ...rest} = action.payload
            return {
                ...state, ...rest,
                courseList: Object.keys(courseList).length === 0 ? state.courseList : state.courseList.concat(courseList),
                isLoading: false
            }
        case START_FETCHING_COURSES:
            return {...state, ...action.payload}
        case SWITCH_TAB:
            return {...state, switchTab: action.payload}
        case NOMORE_COURSE:
            return {...state, noMore: true}
        default:
            return state
    }
}