Skip to content

Commit

Permalink
ref: replace api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
YehiaFarghaly committed Dec 27, 2023
1 parent ac3be8f commit 76e50c8
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 103 deletions.
14 changes: 13 additions & 1 deletion client/src/api/DoctorAPI.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clinicAxios, communicationAxios } from 'utils/AxiosConfig';
import { clinicAxios, communicationAxios, patientAxios } from 'utils/AxiosConfig';
import {
DOCTOR_TYPE_ENUM,
PHARMACIST_TYPE_ENUM,
Expand Down Expand Up @@ -82,4 +82,16 @@ export const addDoctorChat = async (doctor) => {
],
},
});
};

export const updatePrescription = async (selectedEditPrescription) => {
const response = await patientAxios.patch('/prescriptions/' + selectedEditPrescription._id, {
prescription: selectedEditPrescription,
});
return response.data;
};

export const createPrescription = async (prescription) => {
const response = await patientAxios.post('/prescriptions', { prescription });
return response.data;
};
5 changes: 5 additions & 0 deletions client/src/api/PatientAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ export const getPatientDiscount = async (user) => {
export const updateHealthPackageStatus = async (user, subscribedPackage) => {
const response = await patientAxios.patch(`patient/${user.id}/health-packages/${subscribedPackage.packageId}`);
return response.data;
};

export const getPatientPrescription = async (patientID) => {
const response = await patientAxios.get(`patient/${patientID}/prescriptions`);
return response.data;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Button, Card, CardContent, Grid, Stack, Typography } from '@mui/materia
import { useUserContext } from 'hooks/useUserContext';
// project imports
import AnimateButton from 'ui-component/extended/AnimateButton';
import { patientAxios } from 'utils/AxiosConfig';
import { HEALTH_PACKAGE_STATUS } from 'utils/Constants';
import { useNavigate } from 'react-router-dom';
import { getPatientHealthPackage } from 'api/PatientAPI';
// styles
const CardStyle = styled(Card)(({ theme }) => ({
background: theme.palette.warning.light,
Expand Down Expand Up @@ -49,7 +49,7 @@ const UpgradePlanCard = () => {
const formattedRenewalDate = renewalDate.toLocaleString('en-US', options);
const navigate = useNavigate();
useEffect(() => {
patientAxios.get(`/patient/${user.id}/health-packages`).then((response) => {
getPatientHealthPackage(user.id).then((response) => {
setHealthPackages(response.data.healthPackages);
}).catch((err) => {
console.log(err);
Expand Down
17 changes: 7 additions & 10 deletions client/src/pages/prescriptions/PrescriptionItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
IconButton,
Tooltip,
} from '@mui/material';

import { getDoctor } from 'api/DoctorAPI';
import { Edit as EditIcon } from '@mui/icons-material';
import FileDownloadIcon from '@mui/icons-material/FileDownload';
import CheckIcon from '@mui/icons-material/Check';
Expand All @@ -16,7 +16,6 @@ import dayjs from 'dayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import prescrptionImage from '../utilities/prescription.png';
import { clinicAxios } from '../../utils/AxiosConfig';
import { useUserContext } from 'hooks/useUserContext';
import { PATIENT_BASE_URL } from 'utils/Constants';

Expand All @@ -29,20 +28,18 @@ const PrescriptionItem = ({
const [doctor, setDoctor] = useState({});
const [Loading, setLoading] = useState(true);
useEffect(() => {
try {
const getDoctor = () => {
clinicAxios
.get(`doctor/${prescription.doctorId}`)
try {
const fetchDoctor = () => {
getDoctor(prescription.doctorId)
.then((responseClinic) => {
setDoctor(responseClinic.data.doctor);
setDoctor(responseClinic.doctor);
setLoading(false);
})
.catch((err) => {
console.log(err);
.catch(() => {
setLoading(false);
});
};
getDoctor();
fetchDoctor();
} catch (err) {
console.log(err);
}
Expand Down
171 changes: 81 additions & 90 deletions client/src/pages/prescriptions/Prescriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import PrescriptionsList from './PrescriptionsList';
import EditPrescription from './EditPrescription';
import AddPrescription from './AddPrescription';
import MainCard from '../../ui-component/cards/MainCard';
import { patientAxios } from '../../utils/AxiosConfig';
import { updatePrescription, createPrescription } from 'api/DoctorAPI';
import { getPatientPrescription, getPatient } from 'api/PatientAPI';
import PrescriptionDetails from './PrescriptionDetails';
import { useUserContext } from 'hooks/useUserContext';
import { useFilter } from 'contexts/FilterContext';
Expand All @@ -21,7 +22,6 @@ import AddIcon from '@mui/icons-material/Add';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import { useLocation, useNavigate } from 'react-router-dom';


const Prescriptions = () => {
const navigate = useNavigate();
const location = useLocation();
Expand All @@ -46,27 +46,25 @@ const Prescriptions = () => {
const [description, setDescription] = useState('');
const [patientName, setPatientName] = useState('');
let selectedPatient = '';
if(location.state){
if (location.state) {
selectedPatient = location.state.selectedPatient;
}


useEffect(() => {
const getPrescriptions = async () => {
try {
const patientResponses = await patientAxios.get(
`patient/${patientID}/prescriptions`,
);
let patientResponses = getPatientPrescription(patientID);
if (singlePatientPrescriptions) {
const filteredPrescriptions = patientResponses.data.filter(
(prescription) => prescription.doctorId === user.id,
);
patientResponses.data = filteredPrescriptions;
patientResponses = filteredPrescriptions;
}
setPrescriptions(patientResponses.data);
setOriginalPrescritpions(patientResponses.data);
for (let i = 0; i < patientResponses.data.length; i++) {
const patientResponse = patientResponses.data[i];
setPrescriptions(patientResponses);
setOriginalPrescritpions(patientResponses);
for (let i = 0; i < patientResponses.length; i++) {
const patientResponse = patientResponses[i];
doctors.push(patientResponse.doctorName);
}
updateFilter([
Expand Down Expand Up @@ -94,8 +92,8 @@ const Prescriptions = () => {
}, [prescriptions.length]);

useEffect(() => {
patientAxios.get(`/patients/${patientID}`).then((response) => {
setPatientName(response.data.patient.name);
getPatient(patientID).then((response) => {
setPatientName(response.patient.name);
}
);
}, []);
Expand Down Expand Up @@ -135,8 +133,6 @@ const Prescriptions = () => {
};

const handleSelectingPrescription = (prescription, doctor) => {
console.log('prescription 000 === ', prescription);
console.log('doctor 0000 === ', doctor);
setSelectedPrescription(prescription);
setPrescriptionDoctor(doctor);
};
Expand All @@ -149,26 +145,22 @@ const Prescriptions = () => {

const handleSaveEdit = (e) => {
e.preventDefault();
patientAxios
.patch('/prescriptions/' + selectedEditPrescription._id, {
prescription: selectedEditPrescription,
})
.then((response) => {
const updatedPrescription = response.data;
const updatedPrescriptions = prescriptions.map((prescription) => {
if (prescription._id === updatedPrescription._id) {
return updatedPrescription;
}
return prescription;
});
setIsEditDialogOpen(false);
setTimeout(() => {
setPrescriptions(updatedPrescriptions);
setOriginalPrescritpions(updatedPrescriptions);
setSelectedEditPrescription(null);
setEditErrorMessage('');
}, 1000);
})
updatePrescription(selectedEditPrescription).then((response) => {
const updatedPrescription = response;
const updatedPrescriptions = prescriptions.map((prescription) => {
if (prescription._id === updatedPrescription._id) {
return updatedPrescription;
}
return prescription;
});
setIsEditDialogOpen(false);
setTimeout(() => {
setPrescriptions(updatedPrescriptions);
setOriginalPrescritpions(updatedPrescriptions);
setSelectedEditPrescription(null);
setEditErrorMessage('');
}, 1000);
})
.catch((err) => {
console.log(err);
setEditErrorMessage('Error updating prescription');
Expand Down Expand Up @@ -197,8 +189,7 @@ const Prescriptions = () => {
medicines: [],
price: 0,
};
patientAxios
.post('/prescriptions', { prescription })
createPrescription(prescription)
.then(() => {
setPrescriptions((prev) => [...prev, prescription]);
setIsAddDialogOpen(false);
Expand All @@ -213,63 +204,63 @@ const Prescriptions = () => {
<Loader />
) : (
<>
{(user.type === DOCTOR_TYPE_ENUM && patientID) && (
<Button variant="outlined" startIcon={<ArrowBackIcon />} color='secondary' onClick={() => { navigate('/doctor/pages/my-patients', { state: { selectedPatient } }); }}
sx={{ mb: 1.5 }}
>
Back to my patients
</Button>
)
}
<MainCard title={`Mr/Mrs ${patientName} Prescriptions`}>
<PrescriptionsList
prescriptions={prescriptions}
handleSelectingPrescription={handleSelectingPrescription}
handleEditButtonClick={handleEditButtonClick}
/>
{(user.type === DOCTOR_TYPE_ENUM && patientID) && (
<Button variant="outlined" startIcon={<ArrowBackIcon />} color='secondary' onClick={() => { navigate('/doctor/pages/my-patients', { state: { selectedPatient } }); }}
sx={{ mb: 1.5 }}
>
Back to my patients
</Button>
)
}
<MainCard title={`Mr/Mrs ${patientName} Prescriptions`}>
<PrescriptionsList
prescriptions={prescriptions}
handleSelectingPrescription={handleSelectingPrescription}
handleEditButtonClick={handleEditButtonClick}
/>

<PrescriptionDetails
selectedPrescription={selectedPrescription}
setSelectedPrescription={setSelectedPrescription}
prescriptionDoctor={prescriptionDoctor}
handleDialogClose={handleDialogClose}
medicines={medicines}
/>
<PrescriptionDetails
selectedPrescription={selectedPrescription}
setSelectedPrescription={setSelectedPrescription}
prescriptionDoctor={prescriptionDoctor}
handleDialogClose={handleDialogClose}
medicines={medicines}
/>

<EditPrescription
isEditDialogOpen={isEditDialogOpen}
setIsEditDialogOpen={setIsEditDialogOpen}
selectedEditPrescription={selectedEditPrescription}
handleSaveEdit={handleSaveEdit}
setSelectedEditPrescription={setSelectedEditPrescription}
editErrorMessage={editErrorMessage}
setEditErrorMessage={setEditErrorMessage}
/>
<EditPrescription
isEditDialogOpen={isEditDialogOpen}
setIsEditDialogOpen={setIsEditDialogOpen}
selectedEditPrescription={selectedEditPrescription}
handleSaveEdit={handleSaveEdit}
setSelectedEditPrescription={setSelectedEditPrescription}
editErrorMessage={editErrorMessage}
setEditErrorMessage={setEditErrorMessage}
/>

{user.type === DOCTOR_TYPE_ENUM && (
<Fab
color='secondary'
aria-label='Add'
onClick={handleAddButtonClick}
sx={{
position: 'fixed',
bottom: 16,
right: 16,
zIndex: 9999,
}}
>
<AddIcon />
</Fab>
)}
{user.type === DOCTOR_TYPE_ENUM && (
<Fab
color='secondary'
aria-label='Add'
onClick={handleAddButtonClick}
sx={{
position: 'fixed',
bottom: 16,
right: 16,
zIndex: 9999,
}}
>
<AddIcon />
</Fab>
)}

<AddPrescription
isAddDialogOpen={isAddDialogOpen}
handleConfirmAdd={handleConfirmAdd}
handleCancelAdd={handleCancelAdd}
setDescription={setDescription}
/>
<AddPrescription
isAddDialogOpen={isAddDialogOpen}
handleConfirmAdd={handleConfirmAdd}
handleCancelAdd={handleCancelAdd}
setDescription={setDescription}
/>

</MainCard>
</MainCard>
</>
);
};
Expand Down

0 comments on commit 76e50c8

Please sign in to comment.