fixing add prescription
This commit is contained in:
@@ -83,7 +83,7 @@ export type Appointment = {
|
||||
doctor_id:number;
|
||||
organizations:Organizations[];
|
||||
specialities:Specialities[];
|
||||
diagonis:string;
|
||||
diagnosis:string;
|
||||
hospital:string;
|
||||
medicine: Medicine[];
|
||||
}
|
||||
|
||||
@@ -124,15 +124,16 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) {
|
||||
organizations: currentAppointment?.organizations || [],
|
||||
specialities: currentAppointment?.specialities || [],
|
||||
diagnosis: currentAppointment?.diagnosis || '',
|
||||
hospital: currentAppointment?.hospital || '',
|
||||
medicine : [{
|
||||
id: 0,
|
||||
drug_id: 0,
|
||||
qty: 0,
|
||||
signa: '',
|
||||
unit_id: 0,
|
||||
note: '', // input to database
|
||||
}],
|
||||
hospital: currentAppointment?.hospital || null,
|
||||
medicine : currentAppointment?.medicine || [
|
||||
{
|
||||
drug_id: 0,
|
||||
qty: 0,
|
||||
signa: '',
|
||||
unit_id: 0,
|
||||
note: '', // input to database
|
||||
}
|
||||
],
|
||||
}),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[currentAppointment]
|
||||
@@ -150,14 +151,17 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) {
|
||||
const [icdOptions, setIcdOptions] = useState([
|
||||
{ value: '-', label: '-' }
|
||||
]);
|
||||
const codes = defaultValues.diagnosis.split(',');
|
||||
const [selectedIcdOptions, setSelectedIcdOptions] = useState([]);
|
||||
useEffect(() => {
|
||||
const selectedCodes = icdOptions.filter((icd) => {
|
||||
// Logika pemilihan sesuai kebutuhan
|
||||
return codes.includes(icd.value);
|
||||
});
|
||||
setSelectedIcdOptions(selectedCodes);
|
||||
}, [icdOptions]);
|
||||
setValue('diagnosis', selectedCodes);
|
||||
}, [icdOptions, defaultValues]);
|
||||
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
// Ambil data dari API dan atur opsi ICD
|
||||
@@ -189,10 +193,14 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) {
|
||||
|
||||
// Set default value saat hospitalOptions berubah
|
||||
useEffect(() => {
|
||||
if (hospitalOptions.length > 0 && !selectedHospitalOption) {
|
||||
setSelectedHospitalOption({value: '-', label: '-'});
|
||||
}
|
||||
}, [hospitalOptions, selectedHospitalOption]);
|
||||
const selectedId = hospitalOptions.find((hospital) => {
|
||||
|
||||
return hospital.value == defaultValues.hospital
|
||||
});
|
||||
setSelectedHospitalOption(selectedId);
|
||||
setValue('hospital', defaultValues.hospital)
|
||||
|
||||
}, [hospitalOptions, defaultValues]);
|
||||
|
||||
// Autocomplite drugs
|
||||
const [drugOptions, setDrugsOptions] = useState([]);
|
||||
@@ -240,6 +248,26 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) {
|
||||
});
|
||||
}, []); // useEffect dijalankan hanya sekali saat komponen dimount
|
||||
|
||||
useEffect(() => {
|
||||
if (defaultValues.medicine.length > 0) {
|
||||
defaultValues.medicine.map((med, index) => {
|
||||
const selectedDrugId = drugOptions.find((drug) => {
|
||||
return drug.value == med.drug_id
|
||||
});
|
||||
handleAutocompleteChange(selectedDrugId,index)
|
||||
|
||||
const selectedUnitId = unitOptions.find((unit) => {
|
||||
return unit.value == med.unit_id
|
||||
});
|
||||
handleAutocompleteChangeUnit(selectedUnitId,index)
|
||||
|
||||
// Contoh: Lakukan tindakan lainnya sesuai kebutuhan Anda
|
||||
});
|
||||
} else {
|
||||
console.log('Medicine is empty');
|
||||
}
|
||||
}, [defaultValues.medicine]);
|
||||
|
||||
|
||||
const {
|
||||
reset,
|
||||
@@ -265,40 +293,40 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) {
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [isEdit, currentAppointment]);
|
||||
|
||||
const onSubmit = async (data: FormValuesProps) => {
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('id', data.id);
|
||||
formData.append('diagnosis', data.diagnosis);
|
||||
formData.append('hospital', data.hospital);
|
||||
|
||||
// Iterasi melalui setiap objek dalam array medicine dan menambahkannya ke FormData
|
||||
data.medicine.forEach((medicineObj, index) => {
|
||||
// Anda dapat menambahkan setiap properti dari objek medicine ke FormData
|
||||
formData.append(`medicine[${index}][drug_id]`, medicineObj.drug_id);
|
||||
formData.append(`medicine[${index}][qty]`, medicineObj.qty);
|
||||
formData.append(`medicine[${index}][unit_id]`, medicineObj.unit_id);
|
||||
formData.append(`medicine[${index}][signa]`, medicineObj.signa);
|
||||
formData.append(`medicine[${index}][note]`, medicineObj.note);
|
||||
});
|
||||
const onSubmit = async (data: FormValuesProps) => {
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('id', data.id);
|
||||
formData.append('diagnosis', data.diagnosis);
|
||||
formData.append('hospital', data.hospital);
|
||||
|
||||
// Iterasi melalui setiap objek dalam array medicine dan menambahkannya ke FormData
|
||||
data.medicine.forEach((medicineObj, index) => {
|
||||
// Anda dapat menambahkan setiap properti dari objek medicine ke FormData
|
||||
formData.append(`medicine[${index}][drug_id]`, medicineObj.drug_id);
|
||||
formData.append(`medicine[${index}][qty]`, medicineObj.qty);
|
||||
formData.append(`medicine[${index}][unit_id]`, medicineObj.unit_id);
|
||||
formData.append(`medicine[${index}][signa]`, medicineObj.signa);
|
||||
formData.append(`medicine[${index}][note]`, medicineObj.note);
|
||||
});
|
||||
|
||||
|
||||
const response = await axios.post('/prescription', formData);
|
||||
reset();
|
||||
enqueueSnackbar('Berhasil menambahkan resep', {
|
||||
variant: 'success',
|
||||
});
|
||||
navigate('/e-prescription/live-chat');
|
||||
} catch (error: any) {
|
||||
console.log(error, 'submit')
|
||||
enqueueSnackbar(error.message ?? 'Failed Processing Request', { variant: 'error' });
|
||||
}
|
||||
const response = await axios.post('/prescription', formData);
|
||||
reset();
|
||||
enqueueSnackbar('Berhasil menambahkan resep', {
|
||||
variant: 'success',
|
||||
});
|
||||
navigate('/e-prescription/live-chat');
|
||||
} catch (error: any) {
|
||||
console.log(error, 'submit')
|
||||
enqueueSnackbar(error.message ?? 'Failed Processing Request', { variant: 'error' });
|
||||
}
|
||||
|
||||
const ascent = document?.querySelector('ascent');
|
||||
if (ascent != null) {
|
||||
ascent.innerHTML = '';
|
||||
}
|
||||
};
|
||||
const ascent = document?.querySelector('ascent');
|
||||
if (ascent != null) {
|
||||
ascent.innerHTML = '';
|
||||
}
|
||||
};
|
||||
return (
|
||||
<FormProvider methods={methods} onSubmit={handleSubmit(onSubmit)}>
|
||||
<Stack spacing={3}>
|
||||
@@ -508,9 +536,6 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) {
|
||||
<Grid item xs={12} columnSpacing={{ xs: 1, sm: 2, md: 3 }}>
|
||||
<Stack direction="row" alignItems="center" sx={{marginBottom: 4}}>
|
||||
<Typography variant='subtitle1' gutterBottom>Obat</Typography>
|
||||
<Button color="inherit" variant="outlined" startIcon={<AddIcon/>} sx={{marginLeft: 'auto'}} onClick={() => append({medicine_name: '', medicine_price: 0, request_log_id: 1 })}>
|
||||
<Typography variant="button" display="block">Obat</Typography>
|
||||
</Button>
|
||||
</Stack>
|
||||
</Grid>
|
||||
|
||||
@@ -582,14 +607,29 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) {
|
||||
fullWidth
|
||||
/>
|
||||
</Grid>
|
||||
|
||||
{index !== fields.length - 1 && (
|
||||
<Grid item xs={1} sx={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
|
||||
<IconButton size='large' color='error' onClick={() => remove(index)}>
|
||||
<RemoveIcon />
|
||||
</IconButton>
|
||||
</Grid>
|
||||
)}
|
||||
{
|
||||
index === 0 ? (
|
||||
<Grid item xs={1} sx={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
|
||||
<IconButton size='large' color='primary' onClick={() => append({medicine_name: '', medicine_price: 0, request_log_id: 1 })}>
|
||||
<AddIcon />
|
||||
</IconButton>
|
||||
</Grid>
|
||||
) : (
|
||||
index == (fields.length-1) ? (
|
||||
<Grid item xs={1} sx={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
|
||||
<IconButton size='large' color='error' onClick={() => remove(index)}>
|
||||
<RemoveIcon />
|
||||
</IconButton>
|
||||
</Grid>
|
||||
) : (
|
||||
<Grid item xs={1} sx={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
|
||||
<IconButton size='large' color='primary' onClick={() => append({medicine_name: '', medicine_price: 0, request_log_id: 1 })}>
|
||||
<AddIcon />
|
||||
</IconButton>
|
||||
</Grid>
|
||||
)
|
||||
)
|
||||
}
|
||||
</Grid>
|
||||
))}
|
||||
</Grid>
|
||||
@@ -601,7 +641,7 @@ export default function AppointmentForm({ isEdit, currentAppointment }: Props) {
|
||||
size="large"
|
||||
loading={isSubmitting}
|
||||
>
|
||||
{!isEdit ? 'Save' : 'Save'}
|
||||
{!isEdit ? 'Save' : 'Update'}
|
||||
</LoadingButton>
|
||||
</Grid>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user