import { actionTypes } from 'constants/action-types';
import { request } from 'utils/fetch';
import { api } from 'constants/config';
import { CourseState } from './course.types';

export function getCourse() {
  return async (dispatch) => {
    const result = await request(api.getAllCourse);
    if (result) {
      const payload = {};
      result.map((d) => {
        if (d.key) {
          payload[d.key] = d;
        }
      });
      dispatch({
        type: actionTypes.GLOBAL_COURSE_LIST,
        payload: payload,
      });
    }
  };
}

const initialState = {} as CourseState;

// export const defaultState = {};

export default function introReducer(state = initialState, { type, payload }) {
  switch (type) {
    case actionTypes.GLOBAL_COURSE_LIST: {
      return { ...state, ...payload };
    }
    default:
      return state;
  }
}
