race condition with search and firebase initializing
This commit is contained in:
@@ -12,4 +12,4 @@ export const firebaseApp = initializeApp(devConfig);
|
||||
export const firebaseAuth = getAuth(firebaseApp);
|
||||
|
||||
// Initialize Cloud Firestore and get a reference to the service
|
||||
export const firestore = getFirestore(firebaseApp);
|
||||
export const firestoreDb = getFirestore(firebaseApp);
|
||||
|
||||
@@ -8,9 +8,14 @@ import {
|
||||
QueryDocumentSnapshot,
|
||||
Timestamp,
|
||||
FieldValue,
|
||||
getDoc,
|
||||
query,
|
||||
where,
|
||||
getDocs,
|
||||
} from 'firebase/firestore';
|
||||
|
||||
import { User as FirebaseUser } from 'firebase/auth';
|
||||
import { firestoreDb } from './connect';
|
||||
|
||||
/**
|
||||
* UTILS
|
||||
@@ -20,11 +25,15 @@ const converter = <T>() => ({
|
||||
fromFirestore: (snap: QueryDocumentSnapshot) => snap.data() as T,
|
||||
});
|
||||
|
||||
const dataPoint = <T>(collectionPath: string) =>
|
||||
doc(getFirestore(), collectionPath).withConverter(converter<T>());
|
||||
const docPoint = <T>(collectionPath: string) =>
|
||||
firestoreDb && doc(getFirestore(), collectionPath).withConverter(converter<T>());
|
||||
|
||||
const collectionPoint = <T>(collectionPath: string) =>
|
||||
firestoreDb && collection(getFirestore(), collectionPath).withConverter(converter<T>());
|
||||
|
||||
const db = {
|
||||
user: (userId: string) => dataPoint<User>(`users/${userId}`),
|
||||
users: collectionPoint<User>(`users`),
|
||||
user: (userId: string) => docPoint<User>(`users/${userId}`),
|
||||
};
|
||||
|
||||
export class User {
|
||||
@@ -65,3 +74,31 @@ export const createUser = async (user: FirebaseUser) => {
|
||||
throw new Error('Error creating user');
|
||||
}
|
||||
};
|
||||
|
||||
export const searchUsers = async (searchQuery: string): Promise<User[] | undefined> => {
|
||||
try {
|
||||
const emaildocSearchQuery = query(
|
||||
db.users,
|
||||
where('email', '>=', searchQuery.toUpperCase()),
|
||||
where('email', '<=', searchQuery.toUpperCase() + '\uf8ff'),
|
||||
);
|
||||
|
||||
const nameSearchQuery = query(
|
||||
db.users,
|
||||
where('displayName', '>=', searchQuery.toUpperCase()),
|
||||
where('displayName', '<=', searchQuery.toUpperCase() + '\uf8ff'),
|
||||
);
|
||||
|
||||
const emailquerySnap = await getDocs(emaildocSearchQuery);
|
||||
const namequerySnap = await getDocs(nameSearchQuery);
|
||||
|
||||
return [
|
||||
...emailquerySnap.docs.map((doc) => doc.data()),
|
||||
...namequerySnap.docs.map((doc) => doc.data()),
|
||||
];
|
||||
} catch (e) {
|
||||
console.error('Error: ', e);
|
||||
|
||||
throw new Error('Error');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user