-
Notifications
You must be signed in to change notification settings - Fork 1
/
KWdraft1.html
153 lines (125 loc) · 4.62 KB
/
KWdraft1.html
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
<!DOCTYPE html>
<html>
<body>
<h1>Sample Heading</h1>
</body>
<!--DEPENDENCIES:-->
<!-- The core Firebase JS SDK is always required and must be listed first -->
<!-- Insert these scripts at the bottom of the HTML, but before you use any Firebase services -->
<!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.2.6/firebase-app.js"></script>
<!-- If you enabled Analytics in your project, add the Firebase SDK for Analytics -->
<script src="https://www.gstatic.com/firebasejs/8.2.6/firebase-analytics.js"></script>
<!-- Add Firebase products that you want to use -->
<script src="https://www.gstatic.com/firebasejs/8.2.6/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.6/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.6/firebase-database.js"></script>
<script>
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
var firebaseConfig = {
apiKey: "AIzaSyBogUrOkPpYTQqRWYgmPcCttsIuBiBN1zY",
authDomain: "kindwords-c394e.firebaseapp.com",
databaseURL: "https://kindwords-c394e-default-rtdb.firebaseio.com",
projectId: "kindwords-c394e",
storageBucket: "kindwords-c394e.appspot.com",
messagingSenderId: "343957683016",
appId: "1:343957683016:web:c67f812e534d9b8b86117d",
measurementId: "G-48H1K6KYQ0"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
// Get a reference to the database service
var database = firebase.database();
myStorage = window.localStorage; //yes im using localstorage because im lazy
var myKey;
var myReq;
//Clear (delete) current request
function clearReq()
{
myKey = null;
myReq = null;
myStorage.clear();
//TODO: clear all responses
}
//Initialize "need help" interface (on page load)
//A user's request is stored client-side on his/her computer
function initReq()
{
myKey = myStorage.key;
myReq = myStorage.str;
//TODO: Disply myReq, with HTML
getResponses(myKey);
}
//Initialize "help others" interface (on page load)
function initHelp()
{
getPosts();
}
//Generate a "NEED HELP" message, along with a key that points to the message
//Parameters:
//str- "NEED HELP" request message
function writeNewPost(str)
{
//TODO: implement this on the page
var postData = {req: str,};
// Get a key for a new Post.
var newPostKey = firebase.database().ref().child('posts').push().key;
// Write the new post's data in the posts list
var updates = {};
updates['/posts/' + newPostKey] = postData;
firebase.database().ref().update(updates);
localStorage.setItem('key', newPostKey); //save the key
localStorage.setItem('msg', str); //save the message
}
//Retrieve ALL request posts from the database, DOES NOT retrieve response posts
function getPosts()
{
var numberOfRequests = 0;
var leadRef = firebase.database().ref('posts');
leadRef.on("value", function(snapshot)
{
snapshot.forEach(function(childSnapshot)
{
var childData = childSnapshot.val();
numberOfRequests++;
var requestStr = childData.req; // HOLDS "need help" messages
var requestKey = Object.keys(snapshot.val())[numberOfRequests-1]; // HOLDS key to the above "need help message"
console.log(requestKey);
console.log(requestStr);
});
});
}
//Generate a response token to a request, given its key
//Parameters:
//key- request key, accessed using getPosts() -> requestKey,
//str- response message
function writeResponse(key, str)
{
var newres = firebase.database().ref('posts/' + key).push();
newres.set(str);
//TODO: implement this on the page
}
//Retrieves ALL response posts, given the key to the request post
//Parameters:
//key- request key, accessed using getPosts() -> requestKey,
function getResponses(key)
{
var leadRef = firebase.database().ref('posts/' + key);
leadRef.on("value", function(snapshot)
{
var ctr = 0;
snapshot.forEach(function(childSnapshot)
{
var responseStr = childSnapshot.val(); //Retrieves responses
var responseKey = Object.keys(snapshot.val())[ctr]; //Retrieves the key to that^ response
if (responseKey == "req") return;
console.log(responseStr);
//TODO: display each response using HTML on the page
ctr++;
});
});
}
</script>
</html>