forked from valeriaszy/Project-One
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
103 lines (88 loc) · 3.43 KB
/
main.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
//Firebase initialize
var config = {
apiKey: "AIzaSyA8kEgnv75jLgZznRXBSpD7FGHd0ulv8Jo",
authDomain: "project-one-7cd8f.firebaseapp.com",
databaseURL: "https://project-one-7cd8f.firebaseio.com",
projectId: "project-one-7cd8f",
storageBucket: "project-one-7cd8f.appspot.com",
messagingSenderId: "147656414734"
};
firebase.initializeApp(config);
var database = firebase.database();
//End Firebase initilize
//Asign value to databse path
var root=database.ref();
//condition to check for user in local storage
if (!localStorage.getItem("geekverse-key")) {//condition 1: new user
var user = database.ref().push(); //to add a new user and save his reference
var userKey = user.key; //to store userkey
localStorage.setItem("geekverse-key",userKey)
} else {
var userKey = localStorage.getItem("geekverse-key");
var user = database.ref("/"+userKey);
}
function clearPreviousSearch() {
//removing current user (also the whole branch)
user.remove().then( function() {
resultNode = document.querySelector("#result-section");
while(resultNode.firstChild) {
resultNode.removeChild(resultNode.firstChild);
}
})
//re-add user
var child = root.push();
child.key = userKey;
}
document.querySelector("#run-search").addEventListener("click", function(event) {
event.preventDefault();
clearPreviousSearch();
var search = document.querySelector("#search-title").value;
console.log(search);
//function for searching movie
searchTitle(search);
//function for search book
searchGoogleBooks(search);
//funcitoin for search comic
searchMarvelApi(search);
})
user.on("child_added",function(childSnap) {// launch call back function to display the firebase data locally)
childData = childSnap.val();
var resultDiv = document.createElement("div");
resultDiv.className = 'card my-1';
resultDiv.setAttribute('data-type',childData.Type);
//for title area
var titleRow = document.createElement("div");
titleRow.className = "card-header";
var titleEl = document.createElement("div");
titleEl.className = "h2";
titleEl.textContent = childData.Title;
titleRow.appendChild(titleEl);
resultDiv.appendChild(titleRow);
//for body area
var bodyRow = document.createElement("div");
bodyRow.className = "card-body d-flex flex-row card-background";
var imgWrapper = document.createElement("div")
imgWrapper.className = "col-md-4";
var imgEl = document.createElement("img");
imgEl.className = "img-fluid rounded-0 m-0";
imgEl.setAttribute("width","100%");
imgEl.setAttribute('src',childData.Poster);
imgWrapper.appendChild(imgEl)
bodyRow.appendChild(imgWrapper);
var textWrapper = document.createElement("div");
textWrapper.className = "col-md-8";
if (childSnap.Type == "comic") {
var dateEl = document.createElement("p")
dateEl.textContent = "Published Date: "+childData.PublishedDate
textWrapper.append(dateEl)
} else {
var summaryEl = document.createElement("p");
summaryEl.textContent ="Summary: " + childData.Plot;
textWrapper.append(summaryEl);
var dateEl = document.createElement("p");
dateEl.textContent = "Year: "+childData.Year;
}
bodyRow.appendChild(textWrapper);
resultDiv.appendChild(bodyRow)
document.querySelector("#result-section").appendChild(resultDiv);
})