import axios from 'axios';
import * as ACTIONS from './constants';

export function addStudent(student) {
  const nextId = Math.floor(Math.random() * 100);
  const data = {
    _id: nextId,
    lesson: 0,
    level: 0,
    interview: 'not set',
    decision: 'pending',
    contacted: false,
    name: student.name,
    email: student.email
  };

  axios.post(`api/student`, data)
  .then(res => console.log(res))
  .then(err => console.log(err));
  return {
    type: ACTIONS.ADD_STUDENT,
    payload: data
  };
}

export function selectStudent(student) {
    return {
      type: ACTIONS.SELECT_STUDENT,
      payload: student
    }
}

export function updateStudent(student) {
  axios.put(`api/student`, student);
  return {
    type: ACTIONS.UPDATE_STUDENT,
    payload: data
  };
}

export function fetchStudents() {
  const token = localStorage.getItem('id_token') || null;

  const config = {
    headers: { Authorization: token }
  };

  return dispatch => {
    dispatch(requestFetchStudents());
    return axios.get(`api/students`, config)
    .then(res => dispatch(fetchStudentsSuccess(res)))
    .catch(err => dispatch(fetchStudentFail(err)));
  };
}

function requestFetchStudents() {

  return {
    type: ACTIONS.FETCH_STUDENTS_REQUEST,
  };
}

function fetchStudentsSuccess(data) {
  return {
    type: ACTIONS.FETCH_STUDENTS_SUCCESS,
    data
  };
}

function fetchStudentFail(message) {
  return {
    type: ACTIONS.FETCH_STUDENTS_FAILURE,
    message
  };
}

@@ -550,18 +550,16 @@
 dent) %7B%0A
-  
   return
@@ -561,18 +561,16 @@
 eturn %7B%0A
-  
     type
@@ -599,18 +599,16 @@
 NT,%0A    
-  
 payload:
@@ -618,19 +618,18 @@
 udent%0A  
-  
 %7D
+;
 %0A%7D%0A%0Aexpo
@@ -764,388 +764,15 @@
 ad: 
-data%0A  %7D;%0A%7D%0A%0Aexport function fetchStudents() %7B%0A  const token = localStorage.getItem('id_token') %7C%7C null;%0A%0A  const config = %7B%0A    headers: %7B Authorization: token %7D%0A  %7D;%0A%0A  return dispatch =%3E %7B%0A    dispatch(requestFetchStudents());%0A    return axios.get(%60api/students%60, config)%0A    .then(res =%3E dispatch(fetchStudentsSuccess(res)))%0A    .catch(err =%3E dispatch(fetchStudentFail(err)));
+student
 %0A  %7D
@@ -810,17 +810,16 @@
 nts() %7B%0A
-%0A
   return
@@ -1069,16 +1069,16 @@
 AILURE,%0A
-
     mess
@@ -1080,16 +1080,392 @@
  message%0A  %7D;%0A%7D%0A
+%0Aexport function fetchStudents() %7B%0A  const token = localStorage.getItem('id_token') %7C%7C null;%0A%0A  const config = %7B%0A    headers: %7B Authorization: token %7D%0A  %7D;%0A%0A  return dispatch =%3E %7B%0A    dispatch(requestFetchStudents());%0A    return axios.get(%60api/students%60, config)%0A    .then(res =%3E dispatch(fetchStudentsSuccess(res)))%0A    .catch(err =%3E dispatch(fetchStudentFail(err)));%0A  %7D;%0A%7D%0A

clean up actions file