Skip to content

Commit

Permalink
fix: fixing problem when adding alternative prescription medicine
Browse files Browse the repository at this point in the history
  • Loading branch information
Aelmeky committed Dec 15, 2023
1 parent d54aac0 commit 92c6dfc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
24 changes: 22 additions & 2 deletions client/src/pages/medicine/AltMedicineCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,45 @@ import {
} from '@mui/material';
import AddShoppingCartIcon from '@mui/icons-material/AddShoppingCart';
import { PHARMACY_BASE_URL } from 'utils/Constants';
import { pharmacyAxios } from '../../utils/AxiosConfig';
import { patientAxios,pharmacyAxios } from '../../utils/AxiosConfig';
import { useUserContext } from 'hooks/useUserContext';
import Swal from 'sweetalert2';


const AltMedicineCard = ({
medicine,
setSelectedMedicine,
handleAddToCart,
medicineIsBeingAddedToCart,
foundInPrescription
}) => {
const { user } = useUserContext();
const userId = user.id;
const userType = user.type;
const [addToCartStatus, setAddToCartStatus] = useState(true);
const [isLoading, setIsLoading] = useState(true);
const [foundInPrescription, setFoundInPrescription] = useState(false);


useEffect(() => {
if (medicine.prescriptionMedicine === true) {
patientAxios
.get(`/patient/${userId}/prescriptions`)
.then((response) => response.data)
.then((data) => {
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].medicines.length; j++) {
if (medicine._id === data[i].medicines[j].medicineId) {
setFoundInPrescription(true);
}
}
}
})
.catch((error) => {
console.error('Error fetching data:', error);
Swal.fire('error', error.message, 'error');
setIsLoading(false);
});
}
pharmacyAxios
.get(`/cart/users/${userId}/medicines/${medicine._id}`)
.then((response) => {
Expand Down
21 changes: 20 additions & 1 deletion client/src/pages/medicine/MedicineCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const MedicineCard = ({
handleDataChange,
addToCartAlert,
errorAddingToCart,

}) => {
const { user } = useUserContext();
const userId = user.id;
Expand All @@ -43,6 +43,25 @@ const MedicineCard = ({
const [foundInPrescription, setFoundInPrescription] = useState(false);

useEffect(() => {
if (medicine.prescriptionMedicine === true) {
patientAxios
.get(`/patient/${userId}/prescriptions`)
.then((response) => response.data)
.then((data) => {
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].medicines.length; j++) {
if (medicine._id === data[i].medicines[j].medicineId) {
setFoundInPrescription(true);
}
}
}
})
.catch((error) => {
console.error('Error fetching data:', error);
Swal.fire('error', error.message, 'error');
setIsLoading(false);
});
}
pharmacyAxios
.get(`/cart/users/${userId}/medicines/${medicine._id}`)
.then((response) => {
Expand Down

0 comments on commit 92c6dfc

Please sign in to comment.