forked from ohcnetwork/care_fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replaced all axios with native fetch and XMLHttpRequest (ohcnetwork#7368
) * replaced all axios with native fetch and XMLHttpRequest * created a separate util for uploadFile function using XMLHttpRequest * created a separate reusable function to handle uploadFilePercentage
- Loading branch information
Showing
7 changed files
with
170 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Dispatch, SetStateAction } from "react"; | ||
import { handleUploadPercentage } from "./utils"; | ||
|
||
const uploadFile = ( | ||
url: string, | ||
file: File | FormData, | ||
reqMethod: string, | ||
headers: object, | ||
onLoad: (xhr: XMLHttpRequest) => void, | ||
setUploadPercent: Dispatch<SetStateAction<number>> | null, | ||
onError: () => void | ||
) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open(reqMethod, url); | ||
|
||
Object.entries(headers).forEach(([key, value]) => { | ||
xhr.setRequestHeader(key, value); | ||
}); | ||
|
||
xhr.onload = () => { | ||
onLoad(xhr); | ||
}; | ||
|
||
if (setUploadPercent != null) { | ||
xhr.upload.onprogress = (event: ProgressEvent) => { | ||
handleUploadPercentage(event, setUploadPercent); | ||
}; | ||
} | ||
|
||
xhr.onerror = () => { | ||
onError(); | ||
}; | ||
xhr.send(file); | ||
}; | ||
|
||
export default uploadFile; |
Oops, something went wrong.