Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Firebase reads #203

Merged
merged 6 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ function App() {
.doc(DB_HACKATHON)
.collection('Announcements')
.orderBy('timestamp', 'desc')
.limit(6)
.onSnapshot(querySnapshot => {
// firebase doc that triggered db change event
const changedDoc = querySnapshot.docChanges()[0]
Expand Down
1 change: 1 addition & 0 deletions src/containers/Announcements.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default () => {
.doc(DB_HACKATHON)
.collection('Announcements')
.orderBy('timestamp', 'desc')
.limit(6)
.onSnapshot(querySnapshot => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we reuse the data fetched from src/App.js:L117?

Copy link
Contributor Author

@ianmah ianmah Dec 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's get someone with iOS to test the preview pr to make sure my changes didn't break the homepage before we merge. @acchiang? Can you make sure you still see announcements on the homepage

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just got carmen to test on iOS, looks good

setAnnouncements(Object.values(querySnapshot.docs.map(doc => doc.data())))
})
Expand Down
36 changes: 22 additions & 14 deletions src/containers/Quicklinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ const ButtonContainer = styled.div`
}
}
`
const getLinks = () => {

const getCommonLinks = () => {
return db
.collection(DB_COLLECTION)
.doc(DB_HACKATHON)
.collection('QuickLinks')
.where('common', '==', true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

.orderBy('label')
.get()
.then(querySnapshot => {
Expand All @@ -33,18 +35,12 @@ export const CommonLinks = () => {
const [links, setLinks] = useState([])

useEffect(() => {
getLinks().then(docs => {
// Only keep the common links
const filtered = Object.values(
docs.reduce((result, doc) => {
const data = doc.data()
data.common && result.push(data)
return result
}, [])
)
setLinks(filtered)
})
}, [setLinks])
;(async () => {
const commonLinkDocs = await getCommonLinks()
const commonLinks = commonLinkDocs.map(doc => doc.data())
setLinks(commonLinks)
})()
}, [])

return (
<ButtonContainer>
Expand All @@ -57,11 +53,23 @@ export const CommonLinks = () => {
)
}

const getAllLinks = () => {
return db
.collection(DB_COLLECTION)
.doc(DB_HACKATHON)
.collection('QuickLinks')
.orderBy('label')
.get()
.then(querySnapshot => {
return querySnapshot.docs
})
}

export const QuickLinks = () => {
const [links, setLinks] = useState([])

useEffect(() => {
getLinks()
getAllLinks()
.then(docs => {
// Only keep the uncommon links
return Object.values(
Expand Down