update
This commit is contained in:
@@ -203,7 +203,7 @@ export default function AppointmentForm({ isEdit, id, currentAppointment }: Prop
|
||||
|
||||
}, [hospitalOptions, defaultValues]);
|
||||
|
||||
// Autocomplite drugs
|
||||
// Autocomplete drugs
|
||||
const [drugOptions, setDrugsOptions] = useState([]);
|
||||
const [selectedDrugsOptions, setSelectedDrugsOptions] = useState({});
|
||||
|
||||
@@ -215,31 +215,45 @@ export default function AppointmentForm({ isEdit, id, currentAppointment }: Prop
|
||||
setValue(`medicine.${index}.drug_id`, newValue ? newValue.value : '');
|
||||
};
|
||||
|
||||
// Ambil data dari API dan atur opsi rumah sakit
|
||||
// Ambil data dari API dan atur opsi obat
|
||||
useEffect(() => {
|
||||
axios.get('drugs')
|
||||
.then((response) => {
|
||||
setDrugsOptions(response.data.data);
|
||||
const data = response.data.data;
|
||||
// Set nilai default jika data tidak kosong
|
||||
if (data.length > 0) {
|
||||
// Pilih nilai default pertama
|
||||
setSelectedDrugsOptions({ 0: data[0] });
|
||||
setValue('medicine.0.drug_id', data[0] ? data[0].value : '');
|
||||
}
|
||||
setDrugsOptions(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error fetching drug options:', error);
|
||||
});
|
||||
}, []); // useEffect dijalankan hanya sekali saat komponen dimount
|
||||
|
||||
// Autocomplite unit
|
||||
// Autocomplete unit
|
||||
const [unitOptions, setUnitsOptions] = useState([]);
|
||||
const [selectedUnitsOptions, setSelectedUnitsOptions] = useState({});
|
||||
// Ambil data dari API dan atur opsi rumah sakit
|
||||
|
||||
// Ambil data dari API dan atur opsi satuan
|
||||
useEffect(() => {
|
||||
axios.get('units')
|
||||
.then((response) => {
|
||||
setUnitsOptions(response.data.data);
|
||||
const data = response.data.data;
|
||||
// Set nilai default jika data tidak kosong
|
||||
if (data.length > 0) {
|
||||
// Pilih nilai default pertama
|
||||
setSelectedUnitsOptions({ 0: data[0] });
|
||||
setValue('medicine.0.unit_id', data[0] ? data[0].value : '');
|
||||
}
|
||||
setUnitsOptions(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error fetching unit options:', error);
|
||||
});
|
||||
}, []); // useEffect dijalankan hanya sekali saat komponen dimount
|
||||
|
||||
|
||||
const handleAutocompleteChangeUnit = (newValue, index) => {
|
||||
setSelectedUnitsOptions((prevState) => ({
|
||||
@@ -251,24 +265,20 @@ export default function AppointmentForm({ isEdit, id, currentAppointment }: Prop
|
||||
|
||||
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)
|
||||
defaultValues.medicine.forEach((med, index) => {
|
||||
const selectedDrugId = drugOptions.find((drug) => 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
|
||||
});
|
||||
const selectedUnitId = unitOptions.find((unit) => unit.value === med.unit_id);
|
||||
handleAutocompleteChangeUnit(selectedUnitId, index);
|
||||
|
||||
// Lakukan tindakan lainnya sesuai kebutuhan Anda
|
||||
});
|
||||
} else {
|
||||
console.log('Medicine is empty');
|
||||
}
|
||||
}, [defaultValues.medicine]);
|
||||
|
||||
|
||||
|
||||
const {
|
||||
reset,
|
||||
|
||||
Reference in New Issue
Block a user