-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (124 loc) Β· 5.31 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
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
const loggedInLinks = document.querySelectorAll('.logged-in');
const loggedOutLinks = document.querySelectorAll('.logged-out');
const accountDetails = document.querySelector('.account-details');
const logList = document.querySelector('.logs');
const detailArea = document.querySelector('.detail');
// NAVBAR SETUP
const setupUI = (user) => {
if (user) {
const accountDetail = `<div>Logged in as ${user.email}</div>`;
accountDetails.innerHTML = accountDetail;
// NAV BAR
// console.log(loggedInLinks);
loggedInLinks.forEach(item => item.style.display = 'block');
loggedOutLinks.forEach(item => item.style.display = 'none');
} else {
accountDetails.innerHTML = '';
loggedInLinks.forEach(item => item.style.display = 'none');
loggedOutLinks.forEach(item => item.style.display = 'block');
}
};
// DISPLAY CONTENT
const setupGuide = (data) => {
if (data.length) {
let html = '';
let exist_date = {};
// console.log(data);
data.forEach(doc => {
const log = doc.data();
let log_div = ``;
current_date = log.date.mm + log.date.dd + log.date.yyyy;
if (current_date in exist_date) {
log_div = `
<div class="main-log" style="margin-top:50px;" id="${doc.id}" onclick="detailPage(this)" >
<span class="log-category">${log.category}</span>
<div class="log-details">
<h3 class="log-header">${log.title}</h3>
<p class="log-url">${log.url}</p>
<p class="log-body">${log.description}</p>
</div>
</div>
`
} else {
exist_date[current_date] = true;
log_div = `
<h1 class="main-date-header">${log.date.mm} ${log.date.dd}, ${log.date.yyyy}</h1>
<div class="main-log" id="${doc.id}" onclick="detailPage(this)">
<span class="log-category">${log.category}</span>
<div class="log-details">
<h3 class="log-header">${log.title}</h3>
<p class="log-url">${log.url}</p>
<p class="log-body">${log.description}</p>
</div>
</div>
`;
}
html += log_div;
});
logList.innerHTML = html;
} else {
logList.innerHTML = '<h5>Log in to view your log lists</h5>';
}
}
// DISPLAY DETAIL
function detailPage(e) {
console.log('Ready to display detail Page!');
console.log('element', e);
console.log('id', e.id);
let log_data = db.collection('log').doc(e.id);
// Valid options for source are 'server', 'cache', or
// 'default'. See https://firebase.google.com/docs/reference/js/firebase.firestore.GetOptions
// for more information.
let getOptions = {
source: 'cache'
};
log_data.get(getOptions).then(function (doc) {
console.log(doc.data());
const log = doc.data();
detailArea.innerHTML = `
<div class="edit-log button-03" id="edit-log"><a href="#">Edit</a></div>
<span class="log-category">${log.category}</span>
<h1 class="log-header">${log.title}</h1>
<div class="log-details">
<p class="log-url">${log.url}</p>
<li class="log-body">${log.description}</li>
</div>
`;
return log;
}).then((log) => { // EDITING THE LOG
let finishEdit = new Promise(function(resolve, reject) {
document.querySelector('.edit-log').addEventListener('click', function() {
console.log('Edit!!');
detailArea.innerHTML = `
<div class="edit-log button-03" id="edit-log" style="visibility:hidden;"><a href="#">Edit</a></div>
<span class="log-category edit-category">${log.category}</span>
<h1 class="log-header edit-header" contenteditable="true">${log.title}</h1>
<div class="log-details edit-details">
<p class="log-url edit-url" contenteditable="true">${log.url}</p>
<li class="log-body edit-description" contenteditable="true">${log.description}</li>
</div>
<br />
<div class="save-log button-01" id="save-log"><a href="#">Save</a></div>
`;
// Implement forms & functionality here.
// USER
resolve();
});
});
finishEdit.then(() => {
document.querySelector('.save-log').addEventListener('click', function() {
console.log(e.id);
let newTitle = document.querySelector('.edit-header').innerHTML;
let newUrl = document.querySelector('.edit-url').innerHTML;
let newDescription = document.querySelector('.edit-description').innerHTML;
db.collection('log').doc(e.id).update({
title: newTitle,
description: newDescription,
url: newUrl,
updatedAt: firebase.firestore.Timestamp.fromDate(new Date())
});
detailPage(e);
});
});
});
}