feate:update

This commit is contained in:
TuanVT
2026-05-25 17:42:19 +07:00
parent b0baa509d9
commit 2978cf2db0
9 changed files with 416 additions and 49 deletions
@@ -16,8 +16,6 @@ import TextArea from "@/components/utils/FormCustom/components/TextArea";
import SelectForm from "@/components/utils/FormCustom/components/SelectForm";
import UploadMultipleFile from "@/components/utils/UploadMultipleFile";
import { httpRequest } from "@/services";
import prescriptionServices from "@/services/prescriptionServices";
@@ -31,6 +29,7 @@ import { QUERY_KEY } from "@/constant/config/enum";
import { toastWarn } from "@/common/funcs/toast";
import { PATH } from "@/constant/config";
import { useRouter } from "next/navigation";
import UploadImage from "@/components/utils/UploadImage";
interface PrescriptionItem {
medicineName: string;
@@ -43,7 +42,7 @@ interface PrescriptionItem {
interface PrescriptionForm {
examinationSessionId: string;
notes: string;
prescriptionImg: string;
PrescriptionFile: string;
items: PrescriptionItem[];
}
@@ -62,14 +61,30 @@ const defaultMedicine: PrescriptionItem = {
instructions: "",
};
const convertFileToBase64 = (file: File): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
resolve(reader.result as string);
};
reader.onerror = (error) => {
reject(error);
};
});
};
const CreatePrescription = () => {
const router = useRouter();
const [images, setImages] = useState<UploadImageItem[]>([]);
const [file, setFile] = useState<any>(null);
const [form, setForm] = useState<PrescriptionForm>({
examinationSessionId: "",
notes: "",
prescriptionImg: "",
PrescriptionFile: "",
items: [defaultMedicine],
});
@@ -106,7 +121,7 @@ const CreatePrescription = () => {
const examinationSessionOptions = examinationSessionQuery.data?.items || [];
const createPrescriptionMutation = useMutation({
mutationFn: async () => {
mutationFn: async (body: { PrescriptionFile: string }) => {
return await httpRequest({
showMessageFailed: true,
showMessageSuccess: true,
@@ -114,9 +129,20 @@ const CreatePrescription = () => {
http: prescriptionServices.createPrescription({
examinationSessionId: form.examinationSessionId,
notes: form.notes,
prescriptionImg: form.prescriptionImg,
items: form.items,
PrescriptionFile: body.PrescriptionFile,
items: form.items
.filter((x) => x.medicineName.trim() !== "")
.map((x) => ({
medicineName: x.medicineName,
dosage: x.dosage,
frequency: x.frequency,
duration: x.duration,
instructions: x.instructions,
})),
}),
});
},
@@ -193,11 +219,61 @@ const CreatePrescription = () => {
}));
};
/**
* SUBMIT
*/
const handleSubmit = () => {
createPrescriptionMutation.mutate();
const handleSubmit = async () => {
try {
/**
* VALIDATE
*/
if (!form.examinationSessionId) {
return toastWarn({
msg: "Vui lòng chọn phiên khám",
});
}
if (!file) {
return toastWarn({
msg: "Vui lòng chọn ảnh đơn thuốc",
});
}
/**
* UPLOAD IMAGE
*/
let uploadedImage = "";
if (file) {
const formData = new FormData();
formData.append("file", file);
/**
* API UPLOAD
* SỬA uploadServices theo project của bạn
*/
const uploadRes: any = await httpRequest({
showMessageFailed: true,
http: prescriptionServices.uploadImage(formData),
});
/**
* BACKEND RESPONSE
*/
uploadedImage =
uploadRes?.url || uploadRes?.path || uploadRes?.data?.url || "";
}
/**
* CREATE PRESCRIPTION
*/
createPrescriptionMutation.mutate({
PrescriptionFile: uploadedImage,
});
} catch (error: any) {
toastWarn({
msg: error?.message || "Upload ảnh thất bại",
});
}
};
return (
@@ -207,7 +283,7 @@ const CreatePrescription = () => {
<div className="flex items-center justify-between">
<div>
<h2 className="text-[28px] font-semibold text-[#111827]">
Thêm đơn thuốc
Tạo đơn thuốc
</h2>
<p className="mt-1 text-sm text-[#697586]">
@@ -221,8 +297,8 @@ const CreatePrescription = () => {
disabled={createPrescriptionMutation.isPending}
>
{createPrescriptionMutation.isPending
? "Đang lưu..."
: "Lưu đơn thuốc"}
? "Đang tạo..."
: "Tạo đơn thuốc"}
</CustomButton>
</div>
</div>
@@ -265,22 +341,16 @@ const CreatePrescription = () => {
{/* IMAGE */}
<div className="flex flex-col gap-2">
<UploadMultipleFile
images={images}
setImages={(files: UploadImageItem[]) => {
setImages(files);
const imagePath =
files?.[0]?.path ||
files?.[0]?.url ||
files?.[0]?.img ||
"";
setForm((prev) => ({
...prev,
prescriptionImg: imagePath,
}));
}}
<UploadImage
label={
<span>
Hình nh đơn thuốc <span style={{ color: "red" }}>*</span>
</span>
}
name="PrescriptionFile"
file={file}
setFile={setFile}
path={""}
/>
</div>
@@ -292,6 +362,12 @@ const CreatePrescription = () => {
value={form.notes}
isBlur
max={5000}
onChangeValue={(value) =>
setForm((prev) => ({
...prev,
notes: String(value),
}))
}
/>
</div>
</div>
@@ -142,6 +142,8 @@ const MainPrescription = () => {
<Image
src={item.prescriptionImg}
alt="prescription"
width={40}
height={40}
className="h-10 w-10 rounded object-cover"
/>
) : (
@@ -33,6 +33,7 @@ import type { PrescriptionDetail } from "@/services/prescriptionServices";
========================= */
interface PrescriptionItem {
id: string;
medicineName: string;
dosage: string;
frequency: string;
@@ -58,6 +59,7 @@ interface UploadImageItem {
========================= */
const defaultMedicine: PrescriptionItem = {
id: "",
medicineName: "",
dosage: "",
frequency: "",
@@ -171,6 +173,7 @@ const UpdatePrescription = () => {
items:
data.items && data.items.length > 0
? data.items.map((i) => ({
id: i.id,
medicineName: i.medicineName ?? "",
dosage: i.dosage ?? "",
frequency: i.frequency ?? "",
@@ -224,19 +227,32 @@ const UpdatePrescription = () => {
throw new Error("Không tìm thấy ID đơn thuốc!");
}
const payload = {
examinationSessionId: form.examinationSessionId,
notes: form.notes,
prescriptionImg: form.prescriptionImg,
items: form.items
.filter((item) => item.medicineName.trim() !== "")
.map((item) => ({
...(item.id ? { id: item.id } : {}),
medicineName: item.medicineName,
dosage: item.dosage,
frequency: item.frequency,
duration: item.duration,
instructions: item.instructions,
})),
};
console.log("PAYLOAD UPDATE:", payload);
return await httpRequest({
showMessageFailed: true,
showMessageSuccess: true,
msgSuccess: "Cập nhật đơn thuốc thành công!",
http: prescriptionServices.putPrescription(id, {
// examinationSessionId: form.examinationSessionId,
notes: form.notes,
prescriptionImg: form.prescriptionImg,
items: form.items,
}),
http: prescriptionServices.putPrescription(id, payload),
});
},
@@ -245,8 +261,10 @@ const UpdatePrescription = () => {
},
onError: (error: any) => {
console.log("UPDATE ERROR:", error);
const message =
error?.message || error?.error?.message || "Có lỗi xảy ra";
error?.response?.data?.message || error?.message || "Có lỗi xảy ra";
toastWarn({
msg: message,
@@ -283,6 +301,7 @@ const UpdatePrescription = () => {
items: [
...prev.items,
{
id: "",
medicineName: "",
dosage: "",
frequency: "",