-
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.
* feat: UploadFiles * fix: File type and INewFile * style: Format file and add missing comma * fix: Dialog show on click Still in progress, i have this response msg : "There was an error in the upload." success : false * refactor: file type * feat: Upload Files * fix: multiple upload * fix: lint * fix:lint * fix: fragment and button disable * feat(ui): Animate loader --------- Co-authored-by: Pedro Andrés Chaparro Quintero <[email protected]> Co-authored-by: Pedro Andrés Chaparro Quintero <[email protected]>
- Loading branch information
1 parent
66a5e23
commit c5138ac
Showing
9 changed files
with
206 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { faker } from "@faker-js/faker"; | ||
|
||
describe("File Upload Tests", () => { | ||
const username = faker.internet.userName(); | ||
const password = faker.internet.password({ length: 8 }); | ||
|
||
it("Uploads a file", () => { | ||
cy.visit("/register"); | ||
cy.get("input[name=username]").type(username); | ||
cy.get("input[name=password]").type(password); | ||
cy.get("input[name=confirmPassword]").type(password); | ||
cy.get("button").contains("Submit").click(); | ||
cy.url().should("include", "/files"); | ||
|
||
// Open the modal to upload a file | ||
cy.get("button").contains("Upload File").click(); | ||
|
||
cy.get("div[role=dialog]").should("be.visible"); | ||
cy.get("div[role=dialog]").should("contain", "Select the files to upload"); | ||
|
||
// Select the file input element | ||
cy.get("input[type=file]").selectFile({ | ||
contents: Cypress.Buffer.from("file contents"), | ||
fileName: "file.txt", | ||
lastModified: Date.now() | ||
}); | ||
|
||
// Submit the form | ||
cy.get("button").contains("Upload files").click(); | ||
cy.get("div[role=dialog]").should("not.exist"); | ||
// Verify that the file is listed in the table | ||
cy.get("div").should("contain", "file.txt"); | ||
}); | ||
}); |
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
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,98 @@ | ||
import { useContext, useRef, useState } from "react"; | ||
import toast from "react-hot-toast"; | ||
import { uploadfileService } from "../../services/files/upload-file.service"; | ||
import { Dialog } from "../../components/Dialog"; | ||
import { Loader } from "lucide-react"; | ||
import { | ||
FilesDialogsContext, | ||
AuthContext, | ||
AVAILABLE_DIALOGS, | ||
FilesContext | ||
} from "../../context/index"; | ||
|
||
interface INewFile { | ||
isFile: boolean; | ||
isReady: boolean; | ||
name: string; | ||
size: number; | ||
uuid: string; | ||
} | ||
|
||
export const UploadFileDialog = () => { | ||
// Dialog state | ||
const { closeDialog, dialogsVisibilityState } = | ||
useContext(FilesDialogsContext); | ||
const [isUploading, setIsUploading] = useState(false); | ||
const isOpen = dialogsVisibilityState[AVAILABLE_DIALOGS.UPLOAD_FILE]; | ||
const { addFiles, currentDirectory } = useContext(FilesContext); | ||
const { session } = useContext(AuthContext); | ||
|
||
const files: INewFile[] = []; | ||
const fileInputRef = useRef<HTMLInputElement>(null); | ||
|
||
const uploadFile = async () => { | ||
setIsUploading(true); | ||
const fileInput = fileInputRef.current; | ||
if (fileInput?.files?.length) { | ||
const fileList = fileInput.files; | ||
for (let i = 0; i < fileList.length; i++) { | ||
const file = fileList[i]; | ||
|
||
const token = session!.token; | ||
const uploadRequest = { | ||
fileContent: file, | ||
fileName: file.name, | ||
location: currentDirectory || "", | ||
token | ||
}; | ||
const response = await uploadfileService(uploadRequest); | ||
|
||
if (response.success) { | ||
const newFile: INewFile = { | ||
isFile: true, | ||
isReady: true, | ||
name: file.name, | ||
size: 0, | ||
uuid: response.fileUUID! | ||
}; | ||
toast.success(`The file ${file.name} has been uploaded successfully`); | ||
|
||
files.push(newFile); | ||
} else { | ||
toast.error(response.msg); | ||
} | ||
} | ||
} | ||
addFilesUI(); | ||
setIsUploading(false); | ||
}; | ||
|
||
const addFilesUI = () => { | ||
addFiles(files); | ||
closeDialog(AVAILABLE_DIALOGS.UPLOAD_FILE); | ||
}; | ||
|
||
return ( | ||
<Dialog | ||
isOpen={isOpen} | ||
onClose={() => closeDialog(AVAILABLE_DIALOGS.UPLOAD_FILE)} | ||
title="Select the files to upload" | ||
> | ||
<input | ||
type="file" | ||
ref={fileInputRef} | ||
multiple | ||
aria-label="Select the files" | ||
className="w-full rounded-lg border p-2" | ||
/> | ||
<button | ||
className="hover-bg-blue-700 mt-4 flex rounded-full bg-blue-600 px-4 py-2 text-white" | ||
onClick={uploadFile} | ||
disabled={isUploading} | ||
> | ||
{isUploading && <Loader className="mr-2 animate-spin" />} | ||
Upload files | ||
</button> | ||
</Dialog> | ||
); | ||
}; |
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,55 @@ | ||
import axios from "axios"; | ||
import { AxiosError } from "axios"; | ||
import { ENVIRONMENT } from "../../config/environment"; | ||
|
||
type UploadfileRequest = { | ||
fileContent: File; | ||
fileName: string; | ||
location: string; | ||
token: string; | ||
}; | ||
|
||
type UploadfileResponse = { | ||
msg: string; | ||
success: boolean; | ||
fileUUID?: string; | ||
}; | ||
|
||
export const uploadfileService = async ( | ||
req: UploadfileRequest | ||
): Promise<UploadfileResponse> => { | ||
try { | ||
const formData = new FormData(); | ||
formData.append("file", req.fileContent); | ||
formData.append("fileName", req.fileName); | ||
formData.append("location", req.location); | ||
|
||
const response = await axios.post( | ||
`${ENVIRONMENT.PROXY_BASE_URL}/file/upload`, | ||
formData, | ||
{ | ||
headers: { | ||
"Authorization": `Bearer ${req.token}`, | ||
"Content-Type": "multipart/form-data" | ||
} | ||
} | ||
); | ||
|
||
return { | ||
success: true, | ||
msg: "Upload successful", | ||
fileUUID: response.data.fileUUID | ||
}; | ||
} catch (error) { | ||
let errorMsg = "There was an error in the upload."; | ||
|
||
if (error instanceof AxiosError) { | ||
errorMsg = error.response?.data.msg || errorMsg; | ||
} | ||
|
||
return { | ||
msg: errorMsg, | ||
success: false | ||
}; | ||
} | ||
}; |