diff --git a/src/components/common/CustomDialog.tsx b/src/components/common/CustomDialog.tsx index 9221c03..27a01ed 100644 --- a/src/components/common/CustomDialog.tsx +++ b/src/components/common/CustomDialog.tsx @@ -6,7 +6,12 @@ import { DialogTitle, Button, } from '@mui/material'; -import React from 'react'; + +interface DialogAction { + text: string; + onClick: () => void; + color?: 'primary' | 'error' | 'secondary' | 'inherit'; +} interface CustomDialogProps { open: boolean; diff --git a/src/types/components.d.ts b/src/types/components.d.ts deleted file mode 100644 index 8ffdac1..0000000 --- a/src/types/components.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -interface DialogAction { - text: string; - onClick: () => void; - color?: 'primary' | 'error' | 'secondary' | 'inherit'; -} diff --git a/src/types/user.d.ts b/src/types/user.d.ts index e186e00..aa0c17b 100644 --- a/src/types/user.d.ts +++ b/src/types/user.d.ts @@ -33,7 +33,7 @@ interface OrganizationProfile extends BasicProfile { quantityNeeded: number; quantityProvided: number; providedBy: string[]; // array of uid of donors - status: 'needed' | 'fulfilled'; + status: boolean; // true if provided }[]; }[]; } diff --git a/src/utils/firebase/auth.ts b/src/utils/firebase/auth.ts index 552c0fb..fb8e381 100644 --- a/src/utils/firebase/auth.ts +++ b/src/utils/firebase/auth.ts @@ -1,7 +1,7 @@ import { GoogleAuthProvider, signInWithPopup, signOut } from 'firebase/auth'; import type { NavigateFunction } from 'react-router-dom'; -import { getDonorProfile, getOrganizationProfile } from './userProfile'; +import getUserProfile from './userProfile'; import { auth } from './firebaseConfig'; const loginUser = async ( @@ -11,35 +11,9 @@ const loginUser = async ( try { const provider = new GoogleAuthProvider(); const result = await signInWithPopup(auth, provider); - const user = result.user; - if (!user.email) { - throw new Error('User email is not available.'); - } - - const photoURL = user.photoURL ?? ''; - const displayName = user.displayName ?? ''; - const email = user.email; // Extract the user's login email - - if (userType === 'donor') { - const donorProfile = await getDonorProfile( - user.uid, - email, // Pass the email - photoURL, - displayName - ); - navigate('/'); - return donorProfile; - } else { - const organizationProfile = await getOrganizationProfile( - user.uid, - email, // Pass the email - photoURL, - displayName - ); - navigate('/organization-dashboard'); - return organizationProfile; - } + const userProfile = await getUserProfile(result.user, userType, navigate); + return userProfile; } catch (error) { console.error('Error during login:', error); return undefined; diff --git a/src/utils/firebase/index.ts b/src/utils/firebase/index.ts index b75dbb0..9c37694 100644 --- a/src/utils/firebase/index.ts +++ b/src/utils/firebase/index.ts @@ -1,4 +1,3 @@ -export { getDonorProfile, getOrganizationProfile } from './userProfile'; export { loginUser, logoutUser } from './auth'; export { auth } from './firebaseConfig'; export { updateDocument } from './firebaseUtils'; diff --git a/src/utils/firebase/userProfile.ts b/src/utils/firebase/userProfile.ts index b7b56dc..e0c608b 100644 --- a/src/utils/firebase/userProfile.ts +++ b/src/utils/firebase/userProfile.ts @@ -1,20 +1,54 @@ +import type { User } from 'firebase/auth'; +import type { NavigateFunction } from 'react-router-dom'; + import { getOrCreateDocument } from './firebaseUtils'; +const getUserProfile = async ( + user: User, + role: 'donor' | 'organization', + navigate: NavigateFunction +): Promise => { + const { uid, email, photoURL, displayName } = user; + + const userProfile = + role === 'donor' + ? getDonorProfile( + uid, + email as string, + photoURL as string, + displayName as string + ) + : getOrganizationProfile( + uid, + email as string, + photoURL as string, + displayName as string + ); + + if (role === 'donor' || !userProfile) { + navigate('/'); + } else if (role === 'organization') { + navigate('/organization-dashboard'); + } + + return userProfile; +}; + const getDonorProfile = async ( uid: string, email: string, // Login email from authentication - photoURL?: string, - displayName?: string + photoURL: string, + displayName: string ): Promise => { const defaultProfile: DonorProfile = { uid, - name: displayName || '', + name: displayName, email, // Store the login email - profilePic: photoURL || '', + profilePic: photoURL, createdAt: new Date(), role: 'donor', - joinedEvents: [], // Initialize as an empty array for joined event IDs - providedSupplies: [], // Initialize as an empty array for provided supplies + joinedEvents: [], // Initialize empty array + providedSupplies: [], // Initialize empty array }; return getOrCreateDocument('users', uid, defaultProfile); @@ -23,23 +57,20 @@ const getDonorProfile = async ( const getOrganizationProfile = async ( uid: string, email: string, // Login email for the organization - photoURL?: string, - displayName?: string, - location?: string, - description?: string, - website?: string + photoURL: string, + displayName: string ): Promise => { const defaultProfile: OrganizationProfile = { uid, - name: displayName || '', + name: displayName, email, // Store the login email - profilePic: photoURL || '', + profilePic: photoURL, createdAt: new Date(), role: 'organization', - location: location || '', // Default to an empty string if not provided - description: description || '', // Default to an empty string if not provided - website: website || '', // Default to an empty string if not provided - events: [], // Initialize as an empty array for events + location: '', // Default empty string + description: '', // Default empty string + website: '', // Default empty string + events: [], // Initialize empty array }; return getOrCreateDocument( @@ -49,4 +80,4 @@ const getOrganizationProfile = async ( ); }; -export { getDonorProfile, getOrganizationProfile }; +export default getUserProfile;