Skip to content

Commit

Permalink
Merge branch 'develop' into user-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishith25 committed Jan 8, 2025
2 parents d518044 + 8bec8bc commit 908d4a1
Show file tree
Hide file tree
Showing 31 changed files with 832 additions and 699 deletions.
5 changes: 2 additions & 3 deletions crowdin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
files:
- source: /public/locale/{{lang}}.json
translation: /public/locale/%two_letters_code%/%original_file_name%
- source: public/locale/en.json
translation: /public/locale/%two_letters_code%.json
bundles:
- 2

6 changes: 6 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
"all_changes_have_been_saved": "All changes have been saved",
"all_details": "All Details",
"all_patients": "All Patients",
"allergen": "Allergen",
"allergies": "Allergies",
"allow_transfer": "Allow Transfer",
"allowed_formats_are": "Allowed formats are",
Expand Down Expand Up @@ -652,6 +653,7 @@
"created_by": "Created By",
"created_date": "Created Date",
"created_on": "Created On",
"criticality": "Criticality",
"csv_file_in_the_specified_format": "Select a CSV file in the specified format",
"current_address": "Current Address",
"current_password": "Current Password",
Expand Down Expand Up @@ -832,6 +834,7 @@
"encounter_discharge_disposition__snf": "Skilled nursing facility",
"encounter_duration_confirmation": "The duration of this encounter would be",
"encounter_id": "Encounter ID",
"encounter_marked_as_complete": "Encounter Completed",
"encounter_notes__all_discussions": "All Discussions",
"encounter_notes__be_first_to_send": "Be the first to send a message",
"encounter_notes__choose_template": "Choose a template or enter a custom title",
Expand Down Expand Up @@ -902,6 +905,7 @@
"error_deleting_shifting": "Error while deleting Shifting record",
"error_fetching_slots_data": "Error while fetching slots data",
"error_sending_otp": "Error while sending OTP, Please try again later",
"error_updating_encounter": "Error to Updating Encounter",
"error_verifying_otp": "Error while verifying OTP, Please request a new OTP",
"error_while_deleting_record": "Error while deleting record",
"escape": "Escape",
Expand Down Expand Up @@ -1166,6 +1170,7 @@
"manufacturer": "Manufacturer",
"map_acronym": "M.A.P.",
"mark_all_as_read": "Mark all as Read",
"mark_as_complete": "Mark as Complete",
"mark_as_fulfilled": "Mark as Fullfilled",
"mark_as_noshow": "Mark as no-show",
"mark_as_read": "Mark as Read",
Expand Down Expand Up @@ -1631,6 +1636,7 @@
"select": "Select",
"select_all": "Select All",
"select_date": "Select date",
"select_department": "Select Department",
"select_eligible_policy": "Select an Eligible Insurance Policy",
"select_facility_for_discharged_patients_warning": "Facility needs to be selected to view discharged patients.",
"select_for_administration": "Select for Administration",
Expand Down
37 changes: 15 additions & 22 deletions src/Providers/AuthUserProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import careConfig from "@careConfig";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import dayjs from "dayjs";
import { navigate } from "raviger";
import { useCallback, useEffect, useState } from "react";

Expand All @@ -13,6 +12,7 @@ import { LocalStorageKeys } from "@/common/constants";
import routes from "@/Utils/request/api";
import query from "@/Utils/request/query";
import request from "@/Utils/request/request";
import { TokenData } from "@/types/auth/otpToken";

interface Props {
children: React.ReactNode;
Expand All @@ -29,8 +29,10 @@ export default function AuthUserProvider({
const [accessToken, setAccessToken] = useState(
localStorage.getItem(LocalStorageKeys.accessToken),
);
const [patientToken, setPatientToken] = useState(
JSON.parse(localStorage.getItem(LocalStorageKeys.patientTokenKey) || "{}"),
const [patientToken, setPatientToken] = useState<TokenData | null>(
JSON.parse(
localStorage.getItem(LocalStorageKeys.patientTokenKey) || "null",
),
);

const { data: user, isLoading } = useQuery({
Expand All @@ -40,16 +42,6 @@ export default function AuthUserProvider({
enabled: !!localStorage.getItem(LocalStorageKeys.accessToken),
});

useEffect(() => {
if (
patientToken.token &&
Object.keys(patientToken).length > 0 &&
dayjs(patientToken.createdAt).isAfter(dayjs().subtract(14, "minutes"))
) {
navigate("/patient/home");
}
}, [patientToken]);

useEffect(() => {
if (!user) {
return;
Expand Down Expand Up @@ -83,20 +75,20 @@ export default function AuthUserProvider({
[queryClient],
);

const patientLogin = useCallback(() => {
setPatientToken(
JSON.parse(
localStorage.getItem(LocalStorageKeys.patientTokenKey) || "{}",
),
const patientLogin = (tokenData: TokenData, redirectUrl: string) => {
setPatientToken(tokenData);
localStorage.setItem(
LocalStorageKeys.patientTokenKey,
JSON.stringify(tokenData),
);
navigate("/patient/home");
}, []);
navigate(redirectUrl);
};

const signOut = useCallback(async () => {
localStorage.removeItem(LocalStorageKeys.accessToken);
localStorage.removeItem(LocalStorageKeys.refreshToken);
localStorage.removeItem(LocalStorageKeys.patientTokenKey);
setPatientToken({});
setPatientToken(null);

await queryClient.resetQueries({ queryKey: ["currentUser"] });

Expand Down Expand Up @@ -134,7 +126,7 @@ export default function AuthUserProvider({
const SelectedRouter = () => {
if (user) {
return children;
} else if (patientToken.token) {
} else if (patientToken?.token) {
return otpAuthorized;
} else {
return unauthorized;
Expand All @@ -148,6 +140,7 @@ export default function AuthUserProvider({
signOut,
user,
patientLogin,
patientToken,
}}
>
<SelectedRouter />
Expand Down
54 changes: 23 additions & 31 deletions src/Providers/PatientUserProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
import { useQuery } from "@tanstack/react-query";
import { navigate } from "raviger";
import { createContext, useEffect, useState } from "react";

import { SidebarProvider } from "@/components/ui/sidebar";
import { AppSidebar } from "@/components/ui/sidebar/app-sidebar";

import { LocalStorageKeys } from "@/common/constants";
import { useAuthContext } from "@/hooks/useAuthUser";

import routes from "@/Utils/request/api";
import query from "@/Utils/request/query";
import { AppointmentPatient } from "@/pages/Patient/Utils";
import { TokenData } from "@/types/auth/otpToken";

const tokenData: TokenData = JSON.parse(
localStorage.getItem(LocalStorageKeys.patientTokenKey) || "{}",
);

export type PatientUserContextType = {
patients?: AppointmentPatient[];
selectedPatient: AppointmentPatient | null;
setSelectedPatient: (patient: AppointmentPatient) => void;
tokenData: TokenData;
};

export const PatientUserContext = createContext<PatientUserContextType>({
patients: undefined,
selectedPatient: null,
setSelectedPatient: () => {},
tokenData: tokenData,
});
export const PatientUserContext = createContext<PatientUserContextType | null>(
null,
);

interface Props {
children: React.ReactNode;
Expand All @@ -38,14 +29,16 @@ export default function PatientUserProvider({ children }: Props) {
const [selectedPatient, setSelectedPatient] =
useState<AppointmentPatient | null>(null);

const { patientToken: tokenData } = useAuthContext();

const { data: userData } = useQuery({
queryKey: ["patients", tokenData.phoneNumber],
queryKey: ["patients", tokenData],
queryFn: query(routes.otp.getPatient, {
headers: {
Authorization: `Bearer ${tokenData.token}`,
Authorization: `Bearer ${tokenData?.token}`,
},
}),
enabled: !!tokenData.token,
enabled: !!tokenData?.token,
});

useEffect(() => {
Expand All @@ -62,22 +55,21 @@ export default function PatientUserProvider({ children }: Props) {
}
}, [userData]);

const patientUserContext: PatientUserContextType = {
patients,
selectedPatient,
setSelectedPatient,
tokenData,
};
if (!tokenData) {
navigate("/");
return null;
}

return (
<PatientUserContext.Provider value={patientUserContext}>
<SidebarProvider>
<AppSidebar
patientUserContext={patientUserContext}
facilitySidebar={false}
/>
{children}
</SidebarProvider>
<PatientUserContext.Provider
value={{
patients,
selectedPatient,
setSelectedPatient,
tokenData: tokenData,
}}
>
{children}
</PatientUserContext.Provider>
);
}
94 changes: 65 additions & 29 deletions src/Routers/PatientRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import careConfig from "@careConfig";
import { useRoutes } from "raviger";

import { SidebarTrigger } from "@/components/ui/sidebar";
import { SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar";
import { AppSidebar, SidebarFor } from "@/components/ui/sidebar/app-sidebar";

import ErrorBoundary from "@/components/Common/ErrorBoundary";
import ErrorPage from "@/components/ErrorPages/DefaultErrorPage";
import { patientTabs } from "@/components/Patient/PatientDetailsTab";
import { PatientHome } from "@/components/Patient/PatientHome";

import PatientUserProvider from "@/Providers/PatientUserProvider";
import { PatientRegistration } from "@/pages/Appoinments/PatientRegistration";
import PatientSelect from "@/pages/Appoinments/PatientSelect";
import { ScheduleAppointment } from "@/pages/Appoinments/Schedule";
import { AppointmentSuccess } from "@/pages/Appoinments/Success";
import { FacilitiesPage } from "@/pages/Facility/FacilitiesPage";
import PatientIndex from "@/pages/Patient/index";

import PublicRouter from "./PublicRouter";

const PatientRoutes = {
const DashboardRoutes = {
"/nearby_facilities": () => <FacilitiesPage />,
"/facility/:facilityId/appointments/:appointmentId/success": ({
appointmentId,
Expand All @@ -35,43 +39,75 @@ const PatientRoutes = {
}) => <PatientHome id={id} page={tab} />,
};

const AppointmentRoutes = {
"/facility/:facilityId/appointments/:staffId/book-appointment": ({
facilityId,
staffId,
}: {
facilityId: string;
staffId: string;
}) => <ScheduleAppointment facilityId={facilityId} staffId={staffId} />,
"/facility/:facilityId/appointments/:staffId/patient-select": ({
facilityId,
staffId,
}: {
facilityId: string;
staffId: string;
}) => <PatientSelect facilityId={facilityId} staffId={staffId} />,
"/facility/:facilityId/appointments/:staffId/patient-registration": ({
facilityId,
staffId,
}: {
facilityId: string;
staffId: string;
}) => <PatientRegistration facilityId={facilityId} staffId={staffId} />,
};

export default function PatientRouter() {
const pages = useRoutes(PatientRoutes);
const pages = useRoutes(DashboardRoutes);

const appointmentPages = useRoutes(AppointmentRoutes);

if (!pages) {
if (appointmentPages) {
return <PatientUserProvider>{appointmentPages}</PatientUserProvider>;
}
return <PublicRouter />;
}

return (
<PatientUserProvider>
<main
id="pages"
className="flex-1 overflow-y-auto bg-gray-100 focus:outline-none md:pb-2 md:pr-2"
>
<div className="relative z-10 flex h-16 shrink-0 bg-white shadow md:hidden">
<div className="flex items-center">
<SidebarTrigger className="px-2" />
<SidebarProvider>
<AppSidebar sidebarFor={SidebarFor.PATIENT} />
<main
id="pages"
className="flex-1 overflow-y-auto bg-gray-100 focus:outline-none md:pb-2 md:pr-2"
>
<div className="relative z-10 flex h-16 shrink-0 bg-white shadow md:hidden">
<div className="flex items-center">
<SidebarTrigger className="px-2" />
</div>
<a
href="/"
className="flex h-full w-full items-center px-4 md:hidden"
>
<img
className="h-8 w-auto"
src={careConfig.mainLogo?.dark}
alt="care logo"
/>
</a>
</div>
<a
href="/"
className="flex h-full w-full items-center px-4 md:hidden"
<div
className="max-w-8xl mx-auto mt-4 min-h-[96vh] rounded-lg border bg-gray-50 p-3 shadow"
data-cui-page
>
<img
className="h-8 w-auto"
src={careConfig.mainLogo?.dark}
alt="care logo"
/>
</a>
</div>
<div
className="max-w-8xl mx-auto mt-4 min-h-[96vh] rounded-lg border bg-gray-50 p-3 shadow"
data-cui-page
>
<ErrorBoundary fallback={<ErrorPage forError="PAGE_LOAD_ERROR" />}>
{pages}
</ErrorBoundary>
</div>
</main>
<ErrorBoundary fallback={<ErrorPage forError="PAGE_LOAD_ERROR" />}>
{pages}
</ErrorBoundary>
</div>
</main>
</SidebarProvider>
</PatientUserProvider>
);
}
Loading

0 comments on commit 908d4a1

Please sign in to comment.