🎨 style(inputFileComponent): refactor file upload to use a separate function
The file upload functionality has been refactored to use a separate function called uploadFile, which is imported from the API controller. This improves the separation of concerns and makes the code more modular.
This commit is contained in:
parent
5216b519e0
commit
a81a99f1c4
1 changed files with 3 additions and 9 deletions
|
|
@ -4,6 +4,7 @@ import { alertContext } from "../../contexts/alertContext";
|
||||||
import { FileComponentType } from "../../types/components";
|
import { FileComponentType } from "../../types/components";
|
||||||
import { TabsContext } from "../../contexts/tabsContext";
|
import { TabsContext } from "../../contexts/tabsContext";
|
||||||
import { INPUT_STYLE } from "../../constants";
|
import { INPUT_STYLE } from "../../constants";
|
||||||
|
import { uploadFile } from "../../controllers/API";
|
||||||
|
|
||||||
export default function InputFileComponent({
|
export default function InputFileComponent({
|
||||||
value,
|
value,
|
||||||
|
|
@ -53,16 +54,9 @@ export default function InputFileComponent({
|
||||||
|
|
||||||
// Check if the file type is correct
|
// Check if the file type is correct
|
||||||
if (file && checkFileType(file.name)) {
|
if (file && checkFileType(file.name)) {
|
||||||
// Prepare the file for upload
|
|
||||||
const formData = new FormData();
|
|
||||||
formData.append("file", file);
|
|
||||||
|
|
||||||
// Upload the file
|
// Upload the file
|
||||||
fetch(`/upload/${id}`, {
|
uploadFile(file, id)
|
||||||
method: "POST",
|
.then((res) => res.data)
|
||||||
body: formData,
|
|
||||||
})
|
|
||||||
.then((response) => response.json())
|
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
console.log("File uploaded successfully");
|
console.log("File uploaded successfully");
|
||||||
// Get the file name from the response
|
// Get the file name from the response
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue