Skip to content

Commit 5a0ebb1

Browse files
authored
Merge pull request #349 from bruin-tennis-consulting/318-website-ux
318 website ux
2 parents 47391e9 + 9f0ddb9 commit 5a0ebb1

11 files changed

Lines changed: 471 additions & 205 deletions

File tree

app/AuthWrapper.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
'use client'
2-
import React, {
3-
createContext,
4-
useContext,
5-
useEffect,
6-
useState,
7-
useMemo
8-
} from 'react'
9-
import { onAuthStateChanged, signOut } from 'firebase/auth'
102

3+
import { createContext, useContext, useEffect, useState, useMemo } from 'react'
4+
import { onAuthStateChanged, signOut } from 'firebase/auth'
115
import { auth } from '@/app/services/initializeFirebase'
126
import { getUserProfile } from '@/app/services/userInfo'
137
import LandingPage from '@/app/components/LandingPage'
148
import Loading from './components/Loading'
9+
import styles from '@/app/styles/Navbar.module.css'
10+
1511
const AuthContext = createContext()
1612

1713
export const AuthProvider = ({ children }) => {
@@ -36,14 +32,6 @@ export const AuthProvider = ({ children }) => {
3632

3733
const memoizedUserProfile = useMemo(() => userProfile, [userProfile])
3834

39-
if (loading) {
40-
return (
41-
<div>
42-
<Loading prompt={'Logging In...'} />
43-
</div>
44-
)
45-
}
46-
4735
const handleSignOut = () => {
4836
signOut(auth)
4937
.then(() => {
@@ -55,6 +43,21 @@ export const AuthProvider = ({ children }) => {
5543
})
5644
}
5745

46+
if (loading) {
47+
return (
48+
<div>
49+
<div className={styles.container} style={{ marginBottom: '100px' }}>
50+
<header className={styles.header}>
51+
<div className={styles.titleBar}>
52+
<h1 className={styles.noUnderline}>BSA | Tennis Consulting</h1>
53+
</div>
54+
<Loading prompt={'Logging In...'} />
55+
</header>
56+
</div>
57+
</div>
58+
)
59+
}
60+
5861
return (
5962
<div style={{ width: '100%' }}>
6063
<AuthContext.Provider

app/DataProvider.js

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ import React, {
66
useCallback,
77
useContext
88
} from 'react'
9-
import { collection, getDocs, doc, updateDoc, addDoc } from 'firebase/firestore'
9+
import {
10+
collection,
11+
getDocs,
12+
doc,
13+
updateDoc,
14+
addDoc,
15+
query,
16+
where
17+
} from 'firebase/firestore'
1018
import { ref, uploadBytes, getDownloadURL } from 'firebase/storage'
1119

1220
import { db, storage } from '@/app/services/initializeFirebase.js'
@@ -31,36 +39,38 @@ export const DataProvider = ({ children }) => {
3139

3240
const { userProfile } = useAuth()
3341

42+
// Optmized fetchMatches version
3443
const fetchMatches = useCallback(async () => {
35-
if (userProfile && userProfile.collections) {
36-
setLoading(true)
37-
setError(null)
38-
const allMatches = []
44+
if (!userProfile?.collections?.length) return
3945

40-
try {
41-
for (const col of userProfile.collections) {
42-
const colRef = collection(db, col)
43-
const querySnapshot = await getDocs(colRef)
44-
45-
querySnapshot.docs.forEach((doc) => {
46-
const matchData = doc.data()
47-
// Only add matches that are not marked as deleted
48-
if (!matchData._deleted) {
49-
allMatches.push({
50-
id: doc.id,
51-
collection: col, // Track which collection this match belongs to
52-
...matchData
53-
})
54-
}
55-
})
56-
}
46+
setLoading(true)
47+
setError(null)
5748

58-
setMatches(allMatches)
59-
} catch (err) {
60-
setError(err)
61-
} finally {
62-
setLoading(false)
63-
}
49+
try {
50+
// Create all promises at once instead of awaiting each one sequentially
51+
const collectionPromises = userProfile.collections.map(async (col) => {
52+
const colRef = collection(db, col)
53+
const filteredQuery = query(colRef, where('_deleted', '==', false))
54+
const querySnapshot = await getDocs(filteredQuery)
55+
56+
// Process documents in bulk rather than in forEach
57+
return querySnapshot.docs.map((doc) => ({
58+
id: doc.id,
59+
collection: col,
60+
...doc.data()
61+
}))
62+
})
63+
64+
// Wait for all promises to resolve
65+
const matchesArrays = await Promise.all(collectionPromises)
66+
67+
// Flatten the array of arrays
68+
setMatches(matchesArrays.flat())
69+
} catch (err) {
70+
console.error('Error fetching matches:', err)
71+
setError(err)
72+
} finally {
73+
setLoading(false)
6474
}
6575
}, [userProfile])
6676

@@ -110,6 +120,7 @@ export const DataProvider = ({ children }) => {
110120
}
111121
console.log(pdfUrl)
112122
newMatchData.pdfFile = pdfUrl
123+
newMatchData._deleted = false
113124

114125
const newMatch = {
115126
id: 'temp-id',

0 commit comments

Comments
 (0)