-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from brown-ccv/firebase-pagination
Firebase pagination
- Loading branch information
Showing
10 changed files
with
886 additions
and
53 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
serviceAccount.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { existsSync } from 'fs'; | ||
|
||
import admin from 'firebase-admin'; | ||
import { applicationDefault } from 'firebase-admin/app'; | ||
// import { Timestamp } from 'firebase-admin/firestore'; | ||
|
||
import { createPublicationTokens } from '../src/utils/firebase'; | ||
|
||
const SERVICE_ACCOUNT_PATH = './scripts/serviceAccount.json'; | ||
|
||
type DB = ReturnType<(typeof admin)['firestore']>; | ||
type Pubs = Awaited<ReturnType<typeof getPubs>>; | ||
|
||
function setup() { | ||
if (existsSync(SERVICE_ACCOUNT_PATH)) { | ||
process.env.GOOGLE_APPLICATION_CREDENTIALS = SERVICE_ACCOUNT_PATH; | ||
console.log('Using Service Account Json'); | ||
} else { | ||
console.log('Unable to find Service Account Json. Aborting...'); | ||
process.exit(-1); | ||
} | ||
|
||
admin.initializeApp({ credential: applicationDefault(), projectId: 'ccv-pubs' }); | ||
const db = admin.firestore(); | ||
return { admin, db }; | ||
} | ||
|
||
async function getPubs(db: DB) { | ||
const moduleDocs = await db.collection('publications').get(); | ||
if (moduleDocs.empty) { | ||
console.error('No publications found.'); | ||
return []; | ||
} | ||
return moduleDocs.docs; | ||
} | ||
|
||
async function updatePubsWithTokens(db: DB, pubs: Pubs) { | ||
await Promise.all( | ||
pubs.map((pub) => { | ||
const pubData = pub.data() as { title: string; author: string; updatedAt: number }; | ||
const newData = { | ||
...pubData, | ||
//updatedAt: Timestamp.fromDate(new Date(pubData.updatedAt)), | ||
tokens: createPublicationTokens(pubData), | ||
}; | ||
return db.doc(`publications/${pub.id}`).set(newData); | ||
}) | ||
); | ||
} | ||
|
||
async function main() { | ||
const { db } = setup(); | ||
const pubs = await getPubs(db); | ||
console.log('fetched', pubs.length, 'publications'); | ||
await updatePubsWithTokens(db, pubs); | ||
console.log('Done.'); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
import React from 'react'; | ||
import { BrowserRouter, Route, Routes } from 'react-router-dom'; | ||
|
||
import { Navbar } from './components/react-ccv-components/Navbar.tsx'; | ||
import Footer from './components/react-ccv-components/Footer'; | ||
|
||
import { ContentPage } from './components/ContentPage'; | ||
import { useAuthStateChanged, usePublicationsCollection } from './utils/firebase.ts'; | ||
import { PublicationsProvider } from './utils/PublicationsContext.tsx'; | ||
import { useAuthStateChanged } from './utils/firebase.ts'; | ||
|
||
export function App() { | ||
useAuthStateChanged(); | ||
usePublicationsCollection(); | ||
|
||
return ( | ||
<div aria-live="polite"> | ||
<Navbar /> | ||
<BrowserRouter> | ||
<main className="main"> | ||
<Routes> | ||
<Route exact path="/" element={<ContentPage />} /> | ||
</Routes> | ||
</main> | ||
</BrowserRouter> | ||
<Footer /> | ||
</div> | ||
<PublicationsProvider> | ||
<div aria-live="polite"> | ||
<Navbar /> | ||
<BrowserRouter> | ||
<main className="main"> | ||
<Routes> | ||
<Route exact path="/" element={<ContentPage />} /> | ||
</Routes> | ||
</main> | ||
</BrowserRouter> | ||
<Footer /> | ||
</div> | ||
</PublicationsProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.