-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.jsx
69 lines (60 loc) · 1.56 KB
/
api.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { initializeApp } from "firebase/app"
import {
getFirestore,
collection,
doc,
getDocs,
getDoc,
query,
where,
} from "firebase/firestore/lite"
const firebaseConfig = {
apiKey: "AIzaSyDhbk6hYM399Vj5geqgKT7u5Jjnhv5RHAM",
authDomain: "vanlife-2e444.firebaseapp.com",
projectId: "vanlife-2e444",
storageBucket: "vanlife-2e444.appspot.com",
messagingSenderId: "760078215341",
appId: "1:760078215341:web:07d1a61b48ca7465877e24"
};
const app = initializeApp(firebaseConfig)
const db = getFirestore(app)
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
}
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
}