-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·457 lines (364 loc) · 12.7 KB
/
app.js
File metadata and controls
executable file
·457 lines (364 loc) · 12.7 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
const list = document.querySelector('#list');
const form = document.querySelector('#add-form');
const user = "annie101101046@gmail.com";
// db.collection("user").doc(user).get()
// 先拿到user的資料 把invited render出來
// 做一個 btn 接受邀請
// 拿 data
db.collection('users').get().then((snapshot)=>{
snapshot.docs.forEach(doc =>{
console.log(doc)
if(doc.exists) {
const data = doc.data()
if(data.email === user) {
for(const inviter of data.invited) {
console.log(data)
inviteList = document.getElementById('inviteList');
inviting = document.createElement('div');
inviting.innerText = inviter;
acceptbtn = document.createElement('button');
acceptbtn.innerText = 'Add Friend';
acceptbtn.onclick = (e) => {
agreeFriend(inviter, user);
e.target.style.display = 'none';
}
inviteList.appendChild(inviting)
inviteList.appendChild(acceptbtn)
}
}
}
})
})
var users = db.collection("users");
// db.collection('users').doc(form.email.value).set({
// name: form.name.value,
// email: form.email.value,
// userId: form.userId.value,
// invite: ["test@test.com", ],
// // (if invite > 0) 去找對方的 email值(email要調換成 firebase 給的 id)
// // 當 invite accepted,把對方的 email 值從 invite 處刪掉
// invited:["test@test.com",],
// //(if invited > 0) 去找對方的 email 值, if 接受,從 [] 刪掉
// friendList:["test@test.com",],
// //秀出目前接受與被接受的 invite
// });
//拿到 email
//citiesRef.doc(email).get().then(sentInvite);
function SentInvite(){
//invite
let friends = document.getElementById("search").value;
console.log(friends)
// A 加 B 好友
db.collection("users").doc(user).get().then(function(doc){
//確認資料有沒有拿出來
if(doc.exists){
//更新 A
const data = doc.data();
data.inviting.push(friends);
let inviting = data.inviting;
//更新回去
db.collection("users").doc(user).update({
inviting: inviting
});
}
});
db.collection("users").doc(friends).get().then(function(doc){
//確認資料有沒有拿出來
if(doc.exists){
//更新 B
const data = doc.data();
data.invited.push(user);
let invited = data.invited;
//更新回去
db.collection("users").doc(friends).update({
invited: invited
});
}
});
}
//有邀請後,同意加好友、好友增加
//資料拿出來
function agreeFriend(inviter, me){
db.collection("users").doc(me).get().then(function(doc){
//確認資料有沒有拿出來
if(doc.exists){
//更新 inviter
const data = doc.data();
data.friends.push(inviter);
const friends = data.friends;
const invited = data.invited.filter(item => item != inviter);
//更新回去
db.collection("users").doc(me).update({
friends: friends,
invited: invited
});
}
});
//更新 me user
db.collection("users").doc(inviter).get().then(function(doc){
//確認資料有沒有拿出來
if(doc.exists){
//更新 me
const data = doc.data();
data.friends.push(me);
const friends = data.friends;
const inviting = data.inviting.filter(item => item != me)
//更新回去
db.collection("users").doc(inviter).update({
friends: friends,
inviting: inviting
});
}
});
}
//點擊送出好友
document.getElementById("sent").onclick = function(e) {
console.log(e)
SentInvite()
};
// 點擊 search
// function getData(){
// db.collection("users").doc(user).get().then(function(doc){
// //確認資料有沒有拿出來
// if(doc.exists){
// renderList(doc);
// }
// }
// )}
// create element & render
// function renderList(doc){
// let li = document.createElement('li');
// let email = document.createElement('span');
// let invited = document.createElement('span');
// let friendList = document.createElement('span');
// let cross = document.createElement('div');
// let list = document.getElementById('list');
// li.setAttribute('data-id', doc.id);
// name.textContent = doc.data().name;
// email.textContent = doc.data().email;
// userId.textContent = doc.data().userId;
// invited.textContent = doc.data().invited;
// invite.textContent = doc.data().invite;
// friendList.textContent=doc.data().textContent;
// cross.textContent = 'x';
// li.appendChild(name);
// li.appendChild(email);
// li.appendChild(userId);
// li.appendChild(invite);
// li.appendChild(invited);
// li.appendChild(friendList);
// li.appendChild(cross);
// list.appendChild(li);
// // saving data
// form.addEventListener('submit', (e) => {
// e.preventDefault();
// real-time listener
//要判斷是否有邀請(要有 pending 的狀況)
// db.collection('users').orderBy('email').onSnapshot(snapshot => {
// let changes = snapshot.docChanges();
// changes.forEach(change => {
// console.log(change.doc.data());
// if(change.type == 'added'){
// renderList(change.doc);
// } else if (change.type == 'removed'){
// let li = list.querySelector('[data-id=' + change.doc.id + ']');
// list.removeChild(li);
// }
// });
// });
//送出邀請 function
//做個邀請 button
// A 加 B 好友
// db.collection("users").doc("a@test.com").get().then(function(doc){
// //確認資料有沒有拿出來
// if(doc.exists){
// //更新 A
// let invite = doc.data()["invite"].push("b@test.com")
// //更新回去
// db.collection("users").doc("a@test.com").update({
// invite: invite
// })
// }
// })
// db.collection("users").doc("b@test.com").get().then(function(doc){
// //確認資料有沒有拿出來
// if(doc.exists){
// //更新 B
// let invited = doc.data()["invited"].push("a@test.com")
// //更新回去
// db.collection("users").doc("b@test.com").update({
// invited: invited
// })
// }
// })
// let post = {
// "article_id":"",
// "article_title":"",
// "article":"",
// "content":"",
// "article_tag":"Beauty",
// "author":"",
// "created_time":""
// }
//post
//db.collection("post").add(post);
//選到某人後的 tag research
//當 input 有值時,撈到該 user 相對應的 tag 文章,然後 render 出來
// 沒有撈到就沒畫面
var selectTag = undefined;
// search specify tag
const tags = ["Beauty", "Gossiping", "Joke", "SchoolLife"];
let defaultTag = "Beauty";
var citiesRef = db.collection("article");
var postList = document.querySelector('#list');
var searchArticle = document.getElementById('article-search')
var searchSubmit = document.getElementById('searchSubmit')
// Create a query against the collection.
var query = citiesRef.where("article_tag", "==", defaultTag);
query.get().then( result => result.docs )
.then(docs => docs.forEach(doc => renderPost(doc)));
const tagEle = document.getElementById("tags");
for(let i=0; i < tags.length; i++) {
tagBtn = document.createElement('button');
tagBtn.innerText = tags[i];
tagBtn.setAttribute('tagName', tags[i]);
tagBtn.onclick = (e) => {
var t = e.target.getAttribute('tagName');
selectTag = t;
console.log(t)
var q = citiesRef.where("article_tag", "==", t);
q.get().then( result => result.docs )
.then(docs => {
while (postList.firstChild) {
postList.removeChild(postList.firstChild);
}
docs.forEach(doc => {renderPost(doc)})
});
}
tagEle.appendChild(tagBtn);
}
searchSubmit.onclick = (e) => {
const userEmail = searchArticle.value;
while (postList.firstChild) {
postList.removeChild(postList.firstChild);
}
if(selectTag === undefined){
var query = citiesRef.where("author", "==", userEmail);
console.log(query);
query.get().then( result => result.docs)
.then(docs => docs.forEach(doc => renderPost(doc)));
}else{
var query = citiesRef.where("author", "==", userEmail).where("article_tag", "==", selectTag)
console.log(query);
query.get().then( result => result.docs)
.then(docs => docs.forEach(doc => renderPost(doc)));
}
}
// create element & render post
function renderPost(post){
let li = document.createElement('li');
let article_id = document.createElement('span');
let article_title = document.createElement('span');
let article = document.createElement('span');
let cross = document.createElement('div');
li.setAttribute('data-id', post.id);
article_id.textContent = post.data().article_id;
article_title.textContent = post.data().article_title;
cross.textContent = 'x';
li.appendChild(article_id);
li.appendChild(article_title);
li.appendChild(cross);
postList.appendChild(li);
// deleting data
cross.addEventListener('click', (e) => {
e.stopPropagation();
let id = e.target.parentElement.getAttribute('data-id');
db.collection('post').doc(id).delete();
});
}
//留言寫完 submit
document.getElementById("submitYa").onclick = function(e) {
e.preventDefault();
var article_id = document.getElementById('article_id').value;
var article_title = document.getElementById('article_title').value;
var article = document.getElementById('article').value;
var content = document.getElementById('content').value;
var article_tag = document.getElementById('article_tag').value;
db.collection("article").add({
"article_id": article_id,
"article_title": article_title,
"article": article,
"content": content,
"article_tag": article_tag,
"author": user,
"created_time": Date().toString()
});
};
// getting data
// db.collection('cafes').orderBy('city').get().then(snapshot => {
// snapshot.docs.forEach(doc => {
// renderCafe(doc);
// });
// });
// saving data
form.addEventListener('submit', (e) => {
e.preventDefault();
db.collection('article').add({
article_tag: form.article_id.value,
article_title: form.article_title.value
});
form.article_id.value = '';
form.article_title.value = '';
});
// real-time listener
// db.collection('post').orderBy('article_title').onSnapshot(snapshot => {
// let changes = snapshot.docChanges();
// changes.forEach(change => {
// console.log(change.doc.data());
// if(change.type == 'added'){
// renderList(change.doc);
// } else if (change.type == 'removed'){
// let li = cafeList.querySelector('[data-id=' + change.doc.id + ']');
// cafeList.removeChild(li);
// }
// });
// });
//發文
//做一個 post 的 collection
// 每個 post 會有
// {
// article_id:
// article_title:
// article:content:
// article_tag: (Beauty / Gossiping / Joke/ SchoolLife)
// author:
// created_time:
// }
//
//搜尋功能
// 去撈 article_tag:
// 去撈 特定好友的 document
// 去撈 特定好友的 document + tag
// updating records (console demo)
// db.collection('cafes').doc('DOgwUvtEQbjZohQNIeMr').update({
// name: 'mario world'
// });
// db.collection('cafes').doc('DOgwUvtEQbjZohQNIeMr').update({
// city: 'hong kong'
// });
// setting data
// db.collection('cafes').doc('DOgwUvtEQbjZohQNIeMr').set({
// city: 'hong kong'
// });
// //getting data
// db.collection('cafes').orderBy('city').get().then(snapshot => {
// snapshot.docs.forEach(doc => {
// renderCafe(doc);
// });
// });
// // deleting data
// cross.addEventListener('click', (e) => {
// e.stopPropagation();
// let id = e.target.parentElement.getAttribute('data-id');
// db.collection('users').doc(id).delete();
// });