-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddtransaction.html
256 lines (216 loc) · 8.23 KB
/
addtransaction.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>PayApp - Add Transaction</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Navigation -->
<div id="nav-placeholder"></div>
<!-- Use Fontawesome for icons -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css"
integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<!-- Page Content# -->
<div class="container" style="max-width: 800px;">
<br>
<h3>New Transaction</h3>
<br>
<form>
<div class="form-group">
<label for="inputtitle">Title</label>
<input type="text" class="form-control" id="inputtitle" placeholder="Enter title" required>
</div>
<div class="form-group">
<label for="inputamount">Amount</label>
<input type="number" step="0.05" class="form-control" id="inputamount" placeholder="Enter Amount" required>
</div>
<div class="form-group">
<label for="inputpayer">Payer</label>
<select id="inputpayer" class="form-control">
</select>
</div>
<div class="form-group">
<label for="participantlist">Participants
<i class="fas fa-plus-circle" style="cursor:pointer" id="addparticipant"></i>
</label>
<ul class="list-group list-group-flush" id="participantlist">
</ul>
</div>
<button type="submit" id="submittransaction" class="btn btn-primary">Submit</button>
</form>
</div>
<div class="modal" tabindex="-2" role="dialog" id="addparticipantdialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add New Participant</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p id="namemessage">Enter the name of the new participant</p>
<input class="form-control" type="text" id="newparticipantname" placeholder="Name" required>
<p id="errormessage" class="text-danger"></p>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" id="addparticipantbutton">Add</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="modal" tabindex="-2" role="dialog" id="noparticipantdialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">No Participants Selected</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Please select a participant.</p>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="modal" tabindex="-2" role="dialog" id="emptypayernamedialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Empty Payer Name</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Please make sure you selected a payer</p>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Utility functions -->
<script src="connection.js"></script>
<script>
$("#nav-placeholder").load("nav.html");
var groupid = localStorage.getItem("groupid");
var participants;
var setParticipants = function(data) {
participants = data["participants"];
updateParticipants();
// add values from URL query (eg reverse transaction)
uriParams = new URLSearchParams(window.location.search);
document.getElementById("inputtitle").value = uriParams.get("title");
document.getElementById("inputamount").value = uriParams.get("amount");
document.getElementById("inputpayer").value = uriParams.get("payer");
var i = 0;
// the while condition sets uriParticipant and checks if it's not null #beaut
while (uriParticipant = uriParams.get("involved" + i)) {
for (var existingParticipant of participants) {
if (uriParticipant === existingParticipant) {
document.getElementById("involved-checkbox-" + existingParticipant).checked = true;
}
}
++i;
}
}
sendRequest("participants/" + groupid, "GET", {}, setParticipants, standardErrorAlert);
function optionTag(participant) {
var option = $("<option>");
option.text(participant);
return option;
}
function listItem(participant) {
var item = $("<li>");
item.attr("class", "list-group-item");
var formdiv = $("<div>");
formdiv.attr("class", "form-check");
var label = $("<label>");
label.attr("class", "form-check-label");
var checkbox = $("<input>");
checkbox.attr("type", "checkbox");
checkbox.attr("class", "form-check-input");
checkbox.attr("id", "involved-checkbox-" + participant);
label.text(participant);
label.prepend(checkbox);
formdiv.append(label);
item.append(formdiv);
return item;
}
function updateParticipants() {
$(".list-group-item").remove()
$("option").remove()
for (var p of participants) {
$("#inputpayer").append(optionTag(p));
$("#participantlist").append(listItem(p));
}
}
function getSelectedParticipants() {
selected = [];
$(".form-check-input").each(function(index, obj) {
if (obj.checked){
selected.push(participants[index]);
}
});
return selected;
}
$("#addparticipant").on("click", function (e) {
$("#errormessage").text("");
$("#newparticipantname").val("");
$("#addparticipantdialog").modal("show");
});
$("#addparticipantbutton").on("click", function (e) {
var newParticipant = $("#newparticipantname").val();
if (participants.indexOf(newParticipant) > -1) {
$("#errormessage").text("Participant already exists. Please choose another name.")
} else if (newParticipant=="") {
$("#errormessage").text("Please choose a name.");
} else {
participants.push(newParticipant);
updateParticipants(participants);
$("#addparticipantdialog").modal("hide");
}
});
$("form").on("submit", function (e) {
e.preventDefault();
});
$("#submittransaction").on("click", function (e) {
var selectedParticipants = getSelectedParticipants();
if (selectedParticipants.length == 0) {
$("#noparticipantdialog").modal("show");
return;
}
var payer = $("#inputpayer option:selected").text();
if (payer.length == 0) {
$("#emptypayernamedialog").modal("show");
return;
}
localStorage.setItem("lastPayer", payer);
var amount = $("#inputamount").val();
var title = $("#inputtitle").val();
transaction = {"payer": payer, "involved": selectedParticipants, "amount": amount, "comment": title, "timestamp": Date.now()};
onSuccess = function(data) {
location.href = "balances.html";
}
sendRequest("addtransaction/" + groupid, "POST", transaction, onSuccess, standardErrorAlert);
//TODO: save last participants
});
</script>
</body>
</html>