-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirebase.js
111 lines (91 loc) · 3.77 KB
/
firebase.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// see: https://firebase.google.com/docs/web/setup
import firebase from "firebase/app"
import "firebase/auth"
import "firebase/firestore"
const firebaseConfig = {
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_FIREBASE_DATABASE_URL,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_FIREBASE_APP_ID
}
//
// FIREBASE AUTH APP CONFIG
//
// https://firebase.google.com/docs/web/setup
// https://firebase.google.com/docs/reference/js/firebase
// https://firebase.google.com/docs/reference/js/firebase.auth
const app = firebase.initializeApp(firebaseConfig)
export default app
//
// FIRESTORE DATABASE CONFIG
//
// https://firebase.google.com/docs/reference/js/firebase
// https://firebase.google.com/docs/reference/js/firebase.firestore.Firestore
// https://firebase.google.com/docs/firestore/quickstart
const db = firebase.firestore(app)
export async function fetchProducts() {
console.log("FETCHING PRODUCTS...")
// https://googleapis.dev/nodejs/firestore/latest/CollectionReference.html#get
const docs = await db.collection("products").get()
console.log("DOCS:", docs.size)
// https://googleapis.dev/nodejs/firestore/latest/QuerySnapshot.html
// instead of returning the products as documents with separate ids and data
// let's create a single object with both the id and the data
// to make them easier to process and loop through later
var products = []
docs.forEach((doc) => {
//console.log("DOC ID:", doc.id, "DATA", doc.data())
var product = doc.data() // create a new object with the product info
product["id"] = doc.id // merge the id with the object
products.push(product)
})
//console.log("PRODUCTS:", products.length)
return products
}
export async function orderProduct(user, product) {
//console.log("ORDERING PRODUCT")
//console.log("USER:", user.uid, user.email)
//console.log("PRODUCT:", product.id, product.name, product.price)
//var newOrder = {
// "productId": product.id,
// "userUid": user.uid,
// "orderAt": Date.now().toFixed()
//} // consider saving the entire user and product info (more convenient, but less efficient from a storage standpoint)
var newOrder = {
"userUid": user.uid,
"userEmail": user.email,
"productId": product.id,
"productName": product.name,
"productPrice": product.price,
"orderAt": Date.now().toFixed()
}
console.log("NEW ORDER", newOrder)
// https://firebase.google.com/docs/database/admin/save-data
// https://googleapis.dev/nodejs/firestore/latest/CollectionReference.html
var ordersRef = db.collection("orders")
//console.log("ORDERS COLLECTION", ordersRef)
await ordersRef.add(newOrder)
return newOrder
}
export async function fetchOrders(user) {
console.log("FETCHING ORDERS")
console.log("USER:", user.uid, user.email)
// https://firebase.google.com/docs/firestore/query-data/queries
const docs = await db.collection("orders").where('userUid', '==', user.uid).get()
console.log("DOCS:", docs.size)
// instead of returning the documents with separate ids and data,
// ... let's create a single object with both the id and the data
// ... to make them easier to process and loop through later
var orders = []
docs.forEach((doc) => {
//console.log("DOC ID:", doc.id, "DATA", doc.data())
var order = doc.data()
order["id"] = doc.id
orders.push(order)
})
console.log("ORDERS:", orders.length)
return orders
}