import { connect } from "react-redux";
import { actionPromise } from "./";
import { backURL } from '../api';
import { Upload, message, Button } from 'antd';
import { UploadOutlined } from '@ant-design/icons';

const actionUploadFile = (file) => {
  const fileJson = JSON.stringify(file[0]);
  const formData = new FormData();
  formData.append("photo", file[0]);
  
   return actionPromise('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;