-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
80 lines (67 loc) · 1.99 KB
/
api.js
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
70
71
72
73
74
75
76
77
78
79
80
import { initializeApp } from "firebase/app";
import { getFirestore, collection, doc, getDocs, getDoc, query } from "firebase/firestore/lite"
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyB0aDJqerAyxI9QMePKD1NgiOUc8VOFojk",
authDomain: "van-life-6e4b2.firebaseapp.com",
projectId: "van-life-6e4b2",
storageBucket: "van-life-6e4b2.appspot.com",
messagingSenderId: "34009423152",
appId: "1:34009423152:web:1c7540afd4321d6fa83cf5"
};
// Initialize Firebase
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 getHostVans(id) {
// const url = id ? `/api/host/vans/${id}` : "/api/host/vans"
// const res = await fetch(url)
// if (!res.ok) {
// throw {
// message: "Failed to fetch vans",
// statusText: res.statusText,
// status: res.status
// }
// }
// const data = await res.json()
// return data.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
}