{"size": 1045, "ext": "js", "max_stars_repo_path": "src/context/StudentContext.js", "max_stars_repo_name": "GabrielHGobi/casdplus-frontend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/context/StudentContext.js", "max_issues_repo_name": "GabrielHGobi/casdplus-frontend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/context/StudentContext.js", "max_forks_repo_name": "GabrielHGobi/casdplus-frontend", "max_forks_repo_forks_event_min_datetime": "2022-03-15T15:36:10.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-17T12:19:32.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 24.880952381, "max_line_length": 55, "alphanum_fraction": 0.6057416268}
import createDataContext from "./createDataContext";
import studentAPI from "../api/CASDplus_student";

const studentReducer = (state, action) => {
  switch (action.type) {
    case "register_info":
      return {
        first_name: action.payload.first_name,
        last_name: action.payload.last_name,
        is_casdinho: action.payload.is_casdinho,
      };
    default:
      return state;
  }
};

const getStudentInfo =
  (dispatch) =>
  async ({ token }) => {
    try {
      const response = await studentAPI.get("", {
        headers: { Authorization: `Bearer ${token}` },
      });
      dispatch({
        type: "register_info",
        payload: {
          first_name: response.data.first_name,
          last_name: response.data.last_name,
          is_casdinho: response.data.is_casdinho,
        },
      });
    } catch (err) {
      console.log(err.message);
    }
  };

export const { Provider, Context } = createDataContext(
  studentReducer,
  { getStudentInfo },
  { first_name: "", last_name: "", is_casdinho: false }
);
