Skip to content

Commit

Permalink
axios indreceptor added
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Aug 30, 2024
1 parent 04ded2b commit 7083a09
Show file tree
Hide file tree
Showing 28 changed files with 249 additions and 193 deletions.
28 changes: 23 additions & 5 deletions client/app/(patient)/profile/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use client';
import NavSection from "@/components/(patient)/profile/NavSection";
import { memo, ReactNode, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { getUserProfile } from "@/services/api/patient";
import { ErrorResponse, IPatient } from "@/types";
import { AxiosError } from "axios";

interface Props {
children: ReactNode;
Expand All @@ -9,15 +13,29 @@ interface Props {
}

const ProfilePageLayout = ({ children, appointments, records }: Props) => {
const [section,setSection] = useState<"profile"|"appointments"|"records">('profile')
const [section, setSection] = useState<"profile" | "appointments" | "records">('profile');

const { data , isLoading, isError } = useQuery<IPatient, AxiosError<ErrorResponse>>({
queryKey:['patientProfile'],
queryFn:getUserProfile
});

if (isLoading) {
return <div>Loading...</div>;
}

if (isError) {
return <div>Error loading profile data</div>;
}


return (
<div className="min-h-screen p-4 sm:p-6 md:p-8">
<div className="max-w-4xl mx-auto space-y-4 sm:space-y-6">
<NavSection setSection={setSection} />
{section==='profile'&&children}
{section==='appointments'&&appointments}
{section==='records'&&records}
<NavSection setSection={setSection} />
{section === 'profile' && children}
{section === 'appointments' && appointments}
{section === 'records' && records}
</div>
</div>
);
Expand Down
1 change: 0 additions & 1 deletion client/app/(patient)/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import PersonalInformation from "@/components/(patient)/profile/PersonalInformation";
import UpcomingAppointment from "@/components/(patient)/profile/UpcomingAppointment";
import AllergiesAndConditions from "@/components/(patient)/profile/AllergiesAndConditions";
Expand Down
26 changes: 16 additions & 10 deletions client/app/(patient)/signin/(.)otp-verification/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
'use client'
"use client";
import { FormEvent } from "react";
import OtpVerificationModel from "@/components/models/OtpVerificationModel";

const OptInterceptor = () =>{
const handleVerify = (e:FormEvent)=>{
e.preventDefault();
};
const handleResend = ()=>{}
return (
<OtpVerificationModel returnRoute="/signin" key={12} timer={30} handleResend={handleResend} handleVerify={handleVerify} />
);
}
const OptInterceptor = () => {
const handleVerify = (e: FormEvent) => {
e.preventDefault();
};
const handleResend = () => {};
return (
<OtpVerificationModel
returnRoute="/signin"
key={12}
timer={30}
handleResend={handleResend}
handleVerify={handleVerify}
/>
);
};

export default OptInterceptor;
34 changes: 34 additions & 0 deletions client/app/(patient)/signin/components/FormSection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react'
import SigninForm from "../../../../components/common/forms/SigninForm";
import Image from "next/image";
import Link from "next/link";
import { Banners } from "../../../../constants";

const FromSection = () => {

return (
<div className="flex h-screen max-h-screen">
<section className="remove-scrollbar container my-auto">
<div className="sub-container max-[496px]">
<Image
src={"/assets/icons/logo-full.svg"}
width={1000}
height={1000}
alt="patient"
className="mb-12 h-10 w-fit"
/>
<SigninForm />
<div className="text-14-regular py-12 flex justify-between">
<p className="justify-items-end text-dark-600 xl:text-left">© 2024 AVM Ayurveda&apos;s</p>
<Link href={"/signup"} className="text-green-500 text-xs">
Staff-Login
</Link>
</div>
</div>
</section>
<Image src={Banners.signin} height={1000} width={1000} alt="patient" className="side-img max-w-[50%]" />
</div>
);
};

export default FromSection;
12 changes: 7 additions & 5 deletions client/app/(patient)/signin/otp-verification/page.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
"use client";
import OtpVerificationSection from "@/components/common/forms/OtpForms";
import Image from "next/image";
import React, { FormEvent, useState } from "react";
import { useValidateOtpPatient } from "@/lib/hooks/usePatinet";
import { FormEvent, useState } from "react";
import { useValidateOtpPatient } from "@/lib/hooks/usePatientAuth";
import { useToast } from "@/components/ui/use-toast";
import { useRouter } from "next/navigation";
import { Button } from "@/components/ui/button";
import Link from "next/link";
import { Banners } from "@/constants";
import { useDispatch } from "react-redux";

const OtpVerificationPage = () => {
const [otp, setOtp] = useState<string>("");
const { mutate: validateOtp, isPending } = useValidateOtpPatient();
const { toast } = useToast();
const navigate = useRouter();

const dispatch = useDispatch();

const handleVerify = async (e: FormEvent) => {
e.preventDefault();
validateOtp(
{ email: "[email protected]", otp: parseInt(otp) },
{
onSuccess: (data) => {
onSuccess: () => {
toast({
title: "Otp Verification Success ✅",
description: "Authentication Completed!. let's book your first appointment",
Expand All @@ -31,7 +33,7 @@ const OtpVerificationPage = () => {
</Button>
),
});
console.log(data);
navigate.push("/");
},
onError: (error) => {
toast({
Expand Down
36 changes: 2 additions & 34 deletions client/app/(patient)/signin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,12 @@
import SigninForm from "@/components/common/forms/SigninForm";
import Image from "next/image";
import Link from "next/link";
import { Metadata } from "next";
import { Banners } from "@/constants";
import FormSection from './components/FormSection'

export const metadata: Metadata = {
title: "SignIn",
};
const SignIn = () => {
return (
<div className="flex h-screen max-h-screen">
<section className="remove-scrollbar container my-auto">
<div className="sub-container max-[496px]">
<Image
src={"/assets/icons/logo-full.svg"}
width={1000}
height={1000}
alt="patient"
className="mb-12 h-10 w-fit"
/>
<SigninForm />
<div className="text-14-regular py-12 flex justify-between">
<p className="justify-items-end text-dark-600 xl:text-left">
© 2024 AVM Ayurveda&apos;s
</p>
<Link href={"/signup"} className="text-green-500 text-xs">
Staff-Login
</Link>
</div>
</div>
</section>

<Image
src={Banners.signin}
height={1000}
width={1000}
alt="patient"
className="side-img max-w-[50%]"
/>
</div>
<FormSection />
);
};

Expand Down
2 changes: 1 addition & 1 deletion client/app/(patient)/signup/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Image from "next/image";
import SignupForm from "@/components/common/forms/SignupForm";
import { Metadata } from "next";
import { Banners } from "@/constants";
import { Banners } from "@/constants";;

export const metadata: Metadata = {
title: "SignUp",
Expand Down
2 changes: 1 addition & 1 deletion client/components/common/forms/SigninForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Link from "next/link";
import { FormFieldType } from "@/types/fromTypes";
import { useToast } from "../../ui/use-toast";
import { useRouter } from "next/navigation";
import { useSignInPatient } from "@/lib/hooks/usePatinet";
import { useSignInPatient } from "@/lib/hooks/usePatientAuth";

const LoginForm = () => {
const [error, setError] = useState("");
Expand Down
2 changes: 1 addition & 1 deletion client/components/common/forms/SignupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Link from "next/link";
import { useRouter } from "next/navigation";
import { useToast } from "../../ui/use-toast";
import { Button } from "../../ui/button";
import { useSignUpPatient } from "@/lib/hooks/usePatinet";
import { useSignUpPatient } from "@/lib/hooks/usePatientAuth";

const RegistrationForm = () => {
const [error, setError] = useState<string>("");
Expand Down
4 changes: 2 additions & 2 deletions client/components/layout/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { useSelector } from "react-redux";

export const NavBar = () => {
const path = usePathname();
const patientToken = useSelector(selectPatientToken);
const patientToken = useSelector(selectPatientToken);

if (path.includes("signup") || path.includes("staff") || path.includes("signin")) {
return null;
}
};

return (
<header className="sticky top-0 flex h-16 items-center gap-4 border-b bg-dark-300 bg-opacity-55 px-4 md:px-6 z-50">
Expand Down
24 changes: 0 additions & 24 deletions client/components/wrapper/PatientWithAuth.tsx

This file was deleted.

26 changes: 0 additions & 26 deletions client/components/wrapper/StaffWithAuth.tsx

This file was deleted.

15 changes: 1 addition & 14 deletions client/lib/features/authSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ const authSlice = createSlice({
name: "auth",
initialState,
reducers: {
setCredentials: (state, action: PayloadAction<SetCredentialsPayload>) => {
const { accessToken, type } = action.payload;
if (type === "patient") {
state.patientToken = accessToken;
} else if (type === "doctor") {
state.doctorToken = accessToken;
} else if (type === "admin") {
state.adminToken = accessToken;
}
if (typeof window !== "undefined") {
localStorage.setItem("auth", JSON.stringify(state));
}
},
logOut: (state, action: PayloadAction<LogOutPayload>) => {
const { type } = action.payload;
if (type === "patient") {
Expand All @@ -58,7 +45,7 @@ const authSlice = createSlice({
},
});

export const { setCredentials, logOut } = authSlice.actions;
export const { logOut } = authSlice.actions;

export const selectPatientToken = (state: RootState) => state.auth.patientToken;
export const selectAdminToken = (state: RootState) => state.auth.adminToken;
Expand Down
35 changes: 35 additions & 0 deletions client/lib/hooks/usePatientAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ErrorResponse, IPatient } from "@/types";
import { useMutation } from "@tanstack/react-query";
import { signInPatient, signUpPatient, validateOtpPatient } from "@/services/api/auth/patientAuth";
import { AxiosError } from "axios";

export const useSignUpPatient = () => {
return useMutation<{ message: string }, AxiosError<ErrorResponse>, IPatient>({
mutationFn: (patient) => signUpPatient(patient),
onError: (error: AxiosError) => {
console.log("Error in creating patient:", error);
},
});
};

export const useSignInPatient = () => {
return useMutation<
{ message: string; email: string },
AxiosError<ErrorResponse>,
{ email: string; password: string }
>({
mutationFn: ({ email, password }) => signInPatient(email, password),
onError: (error) => {
console.log("Error in signing in:", error);
},
});
};

export const useValidateOtpPatient = () => {
return useMutation<{ accessToken: string }, AxiosError<ErrorResponse>, { email: string; otp: number }>({
mutationFn: ({ email, otp }) => validateOtpPatient(email, otp),
onError: (error) => {
console.log("Error in OTP validation:", error);
},
});
};
32 changes: 0 additions & 32 deletions client/lib/hooks/usePatinet.ts

This file was deleted.

Loading

1 comment on commit 7083a09

@vercel
Copy link

@vercel vercel bot commented on 7083a09 Aug 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.