Implemented new saveAddresses method in Dart
Fixes the bug, that long list can't be sent via get methods.
This commit is contained in:
parent
9f605bb12f
commit
56e607c04a
2 changed files with 50 additions and 4 deletions
|
@ -1,14 +1,20 @@
|
|||
// Copyright (c) 2016 - 2017, Marcel Kapfer (mmk2410).
|
||||
// MIT License
|
||||
|
||||
/// frontend code for filespread, a simple file over email spreading web app.
|
||||
import 'dart:convert';
|
||||
import 'dart:html';
|
||||
|
||||
/// main method with click listners.
|
||||
void main() {
|
||||
// send mail listener
|
||||
querySelector("#send").onClick.listen(sendMail);
|
||||
|
||||
// save address list listner
|
||||
querySelector("#save-addresses").onClick.listen(saveAddresses);
|
||||
}
|
||||
|
||||
/// function for calling the backend to send mails.
|
||||
void sendMail(MouseEvent event) {
|
||||
InputElement title = querySelector("#mailtitle");
|
||||
InputElement text = querySelector("#mailtext");
|
||||
|
@ -26,12 +32,52 @@ void sendMail(MouseEvent event) {
|
|||
|
||||
String url = "res/php/sendmail.php?data=${JSON.encode(data)}";
|
||||
|
||||
HttpRequest.getString(url).then((response) {
|
||||
HttpRequest.getString(url).then((String response) {
|
||||
if (response == "0") {
|
||||
window.alert("Mails successfully sent.");
|
||||
} else {
|
||||
window.alert("Failed to send mail.");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/// function to update the addresses list in the backend.
|
||||
void saveAddresses(MouseEvent event) {
|
||||
TextAreaElement addresses = querySelector("#edit-addresses");
|
||||
SelectElement list = querySelector("#edit-address-list");
|
||||
|
||||
List<String> addressesList = new List();
|
||||
|
||||
addresses.value.split("\n").forEach((String address) {
|
||||
if (address.isNotEmpty) addressesList.add(address);
|
||||
});
|
||||
|
||||
Map data = {"list": list.value, "addresses": addressesList};
|
||||
|
||||
String url = "res/php/savelist.php";
|
||||
|
||||
HttpRequest.postFormData(url, {"data": JSON.encode(data)}).then((HttpRequest response) {
|
||||
if (response.responseText == "0") {
|
||||
window.alert("Addresses sucessfully saved.");
|
||||
updateAddresses(addressesList, list.value);
|
||||
} else {
|
||||
window.alert("Failed to save adresses.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Updated the frontend after an address change.
|
||||
void updateAddresses(List<String> addresses, String list) {
|
||||
SelectElement currentList = querySelector("#address_list");
|
||||
|
||||
if (currentList.value == list) {
|
||||
DivElement addressesContainer = querySelector("#addresses");
|
||||
addressesContainer.children .removeWhere((Element child) => addressesContainer.children.contains(child));
|
||||
|
||||
addresses.forEach((String address) {
|
||||
ParagraphElement addressElement = new ParagraphElement();
|
||||
addressElement.text = address;
|
||||
addressesContainer.children.add(addressElement);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<?php
|
||||
# COPYRIGHT (c) 2016 Marcel Kapfer (mmk2410)
|
||||
# COPYRIGHT (c) 2016-2017 Marcel Kapfer (mmk2410)
|
||||
# MIT License
|
||||
|
||||
$content = $_GET["data"];
|
||||
$content = json_decode($_POST["data"], true);
|
||||
|
||||
$filename = "../../lists/" . $content["list"];
|
||||
|
||||
|
|
Reference in a new issue