Skip to content

Commit 6164f3a

Browse files
feat: fix auth context, profile pic storage
1 parent bcffa8f commit 6164f3a

File tree

15 files changed

+115
-62
lines changed

15 files changed

+115
-62
lines changed

src/api/habits/firebase-mutations.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ import {
1010
updateDoc,
1111
} from 'firebase/firestore';
1212

13+
import { getCurrentUserId } from '@/core/auth';
1314
import { type habitColors } from '@/ui/colors';
1415

1516
import { db } from '../common/firebase';
16-
import { getUserById, type UserIdT } from '../users';
17+
import {
18+
getUserById,
19+
getUserWithRelationshipById,
20+
type UserIdT,
21+
} from '../users';
1722
import {
1823
type DbHabitT,
1924
type DbParticipantT,
@@ -27,7 +32,9 @@ type ColorName = keyof typeof habitColors;
2732
export const createHabit = async (
2833
habitCreationInfo: HabitCreationT,
2934
): Promise<DbHabitT> => {
30-
const myId = '1' as UserIdT; // TODO: Get from auth context
35+
const myId = getCurrentUserId();
36+
const currentUserData = await getUserById(myId);
37+
if (currentUserData == null) throw new Error('User data not found');
3138

3239
const newHabit = {
3340
title: habitCreationInfo.title,
@@ -39,8 +46,8 @@ export const createHabit = async (
3946
},
4047
participants: {
4148
[myId]: {
42-
displayName: 'Alex Chen', // TODO: Get from auth context
43-
username: 'alexchen',
49+
displayName: currentUserData.username,
50+
username: currentUserData.username,
4451
lastActivity: serverTimestamp(),
4552
isOwner: true,
4653
} satisfies Omit<DbParticipantT, 'lastActivity'> & {
@@ -88,8 +95,8 @@ export const editHabit = async (
8895
};
8996

9097
export const acceptHabitInvite = async (habitId: HabitIdT): Promise<void> => {
91-
const myId = '1' as UserIdT; // TODO: Get from auth context
92-
const me = await getUserById(myId);
98+
const myId = getCurrentUserId();
99+
const me = await getUserWithRelationshipById(myId);
93100
if (!me) throw new Error('User not found');
94101

95102
const habitRef = doc(db, 'habits', habitId);

src/api/habits/use-habits.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createQuery } from 'react-query-kit';
22

3-
import { type UserIdT } from '../users';
3+
import { getCurrentUserId } from '@/core';
4+
45
import { getHabits } from './firebase-queries';
56
import { type HabitT } from './types';
67

@@ -10,7 +11,7 @@ type Variables = void;
1011
export const useHabits = createQuery<Response, Variables, Error>({
1112
queryKey: ['habits'],
1213
fetcher: async () => {
13-
const myId = '1' as UserIdT; // TODO: Get from auth context
14+
const myId = getCurrentUserId();
1415
return await getHabits(myId);
1516
},
1617
});

src/api/notifications/firebase-mutations.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
serverTimestamp,
77
} from 'firebase/firestore';
88

9+
import { getCurrentUserId } from '@/core';
10+
911
import { db } from '../common/firebase';
1012
import { type HabitIdT } from '../habits';
1113
import { type UserIdT } from '../users';
@@ -15,7 +17,7 @@ export const sendHabitInvite = async (
1517
habitId: HabitIdT,
1618
receiverId: UserIdT,
1719
) => {
18-
const myId = '1' as UserIdT; // TODO: Get from auth context
20+
const myId = getCurrentUserId();
1921
const notification: Omit<DbHabitNotificationT, 'sentAt'> & {
2022
sentAt: ReturnType<typeof serverTimestamp>;
2123
} = {
@@ -29,7 +31,7 @@ export const sendHabitInvite = async (
2931
};
3032

3133
export const sendNudge = async (habitId: HabitIdT, receiverId: UserIdT) => {
32-
const myId = '1' as UserIdT; // TODO: Get from auth context
34+
const myId = getCurrentUserId();
3335
const notification: Omit<DbHabitNotificationT, 'sentAt'> & {
3436
sentAt: ReturnType<typeof serverTimestamp>;
3537
} = {
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createQuery } from 'react-query-kit';
22

3-
import { type UserIdT } from '../users';
3+
import { getCurrentUserId } from '@/core';
4+
45
import { getNotifications } from './firebase-queries';
56
import { type FriendNotificationT, type HabitNotificationT } from './types';
67

@@ -10,7 +11,7 @@ type Variables = void;
1011
export const useNotifications = createQuery<Response, Variables, Error>({
1112
queryKey: ['notifications'],
1213
fetcher: async () => {
13-
const myId = '1' as UserIdT; // TODO: Get from auth context
14+
const myId = getCurrentUserId();
1415
return await getNotifications(myId);
1516
},
1617
});

src/api/users/firebase-mutations.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import {
22
addDoc,
33
collection,
44
deleteDoc,
5-
doc,
65
getDocs,
76
query,
87
serverTimestamp,
9-
setDoc,
108
where,
119
} from 'firebase/firestore';
1210

11+
import { getCurrentUserId } from '@/core';
12+
1313
import { db } from '../common/firebase';
1414
import { type DbRelationshipT, type UserIdT } from './types';
1515

1616
export const sendFriendRequest = async (userId: UserIdT) => {
17-
const myId = '1' as UserIdT; // TODO: Get from auth context
17+
const myId = getCurrentUserId();
1818
await addDoc(collection(db, 'notifications'), {
1919
type: 'friendRequest',
2020
senderId: myId,
@@ -24,9 +24,11 @@ export const sendFriendRequest = async (userId: UserIdT) => {
2424
};
2525

2626
export const acceptFriendRequest = async (userId: UserIdT) => {
27-
const myId = '1' as UserIdT; // TODO: Get from auth context
28-
29-
const relationship: Omit<DbRelationshipT, 'friendsSince'> & { friendsSince: ReturnType<typeof serverTimestamp> } = {
27+
const myId = getCurrentUserId();
28+
29+
const relationship: Omit<DbRelationshipT, 'friendsSince'> & {
30+
friendsSince: ReturnType<typeof serverTimestamp>;
31+
} = {
3032
status: 'friends',
3133
friendsSince: serverTimestamp(),
3234
};
@@ -37,7 +39,7 @@ export const acceptFriendRequest = async (userId: UserIdT) => {
3739
userId2: userId,
3840
...relationship,
3941
});
40-
42+
4143
await addDoc(collection(db, 'relationships'), {
4244
userId1: userId,
4345
userId2: myId,
@@ -46,8 +48,8 @@ export const acceptFriendRequest = async (userId: UserIdT) => {
4648
};
4749

4850
export const removeFriend = async (userId: UserIdT) => {
49-
const myId = '1' as UserIdT; // TODO: Get from auth context
50-
51+
const myId = getCurrentUserId();
52+
5153
// Find and delete both relationship documents
5254
const relationshipsRef = collection(db, 'relationships');
5355
const q1 = query(
@@ -61,13 +63,10 @@ export const removeFriend = async (userId: UserIdT) => {
6163
where('userId2', '==', myId),
6264
);
6365

64-
const [snapshot1, snapshot2] = await Promise.all([
65-
getDocs(q1),
66-
getDocs(q2),
67-
]);
66+
const [snapshot1, snapshot2] = await Promise.all([getDocs(q1), getDocs(q2)]);
6867

6968
await Promise.all([
7069
...snapshot1.docs.map((doc) => deleteDoc(doc.ref)),
7170
...snapshot2.docs.map((doc) => deleteDoc(doc.ref)),
7271
]);
73-
};
72+
};

src/api/users/firebase-queries.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import {
77
type Timestamp,
88
where,
99
} from 'firebase/firestore';
10+
import { getDownloadURL, ref } from 'firebase/storage';
1011

11-
import { db } from '../common/firebase';
12+
import { getCurrentUserId } from '@/core';
13+
14+
import { db, storage } from '../common/firebase';
1215
import {
1316
type DbRelationshipT,
1417
type DbUserT,
@@ -68,13 +71,19 @@ const getRelationshipStatus = async (
6871
return convertDbRelationshipToRelationship(relationship as DbRelationshipT);
6972
};
7073

71-
export const getUserById = async (
74+
export const getUserById = async (id: UserIdT): Promise<UserT | null> => {
75+
const userDoc = await getDoc(doc(db, 'users', id));
76+
if (!userDoc.exists()) return null;
77+
return convertDbUserToUser(id, userDoc.data() as DbUserT);
78+
};
79+
80+
export const getUserWithRelationshipById = async (
7281
id: UserIdT,
7382
): Promise<UserWithRelationshipT | null> => {
7483
const userDoc = await getDoc(doc(db, 'users', id));
7584
if (!userDoc.exists()) return null;
7685

77-
const myId = '1' as UserIdT; // TODO: Get from auth context
86+
const myId = getCurrentUserId();
7887
const relationship = await getRelationshipStatus(myId, id);
7988

8089
return {
@@ -141,9 +150,15 @@ export const searchUsers = async (searchQuery: string): Promise<UserT[]> => {
141150
};
142151

143152
export const getUserPicture = async (userId: UserIdT): Promise<string> => {
144-
const pictureDoc = await getDoc(doc(db, 'pictures', userId));
145-
if (!pictureDoc.exists()) throw new Error('User picture not found');
146-
return pictureDoc.data().url;
153+
const defaultProfilePic = require('/assets/images/default_profile_pic.png');
154+
try {
155+
const pictureRef = ref(storage, `profilePics/${userId}`);
156+
const url = await getDownloadURL(pictureRef);
157+
return url;
158+
} catch (error) {
159+
console.log(`No profile image found for ${userId}, using default.`);
160+
return defaultProfilePic;
161+
}
147162
};
148163

149164
export const getFriends = async (userId: UserIdT): Promise<UserT[]> => {
@@ -177,7 +192,7 @@ export const getFriends = async (userId: UserIdT): Promise<UserT[]> => {
177192
// Fetch user data for each friend
178193
const friends = await Promise.all(
179194
uniqueFriendIds.map(async (friendId) => {
180-
const friend = await getUserById(friendId as UserIdT);
195+
const friend = await getUserWithRelationshipById(friendId as UserIdT);
181196
if (!friend) throw new Error('Friend not found');
182197
return friend;
183198
}),

src/api/users/use-friends.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { createQuery } from 'react-query-kit';
22

3+
import { getCurrentUserId } from '@/core';
4+
35
import { getFriends } from './firebase-queries';
4-
import { type UserIdT, type UserWithRelationshipT } from './types';
6+
import { type UserWithRelationshipT } from './types';
57

68
type Response = UserWithRelationshipT[];
79
type Variables = void;
@@ -10,7 +12,7 @@ type Variables = void;
1012
export const useFriends = createQuery<Response, Variables, Error>({
1113
queryKey: ['friends'],
1214
fetcher: async () => {
13-
const myId = '1' as UserIdT; // TODO: Get from auth context
15+
const myId = getCurrentUserId();
1416
const friends = await getFriends(myId);
1517
return friends.map((friend) => ({
1618
...friend,

src/api/users/use-remove-friend.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createMutation } from 'react-query-kit';
22

33
import { queryClient } from '../common';
44
import { removeFriend } from './firebase-mutations';
5-
import { getUserById } from './firebase-queries';
5+
import { getUserWithRelationshipById } from './firebase-queries';
66
import { type UserIdT, type UserT } from './types';
77

88
type Variables = { id: UserIdT };
@@ -11,7 +11,7 @@ type Response = UserT;
1111
export const useRemoveFriend = createMutation<Response, Variables, Error>({
1212
mutationFn: async (variables) => {
1313
await removeFriend(variables.id);
14-
const user = await getUserById(variables.id);
14+
const user = await getUserWithRelationshipById(variables.id);
1515
if (!user) throw new Error('User not found');
1616
return user;
1717
},

src/api/users/use-send-friend-request.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createMutation } from 'react-query-kit';
22

33
import { queryClient } from '../common';
44
import { sendFriendRequest } from './firebase-mutations';
5-
import { getUserById } from './firebase-queries';
5+
import { getUserWithRelationshipById } from './firebase-queries';
66
import { type UserIdT, type UserT } from './types';
77

88
type Variables = { id: UserIdT };
@@ -11,7 +11,7 @@ type Response = UserT;
1111
export const useSendFriendRequest = createMutation<Response, Variables, Error>({
1212
mutationFn: async (variables) => {
1313
await sendFriendRequest(variables.id);
14-
const user = await getUserById(variables.id);
14+
const user = await getUserWithRelationshipById(variables.id);
1515
if (!user) throw new Error('User not found');
1616
return user;
1717
},

src/api/users/use-user.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createQuery } from 'react-query-kit';
22

3-
import { getUserById } from './firebase-queries';
3+
import { getUserWithRelationshipById } from './firebase-queries';
44
import { type UserIdT, type UserWithRelationshipT } from './types';
55

66
type Variables = { id: UserIdT };
@@ -9,7 +9,7 @@ type Response = UserWithRelationshipT;
99
export const useUser = createQuery<Response, Variables, Error>({
1010
queryKey: ['user'],
1111
fetcher: async (variables) => {
12-
const user = await getUserById(variables.id);
12+
const user = await getUserWithRelationshipById(variables.id);
1313
if (!user) throw new Error('User not found');
1414
return user;
1515
},

0 commit comments

Comments
 (0)