Skip to content

Commit

Permalink
chore: combine user basic info fetching into one function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZL-Asica committed Nov 18, 2024
1 parent b0f0199 commit b26334e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 55 deletions.
7 changes: 6 additions & 1 deletion src/components/common/CustomDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions src/types/components.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/types/user.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}[];
}[];
}
Expand Down
32 changes: 3 additions & 29 deletions src/utils/firebase/auth.ts
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/utils/firebase/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { getDonorProfile, getOrganizationProfile } from './userProfile';
export { loginUser, logoutUser } from './auth';
export { auth } from './firebaseConfig';
export { updateDocument } from './firebaseUtils';
67 changes: 49 additions & 18 deletions src/utils/firebase/userProfile.ts
Original file line number Diff line number Diff line change
@@ -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<DonorProfile | OrganizationProfile | undefined> => {
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<DonorProfile | undefined> => {
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<DonorProfile>('users', uid, defaultProfile);
Expand All @@ -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<OrganizationProfile | undefined> => {
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<OrganizationProfile>(
Expand All @@ -49,4 +80,4 @@ const getOrganizationProfile = async (
);
};

export { getDonorProfile, getOrganizationProfile };
export default getUserProfile;

0 comments on commit b26334e

Please sign in to comment.