forked from ayesha1209/taskChamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirebaseOperations.js
More file actions
102 lines (92 loc) · 2.81 KB
/
FirebaseOperations.js
File metadata and controls
102 lines (92 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { realDb,auth } from "./firebase";
import { ref, set, push, get, child, query, orderByChild, startAt, endAt } from "firebase/database";
export const createUserInFirebase = (user) => {
const userId = `user_${Date.now()}`;
const userRef = ref(realDb, `users/${userId}`);
return set(userRef, {
username: user.username,
password: user.password,
birthdate: user.birthdate,
country: user.country,
email: user.email,
})
.then(() => {
console.log("User data saved successfully for userId:", userId);
return userId;
})
.catch((error) => {
console.error("Error creating user in Firebase:", error);
throw error; // Rethrow error to catch it in the main flow
});
};
export const createEmptyUserChatsCollection = (userId) => {
const userChatsRef = ref(realDb, `chats/${userId}`);
return set(userChatsRef, {})
.then(() => {
console.log("Empty chats collection initialized for userId:", userId);
})
.catch((error) => {
console.error("Error creating empty chats collection:", error);
throw error; // Rethrow error to catch it in the main flow
});
};
export const fetchRecentChats = async (userId) => {
const snapshot = await realDb.ref(`/users/${userId}/recentChats`).once("value");
const recentChats = [];
snapshot.forEach((childSnapshot) => {
recentChats.push(childSnapshot.val());
});
return recentChats;
};
// Search for users in the database
export const searchUsers = async (query) => {
const snapshot = await realDb
.ref("/users")
.orderByChild("username")
.startAt(query)
.endAt(query + "\uf8ff")
.once("value");
const users = [];
snapshot.forEach((childSnapshot) => {
users.push(childSnapshot.val());
});
return users;
};
// Send a message between two users
export const sendMessage = (senderId, recipientId, message) => {
const newMessage = {
senderId,
recipientId,
text: message,
timestamp: Date.now(),
};
realDb.ref(`/chats/${senderId}_${recipientId}`).push(newMessage);
realDb.ref(`/chats/${recipientId}_${senderId}`).push(newMessage);
};
// Fetch messages between two users
export const fetchMessages = async (userId, selectedUserId) => {
const snapshot = await realDb
.ref(`/chats/${userId}_${selectedUserId}`)
.orderByChild("timestamp")
.limitToLast(50)
.once("value");
const messages = [];
snapshot.forEach((childSnapshot) => {
messages.push(childSnapshot.val());
});
return messages;
};
export const getLoggedInUser = () => {
const user = auth.currentUser;
if (user) {
// User is signed in
return {
uid: user.uid,
email: user.email,
username: user.displayName, // Assuming you are setting the displayName during registration
};
} else {
// No user is signed in, you may redirect or handle the situation
return null;
}
};