Skip to content

Commit

Permalink
health packages is done
Browse files Browse the repository at this point in the history
  • Loading branch information
3laaHisham committed Nov 14, 2023
1 parent 04b46bf commit 92cbaec
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 50 deletions.
5 changes: 5 additions & 0 deletions client/src/layouts/dashboard/config-navigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const navConfig = [
path: '/packages',
icon: icon('ic_cart')
},
{
title: 'My package',
path: '/viewPackage',
icon: icon('ic_cart')
},
{
title: 'blog',
path: '/blog',
Expand Down
17 changes: 17 additions & 0 deletions client/src/pages/viewPackage.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Helmet } from 'react-helmet-async';

import { MyPackageView } from 'src/sections/myPackage';

// ----------------------------------------------------------------------

export default function ViewPackagePage() {
return (
<>
<Helmet>
<title> User </title>
</Helmet>

<MyPackageView />
</>
);
}
5 changes: 3 additions & 2 deletions client/src/routes/sections.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const ForgotPage = lazy(() => import('src/pages/forgot-password'));
export const ResetPage = lazy(() => import('src/pages/reset-password'));
export const PackagePage = lazy(() => import('src/pages/packages'));
export const AddFamilyPage = lazy(() => import('src/pages/addFamily'));

export const ViewPackagePage = lazy(() => import('src/pages/viewPackage'));
export const DoctorDocumentUploadPage = lazy(() => import('src/pages/doctor-document-upload'));
export const RequestsListPage = lazy(() => import('src/pages/requests-list'));
export const MedicalHistoryPage = lazy(() => import('src/pages/medical-history'));
Expand Down Expand Up @@ -51,7 +51,8 @@ export default function Router() {
{ path: 'health-record', element: <HealthRecordPage /> },
{ path: 'doctors', element: <DoctorsPage /> },
{ path: 'packages', element: <PackagePage /> },
{ path: 'addFamily', element: <AddFamilyPage /> }
{ path: 'addFamily', element: <AddFamilyPage /> },
{ path: 'viewPackage', element: <ViewPackagePage /> }
]
},
{
Expand Down
1 change: 1 addition & 0 deletions client/src/sections/myPackage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default as MyPackageView} from './my-package-view'
79 changes: 79 additions & 0 deletions client/src/sections/myPackage/my-package-view.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, { useState, useEffect } from 'react';
import { Helmet } from 'react-helmet-async';
import { axiosInstance } from 'src/utils/axiosInstance';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import Container from '@mui/material/Container';
import Grid from '@mui/material/Grid';
import { styled } from '@mui/material/styles';
import Divider from '@mui/material/Divider';
import LoadingButton from '@mui/lab/LoadingButton';
import { useQuery, useMutation } from 'react-query';
import { useNavigate } from 'react-router-dom';

export default function MyPackageView() {
const navigate = useNavigate();
const [userPackage, setUserPackage] = useState({});

const { isLoading: isLoadingPackage } = useQuery(
'myPackages',
() =>
axiosInstance.get('/me/package').then((res) => {
console.log(res.data);
setUserPackage(res.data.userPackage);
}),
{
refetchOnMount: false,
refetchOnWindowFocus: false
}
);

const { isLoading, mutate: unsubscribe } = useMutation(() => axiosInstance.post('me/package', { cancel: true }), {
onSuccess: () => {
navigate('/packages');
}
});

if (isLoadingPackage) return 'Loading...';
console.log(userPackage);

const StyledPackageContainer = styled(Grid)(({ theme }) => ({
backgroundColor: theme.palette.background.paper,
padding: theme.spacing(2),
textAlign: 'center',
color: theme.palette.text.secondary
}));

return (
<Container>
<Typography variant="h4" sx={{ mb: 3 }}>
Packages
</Typography>

<Grid container spacing={3} direction="row" justifyContent="center" alignItems="center" sx={{ mb: 3, mt: 5 }}>
<StyledPackageContainer key={userPackage._id} spacing={5}>
<Typography variant="h6" sx={{ mt: -2, mb: 3, textAlign: 'center' }}>
{userPackage.name}
</Typography>
<Typography sx={{ mb: 2 }}>Price: {userPackage.price}</Typography>
<Typography sx={{ mb: 2 }}>Session Discount: {userPackage.sessionDiscount}</Typography>
<Typography sx={{ mb: 2 }}>Medicine Discount: {userPackage.medicineDiscount}</Typography>
<Typography sx={{ mb: 2 }}>Family Discount: {userPackage.familyDiscount}</Typography>

<LoadingButton
onClick={unsubscribe}
loading={isLoading}
loadingIndicator="Loading…"
fullWidth
size="large"
type="submit"
variant="contained"
color="inherit"
>
Cancel
</LoadingButton>
</StyledPackageContainer>
</Grid>
</Container>
);
}
86 changes: 41 additions & 45 deletions client/src/sections/packages/view/package-view.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMutation, useQuery } from "react-query";
import { useState } from "react";
import { axiosInstance } from "src/utils/axiosInstance";
import { useMutation, useQuery } from 'react-query';
import { useState } from 'react';
import { axiosInstance } from 'src/utils/axiosInstance';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import Container from '@mui/material/Container';
Expand All @@ -9,69 +9,67 @@ import { styled } from '@mui/material/styles';
import Divider from '@mui/material/Divider';
import LoadingButton from '@mui/lab/LoadingButton';

import { useRouter } from 'src/routes/hooks';

const StyledPackageContainer = styled(Grid)(({ theme }) => ({
backgroundColor: theme.palette.mode === 'dark' ? theme.palette.grey[700] : theme.palette.grey[200],
padding: theme.spacing(6),
marginBottom: theme.spacing(6),
borderRadius: theme.shape.borderRadius,
borderRadius: theme.shape.borderRadius
}));

const PackageView = () => {

const { isLoading, error, data: userPackages } = useQuery('packages', () =>
axiosInstance.get('/packages').then((res) => res.data.result), {
refetchOnMount: false,
refetchOnWindowFocus: false,
});

const [subscribeMessage, setSubscribeMessage] = useState('');

export default function PackageView() {
const router = useRouter();

const subscribe = async(packageID) => {
const res = await axiosInstance.post('/me/package',{packageID:packageID});
if(res.data.message === 'Unauthorized')
setSubscribeMessage('Please login first');
else
{
setSubscribeMessage(res.data.message);
}
}

const useSubscribe = () =>
{
return useMutation(subscribe);
}

const{mutate,isLoading:isLoadingSubscribe} = useSubscribe();
const {
isLoading: isLoadingPackages,
error,
data: userPackages
} = useQuery('packages', () => axiosInstance.get('/packages').then((res) => res.data.result), {
refetchOnWindowFocus: false
});

const handleClick = (packageID) => {
return mutate(packageID);
}
const { isLoading, mutate: subscribe } = useMutation((selectedPackage) => {
axiosInstance.post('/me/package', { packageID: selectedPackage._id }).then((res) => router.push('/viewPackage'));

if (isLoading) return 'Loading...';
axiosInstance
.post(`/payment/session/oneTimePayment`, {
products: [{ name: selectedPackage.name, price: selectedPackage.price, quantity: 1 }]
})
.then((res) => {
window.location.replace(res.data.url);
})
.catch((err) => console.log(err));
});

if (isLoadingPackages) return 'Loading...';
if (error) return 'An error has occurred' + error.message;



return (
<Container>
<Typography variant="h4" sx={{ mb: 3 }}>
Packages
</Typography>
{subscribeMessage && (<Typography variant="body1" sx={{ mt: 2 }}>{subscribeMessage}</Typography>)}
{!userPackages && (<Typography variant="body1" sx={{ mt: 2 }}>There are currently no packages</Typography>)}
<Grid container spacing={3} direction="row" justifyContent="center" alignItems="center" sx={{ mb: 3, mt:5 }}>

{!userPackages && (
<Typography variant="body1" sx={{ mt: 2 }}>
There are currently no packages
</Typography>
)}
<Grid container spacing={3} direction="row" justifyContent="center" alignItems="center" sx={{ mb: 3, mt: 5 }}>
{userPackages.map((userPackage) => (
<StyledPackageContainer key={userPackage._id} spacing={5}>
<Typography variant='h6' sx={{ mt: -2, mb: 3 , textAlign: 'center' }}>{userPackage.name}</Typography>
<Typography variant="h6" sx={{ mt: -2, mb: 3, textAlign: 'center' }}>
{userPackage.name}
</Typography>
<Typography sx={{ mb: 2 }}>Price: {userPackage.price}</Typography>
<Typography sx={{ mb: 2 }}>Session Discount: {userPackage.sessionDiscount}</Typography>
<Typography sx={{ mb: 2 }}>Medicine Discount: {userPackage.medicineDiscount}</Typography>
<Typography sx={{ mb: 2 }}>Family Discount: {userPackage.familyDiscount}</Typography>

<LoadingButton
onClick={() => handleClick(userPackage._id)}
loading={isLoadingSubscribe}
onClick={() => subscribe(userPackage)}
loading={isLoading}
loadingIndicator="Loading…"
fullWidth
size="large"
Expand All @@ -86,6 +84,4 @@ const PackageView = () => {
</Grid>
</Container>
);
};

export default PackageView;
}
4 changes: 1 addition & 3 deletions server/src/services/doctor.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { v4: uuidv4 } = require('uuid');
import { HttpError } from '../utils';


import StatusCodes from 'http-status-codes';
import { User, Contract, Appointment, IPatient, IDoctor, Doctor, Request, Patient } from '../models';

Expand Down Expand Up @@ -149,5 +148,4 @@ const viewAvailableAppointments = async (doctorID: string) => {
};
};

export { getDoctors, getMyPatients, viewAvailableAppointments, };

export { getDoctors, getMyPatients, viewAvailableAppointments };
1 change: 1 addition & 0 deletions server/src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const updateInfo = async (id: string, info: any) => {

const updatedUser = await User.findByIdAndUpdate(id, info, { new: true });
if (!updatedUser) throw new HttpError(StatusCodes.NOT_FOUND, 'the user does not exist');

return {
status: StatusCodes.OK,
messsage: 'the user updated successfully',
Expand Down

0 comments on commit 92cbaec

Please sign in to comment.