scrimba
Frontend Career Path
Advanced React
React Router
Final loose ends
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

AboutCommentsNotes
Final loose ends
Expand for more info
api.js
run
preview
console
import { initializeApp } from "firebase/app"
import {
getFirestore,
collection,
doc,
getDocs,
getDoc,
query,
where,
documentId
} from "firebase/firestore/lite"

const firebaseConfig = {
apiKey: "AIzaSyD_k3v3HK3tKEqhlqFHPkwogW7PqEqhGhk",
authDomain: "vanlife-a1af5.firebaseapp.com",
projectId: "vanlife-a1af5",
storageBucket: "vanlife-a1af5.appspot.com",
messagingSenderId: "803007000356",
appId: "1:803007000356:web:446cd3a1ca406839258db1"
};

const app = initializeApp(firebaseConfig)
const db = getFirestore(app)

// Refactoring the fetching functions below
const vansCollectionRef = collection(db, "vans")

export async function getVans() {
const snapshot = await getDocs(vansCollectionRef)
const vans = snapshot.docs.map(doc => ({
...doc.data(),
id: doc.id
}))
return vans
}

export async function getVan(id) {
const docRef = doc(db, "vans", id)
const snapshot = await getDoc(docRef)
return {
...snapshot.data(),
id: snapshot.id
}
}

export async function getHostVans() {
const q = query(vansCollectionRef, where("hostId", "==", "123"))
const snapshot = await getDocs(q)
const vans = snapshot.docs.map(doc => ({
...doc.data(),
id: doc.id
}))
return vans
}

/*
This 👇 isn't normally something you'd need to do. Instead, you'd
set up Firebase security rules so only the currently logged-in user
could edit their vans.

https://firebase.google.com/docs/rules

I'm just leaving this here for educational purposes, as it took
me a while to find the `documentId()` function that allows you
to use a where() filter on a document's ID property. (Since normally
it only looks at the data() properties of the document, meaning you
can't do `where("id", "==", id))`

It also shows how you can chain together multiple `where` filter calls
*/

// export async function getHostVan(id) {
// const q = query(
// vansCollectionRef,
// where(documentId(), "==", id),
// where("hostId", "==", "123")
// )
// const snapshot = await getDocs(q)
// const vans = snapshot.docs.map(doc => ({
// ...doc.data(),
// id: doc.id
// }))
// return vans[0]
// }

export async function loginUser(creds) {
const res = await fetch("/api/login",
{ method: "post", body: JSON.stringify(creds) }
)
const data = await res.json()

if (!res.ok) {
throw {
message: data.message,
statusText: res.statusText,
status: res.status
}
}

return data
}
Console
/host/vans/4
-1:34