Skip to content

Commit

Permalink
ft: patient notification context
Browse files Browse the repository at this point in the history
  • Loading branch information
mr3nz1 committed Jul 19, 2024
1 parent 5ee5d11 commit 719e17b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
14 changes: 11 additions & 3 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import ModalContainer from "@/components/UI/Modal";
import { AutocompleteDropdownContextProvider } from "react-native-autocomplete-dropdown";
import { StatusBar } from "expo-status-bar";
import AuthProvider, { useAuth } from "@/ctx/AuthContext";
import NotificationsProvider, {
useNotifications,
} from "@/ctx/NotificationsContext";

export { ErrorBoundary } from "expo-router";

Expand Down Expand Up @@ -94,7 +97,9 @@ export default function RootLayout() {
<ModalProvider>
<AuthProvider>
<AutocompleteDropdownContextProvider>
<RootLayoutNav />
<NotificationsProvider>
<RootLayoutNav />
</NotificationsProvider>
</AutocompleteDropdownContextProvider>
</AuthProvider>
</ModalProvider>
Expand All @@ -107,13 +112,16 @@ function RootLayoutNav() {
const segments = useSegments();

const inAuth = segments[0] === "(auth)";
const { notifications } = useNotifications();

console.log(notifications);

useEffect(() => {
if (!isLoggedIn && !inAuth) {
console.log("Not logged in");
return router.replace("/(auth)/SignIn&SignOut/LetsYouIn");
}

// if (isLoggedIn && !activated && !email) {
// console.log("Logged in, not activated, without email");
// return router.replace("/(auth)/SignIn&SignOut/LetsYouIn");
Expand Down Expand Up @@ -145,4 +153,4 @@ function RootLayoutNav() {
<ModalContainer />
</>
);
}
}
67 changes: 67 additions & 0 deletions ctx/NotificationsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, { createContext, useContext, useEffect, useState } from "react";

export interface NotificationsType {
notifications: NotificationType[];
getNotifications: () => Promise<void>;
deleteNotification: (id: string) => Promise<void>;
createNotification: (notificaction: NotificationType) => Promise<void>;
setNotifications: React.Dispatch<React.SetStateAction<NotificationType[]>>;
}

export interface NotificationType {
id: string;
title: string;
description: string;
createdAt: Date;
patientId: string;
}

export const NotificationsContext = createContext<NotificationsType>({
notifications: [],
getNotifications: async () => {},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
deleteNotification: async (id: string) => {},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
createNotification: async (notification: NotificationType) => {},
setNotifications: () => {},
});

interface Props {
children: React.JSX.Element | React.JSX.Element[];
}

export default function NotificationsProvider({ children }: Props) {
const [notifications, setNotifications] = useState<NotificationType[]>([]);

async function getNotifications() {}

async function deleteNotification(id: string) {
console.log(id);
}

async function createNotification(notification: NotificationType) {
console.log(notification);
}

useEffect(() => {
console.log("##########################");
console.log("Notifications context");
console.log("##########################");
}, []);

const value = {
notifications,
getNotifications,
setNotifications,
deleteNotification,
createNotification,
};

return (
<NotificationsContext.Provider value={value}>
{children}
</NotificationsContext.Provider>
);
}

export const useNotifications = () => useContext(NotificationsContext);

0 comments on commit 719e17b

Please sign in to comment.