-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (61 loc) · 2.53 KB
/
app.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
var firebaseConfig = {
apiKey: "AIzaSyCXgKtXa6qtRXp4ONGCBYPFZ91wtPhU5H8",
authDomain: "train-scheduler-e2bd6.firebaseapp.com",
databaseURL: "https://train-scheduler-e2bd6.firebaseio.com",
projectId: "train-scheduler-e2bd6",
storageBucket: "",
messagingSenderId: "959007788516",
appId: "1:959007788516:web:69664dfd1c1d0245bd0bf1"
};
firebase.initializeApp(firebaseConfig);
var database = firebase.database();
var name = "";
var destination = "";
var firstTrain = "";
var frequency = 0;
var nextTrain = "";
$("#submit-info").on("click", function(event){
event.preventDefault();
name = $("#name-input").val().trim();
destination = $("#destination-input").val().trim();
firstTrain = $("#first-input").val().trim();
frequency = $("#frequency-input").val().trim();
database.ref().push({
name: name,
destination: destination,
firstTrain: firstTrain,
frequency: frequency,
});
});
database.ref().on("child_added", function(snapshot){
$("#name-input").text(snapshot.val().name);
$("#destination-input").text(snapshot.val().role);
$("#first-input").text(snapshot.val().startDate);
$("#frequency-input").text(snapshot.val().monthlyRate);
$("#name-input").val("");
$("#destination-input").val("");
$("#first-input").val("");
$("#frequency-input").val("");
});
database.ref().on("child_added", function(childSnapshot) {
var firstTimeConverted = moment(childSnapshot.val().firstTrain, "HH:mm").subtract(1, "years");
var diffTime = moment().diff(moment(firstTimeConverted), "minutes");
var tRemainder = diffTime % childSnapshot.val().frequency;
var tMinutesTillTrain = childSnapshot.val().frequency - tRemainder;
var nextTrain = moment().add(tMinutesTillTrain, "minutes");
$("#schedule").append("<tr><td><span class='member-name'> " +
childSnapshot.val().name +
" </td>" + "<td><span class='member-destination'> " + childSnapshot.val().destination +
" </td>" + "<td><span class='member-frequency'> " + childSnapshot.val().frequency +
" </td>" + "<td> " + moment(nextTrain).format("HH:mm") +
" </td>" + "<td> " + tMinutesTillTrain +
" </span></td></tr>");
}, function(errorObject) {
console.log("Errors handled: " + errorObject.code);
});
database.ref().orderByChild("dateAdded").limitToLast(1).on("child_added", function(snapshot) {
$("#name-display").text(snapshot.val().name);
$("#frequency-display").text(snapshot.val().frequency);
$("#destination-display").text(snapshot.val().destination);
$("#firstTrain-display").text(snapshot.val().firstTrain);
});