-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdashboard.js
More file actions
76 lines (67 loc) · 2.14 KB
/
dashboard.js
File metadata and controls
76 lines (67 loc) · 2.14 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
$(document).ready(function() {
const API_URL = `https://littlehelpers.herokuapp.com/parent/`;
$.ajax({
url: `${API_URL}${localStorage.id}`,
headers:{'Authorization': `Bearer ${localStorage.token}`},
type: 'GET'
})
.then(function(data) {
for(var i =0; i <data[0].length; i++){
var parentSource = $("#parent-template").html();
var parentTemplate = Handlebars.compile(parentSource);
var parentContext = {
"parentName": data[0][0].name
};
$("#parentName").append(parentTemplate(parentContext))
}
var childSource = $("#child-template").html();
var childTemplate = Handlebars.compile(childSource);
for(var i=0; i < data[1].length; i++){
var childContext = {
"childName": data[1][i].name,
"childPoints": data[1][i].points,
"id": data[1][i].id,
"childURL": `/child.html?parent_id=${localStorage.id}&child_id=${data[1][i].id}`
}
$("#children").prepend(childTemplate(childContext))
}
$(document).on("click", "#deleteChildButton", function(){
var deleteChildId = $(this).data("child")
console.log(deleteChildId)
return $.ajax({
url: `${API_URL}${localStorage.id}/${deleteChildId}`,
headers:{'Authorization': `Bearer ${localStorage.token}`},
type: 'DELETE'
})
.then(function(result){
console.log(result)
window.location.reload()
})
});
});
$("#addChild").click(function(event){
event.preventDefault();
var childName = $("#childNameAdd").val();
console.log(childName)
var parent_id = localStorage.id;
return $.ajax({
url: `${API_URL}${localStorage.id}`,
headers:{'Authorization': `Bearer ${localStorage.token}`},
type: 'POST',
data: {name: childName}
})
.then(function(result){
console.log(result)
window.location.reload()
})
});
function logOut(){
$("#logout").click(function(){
console.log('logout')
localStorage.removeItem("id");
localStorage.removeItem("token");
window.location = "/index.html";
});
}
logOut()
});