diff --git a/public/locale/en.json b/public/locale/en.json index 7e92d927a64..b5f46dd7308 100644 --- a/public/locale/en.json +++ b/public/locale/en.json @@ -1380,6 +1380,7 @@ "upload_headings__patient": "Upload New Patient File", "upload_headings__sample_report": "Upload Sample Report", "upload_headings__supporting_info": "Upload Supporting Info", + "upload_report": "Upload Report", "uploading": "Uploading", "use_existing_abha_address": "Use Existing ABHA Address", "user_deleted_successfuly": "User Deleted Successfuly", diff --git a/src/components/Patient/UpdateStatusDialog.tsx b/src/components/Patient/UpdateStatusDialog.tsx index 9414ba9bb95..e9a767847aa 100644 --- a/src/components/Patient/UpdateStatusDialog.tsx +++ b/src/components/Patient/UpdateStatusDialog.tsx @@ -1,31 +1,28 @@ -import { useEffect, useReducer, useState } from "react"; +import { useEffect, useReducer } from "react"; import { useTranslation } from "react-i18next"; import CareIcon from "@/CAREUI/icons/CareIcon"; -import { Submit } from "@/components/Common/ButtonV2"; +import { Button } from "@/components/ui/button"; + import ConfirmDialog from "@/components/Common/ConfirmDialog"; import { LinearProgressWithLabel } from "@/components/Files/FileUpload"; import CheckBoxFormField from "@/components/Form/FormFields/CheckBoxFormField"; import { SelectFormField } from "@/components/Form/FormFields/SelectFormField"; import TextFormField from "@/components/Form/FormFields/TextFormField"; import { FieldChangeEvent } from "@/components/Form/FormFields/Utils"; -import { - CreateFileResponse, - SampleTestModel, -} from "@/components/Patient/models"; + +import useFileUpload from "@/hooks/useFileUpload"; import { - HEADER_CONTENT_TYPES, SAMPLE_FLOW_RULES, SAMPLE_TEST_RESULT, SAMPLE_TEST_STATUS, } from "@/common/constants"; import * as Notification from "@/Utils/Notifications"; -import routes from "@/Utils/request/api"; -import request from "@/Utils/request/request"; -import uploadFile from "@/Utils/request/uploadFile"; + +import { SampleTestModel } from "./models"; interface Props { sample: SampleTestModel; @@ -34,7 +31,6 @@ interface Props { } const statusChoices = [...SAMPLE_TEST_STATUS]; - const statusFlow = { ...SAMPLE_FLOW_RULES }; const initForm: any = { @@ -60,17 +56,16 @@ const updateStatusReducer = (state = initialState, action: any) => { return state; } }; - const UpdateStatusDialog = (props: Props) => { const { t } = useTranslation(); const { sample, handleOk, handleCancel } = props; const [state, dispatch] = useReducer(updateStatusReducer, initialState); - const [file, setfile] = useState(); - const [contentType, setcontentType] = useState(""); - const [uploadPercent, setUploadPercent] = useState(0); - const [uploadStarted, setUploadStarted] = useState(false); - const [uploadDone, setUploadDone] = useState(false); + const fileUpload = useFileUpload({ + type: "SAMPLE_MANAGEMENT", + allowedExtensions: ["pdf", "jpg", "jpeg", "png"], + allowNameFallback: true, + }); const currentStatus = SAMPLE_TEST_STATUS.find( (i) => i.text === sample.status, ); @@ -104,79 +99,26 @@ const UpdateStatusDialog = (props: Props) => { dispatch({ type: "set_form", form }); }; - const uploadfile = (data: CreateFileResponse) => { - const url = data.signed_url; - const internal_name = data.internal_name; - - const f = file; - if (f === undefined) return; - const newFile = new File([f], `${internal_name}`); - - uploadFile( - url, - newFile, - "PUT", - { - "Content-Type": contentType, - "Content-disposition": "inline", - }, - (xhr: XMLHttpRequest) => { - if (xhr.status >= 200 && xhr.status < 300) { - setUploadStarted(false); - setUploadDone(true); - request(routes.editUpload, { - pathParams: { - id: data.id, - fileType: "SAMPLE_MANAGEMENT", - associatingId: sample.id?.toString() ?? "", - }, - body: { upload_completed: true }, - }); - Notification.Success({ msg: "File Uploaded Successfully" }); + const handleUpload = async () => { + if (fileUpload.files.length > 0) { + if (!fileUpload.fileNames[0]) { + Notification.Error({ + msg: "Please enter a file name before uploading", + }); + return; + } + if (sample.id) { + await fileUpload.handleFileUpload(sample.id); + if (!fileUpload.error) { + return; } else { - setUploadStarted(false); + Notification.Error({ msg: `Upload failed: ${fileUpload.error}` }); } - }, - setUploadPercent, - () => { - setUploadStarted(false); - }, - ); - }; - - const onFileChange = (e: React.ChangeEvent) => { - if (e.target.files == null) { - throw new Error("Error finding e.target.files"); - } - setfile(e.target.files[0]); - const fileName = e.target.files[0].name; - const ext: string = fileName.split(".")[1]; - setcontentType( - HEADER_CONTENT_TYPES[ext as keyof typeof HEADER_CONTENT_TYPES], - ); - return e.target.files[0]; - }; - const handleUpload = async () => { - const f = file; - if (f === undefined) return; - const category = "UNSPECIFIED"; - const name = f.name; - setUploadStarted(true); - setUploadDone(false); - - const { data } = await request(routes.createUpload, { - body: { - original_name: name, - file_type: "SAMPLE_MANAGEMENT", - name: `${sample.patient_name} Sample Report`, - associating_id: sample.id ?? "", - file_category: category, - mime_type: contentType, - }, - }); - - if (data) { - uploadfile(data); + } else { + Notification.Error({ msg: "Sample ID is missing" }); + } + } else { + Notification.Error({ msg: "No file selected for upload" }); } }; @@ -218,32 +160,58 @@ const UpdateStatusDialog = (props: Props) => { onChange={handleChange} /> - Upload Report : + {t("upload_report")}: - {uploadStarted ? ( - - ) : ( -
-