actionUploadFile.js 684 B

123456789101112131415161718192021
  1. import { backendURL } from "../helpers";
  2. import { actionPromise } from "../reducers";
  3. export const actionUploadFile = (file) => {
  4. const fd = new FormData();
  5. fd.append("photo", file);
  6. return actionPromise(
  7. "uploadFile",
  8. fetch(`${backendURL}/upload/`, {
  9. method: "POST",
  10. headers: localStorage.authToken ? { Authorization: "Bearer " + localStorage.authToken } : {},
  11. body: fd,
  12. })
  13. .then((res) => res.json())
  14. .then((data) => {
  15. if (data.errors) {
  16. throw new Error(JSON.stringify(data.errors));
  17. } else return data.data;
  18. })
  19. );
  20. };