-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (86 loc) · 2.74 KB
/
index.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
import { Elm } from "./src/Main.elm";
import * as firebase from "firebase/app";
import "firebase/auth";
import "firebase/firestore";
const firebaseConfig = {
apiKey: "AIzaSyAMGfJmtSyqY16dnUW2E7abjuaGYmgJyRw",
authDomain: "doglogger-7554d.firebaseapp.com",
projectId: "doglogger-7554d",
storageBucket: "doglogger-7554d.appspot.com",
messagingSenderId: "62116958650",
appId: "1:62116958650:web:cdb36314e3891345f20d83"
};
firebase.initializeApp(firebaseConfig);
let elm;
firebase.auth().onAuthStateChanged((user) => {
elm = Elm.Main.init({
node: document.getElementById('root'),
flags: { user: user },
replace: false,
});
elm.ports.signIn.subscribe(([username, password]) => {
firebase.auth().signInWithEmailAndPassword(username, password).catch(err => {
elm.ports.signInError.send(err.message);
});
});
elm.ports.signOut.subscribe(() => {
firebase.auth().signOut();
});
elm.ports.pooNow.subscribe(() => {
db.collection('poos').add({ timestamp: +new Date()}).then(() => {
elm.ports.complete.send('complete');
})
});
elm.ports.pooThen.subscribe((timestamp) => {
db.collection('poos').add({ timestamp}).then(() => {
elm.ports.complete.send('complete');
})
});
elm.ports.whoopsNow.subscribe(() => {
db.collection('whoops').add({ timestamp: +new Date()}).then(() => {
elm.ports.complete.send('complete');
})
});
elm.ports.whoopsThen.subscribe((timestamp) => {
db.collection('whoops').add({ timestamp}).then(() => {
elm.ports.complete.send('complete');
})
});
elm.ports.loadCollection.subscribe((name) => {
console.log('getting collection: ', name)
getData(user, name).then(data => {
elm.ports[`received_${name}`].send(data);
})
});
elm.ports.deletePoo.subscribe((id) => {
deleteDoc(user, 'poos', id);
});
elm.ports.deleteWhoops.subscribe((id) => {
deleteDoc(user, 'whoops', id);
});
elm.ports.enterWeight.subscribe(([date, weight]) => {
db.collection('weight').add({ date, weight }).then(() => {
elm.ports.complete.send('complete');
})
});
})
const db = firebase.firestore();
function deleteDoc(user, collection, id) {
return db.collection(collection).doc(id).delete().then(() => getData(user, collection)).then(data => {
elm.ports[`received_${collection}`].send(data);
});
}
function getData(user, collection) {
if(!user) return Promise.resolve([]);
return db.collection(collection).get().then(query => {
const items = [];
query.forEach(item => {
const data = item.data();
items.push({
...data,
id: item.id
});
})
return items;
})
}