import { actionUploadFirstImg } from './';
import { backURL } from '../api';

const actionUploadFile = (file) => {
  const formData = new FormData();
  formData.append('photo', file[0]);

  return actionUploadFirstImg('upLoad', fetch(`${backURL}/upload`, {
    method: 'POST',
    headers: localStorage.authToken ? { Authorization: 'Bearer ' + localStorage.authToken } : {},
    body: formData
  })
    .then(res => {
      console.log('file', res);
      return res.json();
    }).then(data => {
      if (data.errors && !data._id) { throw new Error(JSON.stringify(data.errors)); };
      return data;
    })
  );
};

export default actionUploadFile;
