-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fire.js
84 lines (69 loc) · 1.96 KB
/
Fire.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
/*
* Code source provenance de https://github.com/bellifa96/toDoList/blob/main/Fire.js
* dans le but de résoudre l'intégration de Firebase (erreur app/no-app)
*/
import firebase from "firebase"
import '@firebase/firestore'
const firebaseConfig = {
apiKey: "AIzaSyC56I-vmPGWK0XIC7QgxP35jnzChYiq-ls",
authDomain: "todo-leviosa.firebaseapp.com",
projectId: "todo-leviosa",
storageBucket: "todo-leviosa.appspot.com",
messagingSenderId: "973875357327",
appId: "1:973875357327:web:b04403a3e736a66b0a32fa"
}
export default class Fire {
app = undefined
constructor(callback) {
this.init(callback)
}
init(callback) {
if (!firebase.apps.length) {
this.app = firebase.initializeApp(firebaseConfig)
}
firebase.auth().onAuthStateChanged(user => {
if (user) {
callback(null)
} else {
firebase.auth().signInAnonymously().catch(error => {
callback(error)
})
}
})
}
get ref() {
return firebase.firestore(this.app).collection('lists');
}
getLists(callback) {
let ref = this.ref.orderBy('name')
this.unsubscribe = ref.onSnapshot(snapshot => {
let lists = []
snapshot.forEach(doc => {
lists.push({id: doc.id, ...doc.data()})
})
callback(lists)
}, error => {
console.error(error)
})
}
async addList(list) {
const ref = this.ref
await ref.add(list)
}
async deleteList(list) {
const ref = this.ref
await ref.doc(list.id).delete()
}
async updateList(list) {
const ref = this.ref
await ref.doc(list.id).update(list)
}
detach() {
this.unsubscribe()
}
getOneList(id,callback) {
const ref = this.ref.doc(id).onSnapshot(list => {
callback(list.data())
})
}
}