-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (99 loc) · 3.25 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
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
var app = angular.module('UsersApp', ['ngRoute']);
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/addUser.html',
controller: 'userController',
activetab: "home"
})
.when('/userslist', {
templateUrl: 'views/listing.html',
controller: 'listingController',
activetab: "listing"
})
.otherwise({
redirectTo: '/'
});
}]);
app.controller('mainController', function ($scope, $route, commonService) {
$scope.$route = $route;
var users = [];
if (typeof(Storage) !== "undefined") {
users = localStorage.getItem("users");
//localStorage.clear();
if (angular.isUndefined(users) || users === null || users === "") {
users = [];
} else {
users = JSON.parse(users);
//console.log(users);
}
} else {
commonService.setIsLocalStorageSupported(false);
}
commonService.setUsers(users);
});
app.service('commonService', function () {
var isLocalStorageSupported = true;
var users = [];
this.setUsers = function (usersArr) {
users = usersArr;
};
this.getUsers = function () {
return angular.copy(users);
};
this.setIsLocalStorageSupported = function (flag) {
isLocalStorageSupported = flag;
};
this.getIsLocalStorageSupported = function () {
return angular.copy(isLocalStorageSupported);
};
});
app.controller('userController', function ($scope, $timeout, commonService) {
var users = commonService.getUsers();
$scope.showError = false;
$scope.user = {};
$scope.submitSuccess = false;
var today = new Date();
var date = today.getDate();
var month = today.getMonth() + 1;
var year = today.getFullYear();
date = date < 10 ? '0' + date : date;
month = month < 10 ? '0' + month : month;
$scope.today = year + '-' + month + '-' + date;
$scope.maxDOB = (year - 100) + '-' + month + '-' + date;
$scope.submitForm = function () {
if (!isAgeCorrect()) {
$scope.userForm.age.$invalid = true;
} else {
users.push($scope.user);
if (commonService.getIsLocalStorageSupported()) {
localStorage.setItem("users", JSON.stringify(users));
}
commonService.setUsers(users);
$scope.submitSuccess = true;
$timeout(function () {
$scope.submitSuccess = false;
}, 5000);
resetForm();
}
};
var resetForm = function () {
$scope.showError = false;
$scope.user = {};
$scope.userForm.$setPristine();
};
var isAgeCorrect = function () {
var birthDate = new Date($scope.user.dob);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && date < birthDate.getDate())) {
age--;
}
return age === $scope.user.age;
};
});
app.controller('listingController', function ($scope, $route, commonService) {
$scope.$route = $route;
$scope.users = commonService.getUsers();
$scope.searchText = '';
});