-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
173 lines (143 loc) · 6.37 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Address Book</title>
<!-- Bootstrap so it dosen't loop 100% ugly -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Not the most elegant way to use angular, type script would be better but I don't have much
expirence in ts so it would take much longer -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="/scripts/angular-file-upload.min.js"></script>
<style>
body {
padding-top: 5rem;
}
</style>
<script>
// we're just going to create a single component application
// mainly I want to use angulars ng-repeat and a few similar things
var app = angular.module("address-book", ['angularFileUpload', ])
app.controller("address-list", function ($scope, $http, $document) {
// load the list of addresses
function reload() {
$http({
"method": "GET",
"url": "/address"
}).then(function onSuccess(response) {
console.log(response);
$scope.addresses = response.data.entries;
$scope.has_pages = response.data.is_more;
}, function onFailure(response) {
alert("Failed to load data!");
})
}
$scope.reloadAddresses = reload;
// error handling
$scope.errors = [];
$scope.error = "";
$scope.getNextError = function () {
if ($scope.errors.length > 0) {
$scope.error = $scope.errors.pop();
var wait = confirm($scope.error);
$scope.getNextError();
}
}
reload();
});
app.controller("address-upload", function ($scope, $http, FileUploader) {
$scope.uploader = new FileUploader();
$scope.uploader.url = "/address";
$scope.uploader.onAfterAddingFile = function (item) {
console.log(item);
item.upload();
};
$scope.successShow = false;
$scope.successMessage = "";
$scope.conflicts = []
$scope.uploader.onCompleteItem = function (item, response, status, headers) {
console.log(response);
item.remove();
if (status != 200) {
response["errors"].forEach(function (error) {
$scope.errors.push(error);
});
$scope.getNextError();
} else if(response["conflict_entries"].length > 0) {
$scope.conflicts = response["conflict_entries"];
} else {
console.log("success!");
$scope.successShow = true;
$scope.successMessage = "File Uploaded Succesfully!";
$scope.$digest();
}
$scope.reloadAddresses();
};
$scope.resolve = function (conflict) {
var newDict = {
id: conflict["id"],
email: conflict["email"],
}
// we only need to update it if its the new value, old values are already saved
if (conflict["new_value"] == "new") {
newDict["name"] = conflict["requested_name"];
$http.post("/address/"+ conflict["id"], newDict).then(function onSuccess(response) {
console.log(response);
$scope.reloadAddresses();
}, function onFailure(response) {
alert("Failed to update!");
})
}
var listCopy = new Set($scope.conflicts);
listCopy.delete(conflict);
$scope.conflicts = Array.from(listCopy);
$scope.reloadAddresses();
}
});
</script>
</head>
<body ng-app="address-book">
<nav class="navbar navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="#">Address Book</a>
</nav>
<!-- Main Body -->
<div class="container" ng-controller="address-list" id="address-main">
<div class="row">
<div class="col-sm-5">Name</div>
<div class="col-sm-5">Email</div>
</div>
<div class="row" ng-repeat="address in addresses">
<div class="col-sm-5">{{address.name}}</div>
<div class="col-sm-5">{{address.email}}</div>
</div>
<div class="row" >
<div class="col-sm-10" ng-controller="address-upload">
<div class="alert alert-success" role="alert" ng-show="successShow">
{{successMessage}}
</div>
<div class="alert alert-danger" role="alert" ng-repeat="conflict in conflicts">
The entry for {{conflict.email}} has two entries, which do you prefer?<br/>
<form>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="inlineRadioOptions"
id="inlineRadio1" ng-model="conflict.new_value" value="new"> {{conflict.requested_name}}
</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="inlineRadioOptions"
id="inlineRadio2" ng-model="conflict.new_value" value="old"> {{conflict["name"]}}
</label>
</div>
<button class="btn btn-primary" type="button" ng-click="resolve(conflict)">Resolve</button>
</form>
</div>
Upload New Contacts with a CSV (email, name)
<input type="file" nv-file-select uploader="uploader" />
</div>
</div>
</div>
</body>
</html>