-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
70 lines (60 loc) · 1.62 KB
/
api.js
File metadata and controls
70 lines (60 loc) · 1.62 KB
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
import { initializeApp } from "firebase/app";
import {
getFirestore,
collection,
doc,
getDocs,
getDoc,
query,
where
} from "firebase/firestore/lite"
const firebaseConfig = {
apiKey: "AIzaSyAjki6Mm9_9A_8X4ztG3DB8QLoqlNvQYGY",
authDomain: "van-life-38058.firebaseapp.com",
projectId: "van-life-38058",
storageBucket: "van-life-38058.firebasestorage.app",
messagingSenderId: "530020513830",
appId: "1:530020513830:web:071ebfb192131a5ce70ee7"
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app)
const vansCollectionRef = collection(db, "vans")
export async function getVans() {
const querySnapshot = await getDocs(vansCollectionRef)
const dataArr = querySnapshot.docs.map(doc => ({
...doc.data(),
id: doc.id
}))
return dataArr
}
export async function getVan(id) {
const docRef = doc(db, "vans", id)
const vanSnapshot = await getDoc(docRef)
return {
...vanSnapshot.data(),
id: vanSnapshot.id
}
}
export async function getHostVans() {
const q = query(vansCollectionRef, where("hostId", "==", "123"))
const querySnapshot = await getDocs(q)
const dataArr = querySnapshot.docs.map(doc => ({
...doc.data(),
id: doc.id
}))
return dataArr
}
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
}