Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit a87314f

Browse files
committed
Validate input and show errors
1 parent 845ff12 commit a87314f

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

extension/src/common.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ h1, h2, h3 {
2222
font-weight: bold;
2323
}
2424

25+
.error {
26+
color: $text-color-status-error;
27+
}
28+
.small {
29+
margin-top: 6px;
30+
margin-bottom: 6px;
31+
font-size: 0.8em;
32+
}
33+
2534
input[type=text] {
2635
padding: 6px 9px;
2736
border: solid $text-color-darker 1px;

extension/src/config.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</header>
2424

2525
<section id="sail-status" class="status-container content">
26-
<div class="status content">
26+
<div class="status">
2727
<h3 id="sail-available-status">Fetching Sail URL...</h3>
2828
</div>
2929
</section>
@@ -63,11 +63,24 @@ <h2>Approved Hosts</h2>
6363
-->
6464
</tbody>
6565
</table>
66+
<p id="approved-hosts-remove-error" class="error small" style="display: none;">
67+
Failed to remove host from the approved hosts list.
68+
Check the browser console for more details.
69+
</p>
6670

6771
<div style="margin-top: 40px;">
6872
<h3 style="margin-bottom: 8px;">Add an approved host:</h3>
6973
<input id="approved-hosts-add-input" type="text" pattern="^(\.?[^\.]+)+$">
7074
<button id="approved-hosts-add">Add</button>
75+
<p id="approved-hosts-bad-input" class="error small" style="display: none;">
76+
Invalid host. A valid host looks like
77+
<code>.github.com</code> or
78+
<code>sail.dev</code>.
79+
</p>
80+
<p id="approved-hosts-error" class="error small" style="display: none;">
81+
Failed to add host to the approved hosts list.
82+
Check the browser console for more details.
83+
</p>
7184

7285
<p>
7386
If you prepend your host with a period, Sail

extension/src/config.ts

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import "./config.scss";
99
const sailStatus = document.getElementById("sail-status");
1010
const sailAvailableStatus = document.getElementById("sail-available-status");
1111
const approvedHostsEntries = document.getElementById("approved-hosts-entries");
12+
const approvedHostsRemoveError = document.getElementById("approved-hosts-remove-error");
1213
const approvedHostsAdd = document.getElementById("approved-hosts-add");
1314
const approvedHostsAddInput = document.getElementById("approved-hosts-add-input") as HTMLInputElement;
15+
const approvedHostsBadInput = document.getElementById("approved-hosts-bad-input");
16+
const approvedHostsError = document.getElementById("approved-hosts-error");
1417

1518
// Check if the native manifest is installed.
1619
sailAvailable().then(() => {
@@ -30,29 +33,61 @@ sailAvailable().then(() => {
3033
sailStatus.appendChild(pre);
3134
});
3235

33-
// Create event listener to add approved hosts.
36+
// Create event listeners to add approved hosts.
3437
approvedHostsAdd.addEventListener("click", (e: Event) => {
3538
e.preventDefault();
36-
const host = approvedHostsAddInput.value.toLowerCase();
37-
// TODO: validate here
39+
submitApprovedHost();
40+
});
41+
approvedHostsAddInput.addEventListener("keyup", (e: KeyboardEvent) => {
42+
if (e.keyCode === 13) {
43+
e.preventDefault();
44+
submitApprovedHost();
45+
}
46+
});
47+
let invalidInputTimeout: number = null;
48+
let errorTimeout: number = null;
49+
const submitApprovedHost = (): Promise<void> => {
50+
let host = approvedHostsAddInput.value.toLowerCase();
3851
if (!host) {
3952
return;
4053
}
41-
console.log(host);
4254

43-
addApprovedHost(host)
55+
// Validation logic. Users can put in a full URL or a valid host and it
56+
// should be parsed successfully.
57+
const match = host.match(/^\s*(https?:\/\/)?((\.?[a-z\d_-]+)+)(\/.*)?\s*$/);
58+
if (!match) {
59+
approvedHostsBadInput.style.display = "block";
60+
clearTimeout(invalidInputTimeout);
61+
invalidInputTimeout = setTimeout(() => {
62+
approvedHostsBadInput.style.display = "none";
63+
}, 5000);
64+
return;
65+
}
66+
host = match[2];
67+
68+
return addApprovedHost(host)
4469
.then(() => {
4570
approvedHostsAddInput.value = "";
4671
})
4772
.catch((ex) => {
48-
alert("Failed to add host to approved hosts list.\n\n" + ex.toString());
73+
console.error("Failed to add host to approved hosts list.", ex);
74+
approvedHostsRemoveError.style.display = "block";
75+
clearTimeout(errorTimeout);
76+
errorTimeout = setTimeout(() => {
77+
approvedHostsError.style.display = "none";
78+
}, 5000);
4979
})
5080
.finally(() => {
51-
reloadApprovedHostsTable();
81+
reloadApprovedHostsTable()
82+
.then((hosts) => console.log("Reloaded approved hosts.", hosts))
83+
.catch((ex) => {
84+
alert("Failed to reload approved hosts from extension storage.\n\n" + ex.toString());
85+
});
5286
});
53-
});
87+
};
5488

5589
// Handles click events for remove buttons in the approved hosts table.
90+
let removeErrorTimeout: number = null;
5691
const removeBtnHandler = function (e: Event) {
5792
e.preventDefault();
5893
const host = this.dataset.host;
@@ -70,7 +105,12 @@ const removeBtnHandler = function (e: Event) {
70105
return setApprovedHosts(hosts);
71106
})
72107
.catch((ex) => {
73-
alert("Failed to remove host from approved hosts list.\n\n" + ex.toString());
108+
console.error("Failed to remove host from approved hosts list.", ex);
109+
approvedHostsRemoveError.style.display = "block";
110+
clearTimeout(removeErrorTimeout);
111+
removeErrorTimeout = setTimeout(() => {
112+
approvedHostsRemoveError.style.display = "none";
113+
}, 5000);
74114
})
75115
.finally(() => {
76116
reloadApprovedHostsTable()

0 commit comments

Comments
 (0)