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

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

  return actionImg(name, 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 actionUploadImg;
