-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.js
84 lines (73 loc) · 2.66 KB
/
renderer.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
var config = {
apiKey: "AIzaSyBBoOjpRwVt734Tlp75hGp553p6rQNquQc",
authDomain: "jotplus-97323.firebaseapp.com",
databaseURL: "https://jotplus-97323.firebaseio.com",
projectId: "jotplus-97323",
storageBucket: "jotplus-97323.appspot.com",
messagingSenderId: "968420953358"
}
firebase.initializeApp(config)
document.addEventListener('DOMContentLoaded', () => {
console.log('App loaded')
const notesArea = document.getElementById('notes-area')
const addNoteButton = document.getElementById('add-note-button')
const profileUsername = document.getElementById('profile-username')
const logoutButton = document.getElementById('account-info')
const writingArea = document.getElementById('writing-area')
let userId = null
const ref = firebase.database().ref()
const stRef = firebase.storage().ref()
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
userId = firebase.auth().currentUser.uid
loadUser(userId)
console.log(userId)
loadNotes(userId)
} else {
// No user is signed in.
console.log('No user has signed in.')
}
})
function loadUser(user) {
ref.child('User Data').child(user).child('Username').once('value', (snapshot) => {
profileUsername.innerHTML = snapshot.val()
})
}
addNoteButton.onclick = () => {
writingArea.innerHTML = ''
var project = new Jot(userId)
var date = project.getDate()
writingArea.addEventListener('focus', (evnt) => {
var card = new JotCard(date)
card.addCard(notesArea)
setInterval(() => {
var content = evnt.target.innerText
var title = content.split('\n')[0]
project.setTitle(title)
project.setContent(content)
}, 5000)
})
}
logoutButton.onclick = () => {
var promise = firebase.auth().signOut()
promise.then(() => {
window.location = 'login.html'
})
promise.catch(function (err) {
// Handle errors
})
}
function loadNotes(user) {
ref.child('User Data').child(user).child('Jots').once('value', (snapshot) => {
snapshot.forEach((child) => {
var data = child.val()
console.log(data)
var title = data['TITLE']
var content = data['CONTENT']
var date = data['CREATIONDATE']
var card = new JotCard(title, content, date)
card.addCard(notesArea, writingArea)
})
})
}
})