Skip to content

Commit

Permalink
refactor: make context more general
Browse files Browse the repository at this point in the history
  • Loading branch information
MattCMcCoy committed Feb 3, 2024
1 parent cefb51e commit 640851d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 59 deletions.
6 changes: 3 additions & 3 deletions client/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as React from 'react';
import { SafeAreaView } from 'react-native-safe-area-context';
import Router from './navigation/Router';
import UserContext from './contexts/userContext';
import CareWalletProvider from './contexts/CareWalletContext';

export default function App() {
return (
<UserContext>
<CareWalletProvider>
<SafeAreaView className="flex-1">
<Router />
</SafeAreaView>
</UserContext>
</CareWalletProvider>
);
}
53 changes: 53 additions & 0 deletions client/contexts/CareWalletContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React, { createContext, useContext, useEffect, useState } from 'react';
import { getAuth, onAuthStateChanged } from 'firebase/auth';
import { Group, User } from './types';

type CareWalletContextData = {
user: User;
group: Group;
};

const CareWalletContext = createContext({} as CareWalletContextData);

export default function CareWalletProvider({ children }: { children: any }) {
const [user, setUser] = useState({} as User);
const [group, setGroup] = useState({} as Group);
const auth = getAuth();

useEffect(() => {
onAuthStateChanged(auth, (user) => {
const signedInUser: User = {
userID: user?.uid ?? '',
userEmail: user?.email ?? ''
};
setUser(signedInUser);
});
setGroup({
groupID: 'TEMP - REPLACE WITH ACTUAL',
role: 'TEMP - REPLACE WITH ACTUAL'
});
}, []);

const CareWalletContextStore: CareWalletContextData = {
user: user,
group: group
};

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

export const useCareWalletContext = (): CareWalletContextData => {
const context = useContext(CareWalletContext);

if (!context) {
throw new Error(
'useCareWalletContext must be used within a CareWalletContextProvider'
);
}

return context;
};
9 changes: 9 additions & 0 deletions client/contexts/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface User {
userID: string;
userEmail: string;
}

export interface Group {
groupID: string;
role: string; // TODO: update to enum
}
45 changes: 0 additions & 45 deletions client/contexts/userContext.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions client/screens/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const LoginPage: React.FC = () => {
} else {
Alert.alert('Login Success', 'Welcome back!');
// console.log('result: ', result);
navigation.navigate('Landing');
navigation.navigate('BottomNavScreens');
}
};

Expand All @@ -37,7 +37,7 @@ const LoginPage: React.FC = () => {
} else {
Alert.alert('Signup Success', 'Welcome to the app!');
// console.log('result: ', result);
navigation.navigate('Landing');
navigation.navigate('BottomNavScreens');
}
};

Expand Down
12 changes: 7 additions & 5 deletions client/screens/Medication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import * as React from 'react';
import { View, Text } from 'react-native';
import { getAllMedications } from '../services/medication';
import { Medication } from '../types/medication';
import { useUser } from '../contexts/userContext';
import { useCareWalletContext } from '../contexts/CareWalletContext';

export default function MedList() {
const [medications, setMedications] = React.useState<Medication[]>();
const { user } = useUser();
const { user, group } = useCareWalletContext();
React.useEffect(() => {
getAllMedications().then((med) => setMedications(med));
}, []);
Expand All @@ -19,11 +19,13 @@ export default function MedList() {
{`Name: ${med.medication_name} id: ${med.medication_id}`}
</Text>
))}
{user && (
<Text>
{user && group && (
<View>
<Text>The user id is: {user.userID}</Text>
<Text>The user email is: {user.userEmail}</Text>
</Text>
<Text>The group id is: {group.groupID}</Text>
<Text>The group role is: {group.role}</Text>
</View>
)}
</View>
);
Expand Down
4 changes: 0 additions & 4 deletions client/types/user.ts

This file was deleted.

0 comments on commit 640851d

Please sign in to comment.