-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
169 lines (133 loc) · 4.27 KB
/
App.js
File metadata and controls
169 lines (133 loc) · 4.27 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React from "react";
import Cart from "./Cart";
import Navbar from "./Navbar";
//importing in all the required functions only ->thats how firebase V9 works
import {initializeApp} from 'firebase/app'
import {
getFirestore,
collection,
getDocs,
onSnapshot,
setDocs,
addDoc,
updateDoc,
doc,
deleteDoc} from 'firebase/firestore'
//this info which is unique to our firebase project
const firebaseConfig = {
apiKey: "AIzaSyDeJ4lbSFGKEHRgMsvyTjn3RGDnsS6JD70",
authDomain: "cart-app-fca2a.firebaseapp.com",
projectId: "cart-app-fca2a",
storageBucket: "cart-app-fca2a.appspot.com",
messagingSenderId: "234435207195",
appId: "1:234435207195:web:8dce9e18999b3b099218be"
};
class App extends React.Component{
constructor(){
super();
this.state={
products:[
//we removed everything coz now we linked firebase to the app na
],
waiting:true
}
//now to make some firebase stuff, so that i can access directly
this.app=initializeApp(firebaseConfig)
this.db=getFirestore(this.app)
this.colref=collection(this.db,'products')
}
//nicely use this boi to take in data from firebase
/* componentDidMount(){
const app=initializeApp(firebaseConfig) //starting the firebase stuff
const db=getFirestore(app) //accessing the firestore database
const colref=collection(db,'products') //touching the collection we need
getDocs(colref).then((snapshot)=>{ // touching the docs we need one at a time, and then
const products=snapshot.docs.map((item)=>{ //taking the data from them into an array, to set to
const data=item.data() //state, and also dont forget that id thingy
data['id']=item.id
return data})
this.setState({
products,
waiting:false
})
})
} */
//that above code runs perfect, but when we change sm in the db, it doesnt update here automatically, and we need
//to refresh every time ->so now were gonna set a listener ->onSnapshot, so we go like
componentDidMount(){
onSnapshot(this.colref, (snapshot)=>{ //the only diff comes in this line, where we use onSnapshot instead of getDocs.
const products=snapshot.docs.map((item)=>{ //this takes 2 paramters ->colref and any function which will run anytime theres a change in db
const data=item.data() //and the function gets a parameter by default ->thats our snapshot from our previous method ->so after this point, its all the same
data['id']=item.id // and since it isnt like some promise, the code is also shorter
return data})
this.setState({
products,
waiting:false
})
})
}
IncreaseQuantity=(item)=>{
const docref=doc(this.colref,item.id)
updateDoc(docref,{
qty:item.qty+1
})
}
DecreaseQuantity=(item)=>{
const docref=doc(this.colref,item.id)
if(item.qty!=0){
updateDoc(docref,{
qty:item.qty-1
})
}
}
DeleteItem=(id)=>{
const docref=doc(this.colref,id)
deleteDoc(docref)
}
CountItems=()=>{
const {products}=this.state
let s=0;
products.forEach((item)=>{
s+=item.qty
})
return s;
}
FindTotal=()=>{
const{products}=this.state
let s=0
products.forEach((item)=>{
s+=item.qty*item.price
})
return s
}
addProduct=()=>{
addDoc(this.colref,{
title:'Washing machine',
price:9999,
qty:1,
img:"https://images.unsplash.com/photo-1626806787461-102c1bfaaea1?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8d2FzaGluZyUyMG1hY2hpbmV8ZW58MHx8MHx8&auto=format&fit=crop&w=500&q=60"
})
}
render(){
const {products,waiting}=this.state
return(
<div>
<Navbar
CountItems={this.CountItems}
/>
{waiting && (<h1>LOADING ....</h1>)}
<button onClick={this.addProduct}>ADD A PRODUCT</button>
<Cart
products={products}
IncreaseQuantity={this.IncreaseQuantity}
DecreaseQuantity={this.DecreaseQuantity}
DeleteItem={this.DeleteItem}
/>
<div className="total">
TOTAL: {this.FindTotal()}
</div>
</div>
)
}
}
export default App;