import * as React from 'react';
import Request from '../utils/request';
export const useUpload = () => {
  const [state, setState] = React.useState({
    loading: false,
  });
  const request = (data: FormData) => {
    setState((preState) => ({ ...preState, loading: true }));
    return new Promise((resolve, reject) => {
      Request.instance
        .post('/v1/upload', data, {
          headers: {
            'Content-type': 'multipart/form-data',
          },
        })
        .then((data) => {
          resolve(data);
        })
        .catch((error) => {
          reject(error);
        })
        .finally(() => {
          setState((preState) => ({ ...preState, loading: false }));
        });
    });
  };
  return { state, request } as const;
};
