-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
214 lines (180 loc) · 4.85 KB
/
index.html
File metadata and controls
214 lines (180 loc) · 4.85 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
<html>
<head>
<title>Angular Application</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<!-- This represents our API -->
<script>
var users = {'jeremy':'password',
'bob' :'password2',
'jeff' :'password3'}
var entries = [{
person: 'jeremy',
food: 'Pizza',
calories: '500'
},
{
person: 'jeremy',
food: 'French Fries',
calories: '300'
},
{
person: 'jeremy',
food: 'Carrots',
calories: '50'
},
{
person: 'jeff',
food: 'Apples',
calories: '800'
}]
function getEntries(){
return entries
}
//New pseudo API function to filter on username
function getEntriesForUser(username){
//Old code
//return entries
//new list
var toRet = []
//New Code, actually filter
for (var i = 0; i<entries.length; i++){
if (entries[i].person == username){
toRet.push(entries[i])
}
}
//Return the final list
return toRet
}
//Pseudo API endpoint for adding a entry
function addEntry(user, food, calories){
//take args, and insert them into the entries list
var jobj = {person: user,
food: food,
calories: calories}
entries.push(jobj)
}
function logIn(username, password){
if (users[username] == password){
return true;
} else {
return false;
}
}
//pseudo API function for register
function registerUser(username, password){
/*
var jobj = {
username: username,
password: password
}
users.push()
*/
//Here is where we should check to see if th eusername is already used
users[username] = password
}
</script>
</head>
<body ng-app="mainApp" ng-controller="myCtrl">
<div class="container">
<div class="row">
<!-- Where form used to be -->
<!--<p><a href="#/!">Main</a></p>
<a href="#!dashboard">Dashboard</a> -->
<div ng-view></div>
</div>
</div>
</body>
<script type="text/javascript">
//Create the module
var app = angular.module("mainApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "login.html",
controller: "myCtrl"
})
.when("/dashboard", {
templateUrl : "dashboard.html",
controller: "dashCtrl"
})
.when("/register", {
templateUrl : "register.html",
controller: "regCtrl"
})
});
app.controller('dashCtrl', function($scope, $location) {
$scope.username = localStorage.getItem("user")
//New function is getEntriesForUser(usename)
$scope.entrydata = getEntriesForUser($scope.username);
$scope.addEntry = function(){
//Get Values in text boxes
var cal = $scope.toAddCalories
var food = $scope.toAddFood
//get username for current user
var username = $scope.username
//Store them in the database, using an api call
//addEntry(user, food, calories)
addEntry(username, food, cal)
$scope.entrydata = getEntriesForUser($scope.username);
}
$scope.logout = function(){
//Clear the user
localStorage.setItem("user", null)
//Redirect to login screen
$location.path("/");
}
})
app.controller('regCtrl', function($scope, $location){
$scope.doRegister = function (){
//Step 1: Check if the username is already used
//Should be done in the API function
//Step 2: Check to see if the two passwords match
if ($scope.password == $scope.passwordc){
//Step 3: Call api function
registerUser($scope.username, $scope.password)
$location.path("/");
} else {
//Show Error
$scope.errormsg = "Register Fail - Passwords do not match"
}
}
});
//Create the controller
app.controller('myCtrl', function($scope, $location) {
if (localStorage.getItem("user")
&& localStorage.getItem("user") != "null"){
$location.path("dashboard");
}
//Now, do the initialization
$scope.doLogin = function(){
console.log($scope.username + " "+ $scope.password);
/*
$http.get(...).then(
function(){
//called if the call was successful
},
function(){
//called if the call failed
}
);
*/
//Do normal login
var result = logIn($scope.username, $scope.password);
console.log("Login Result: " + result);
if (result) {
//Clear Error
$scope.errormsg = null;
localStorage.setItem("user", $scope.username)
$location.path("dashboard");
} else {
//Else Print error
$scope.errormsg = "Login Failed.";
}
};
});
</script>
</html>