Skip to content

Commit

Permalink
Fix #1: disable form input after send button is clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
CorySanin committed Jan 15, 2024
1 parent 803a6f2 commit c4326c4
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "seance-for-ghost",
"module": "index.ts",
"type": "module",
"version": "0.1.0",
"version": "0.1.1",
"dependencies": {
"body-parser": "1.20.2",
"ejs": "3.1.9",
Expand Down
32 changes: 28 additions & 4 deletions scripts/form.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
window.unblockSend = function() {
let sendBtn = document.getElementById('sendBtn');
sendBtn.disabled = false;
}
(function () {
let sendBtn, form;

function blockInput(element) {
element.readOnly = true;
}

document.addEventListener("DOMContentLoaded", function () {
sendBtn = document.getElementById('sendBtn');
form = document.getElementById('contactForm');

form.addEventListener("submit", function(ev) {
let el;
for(el of form.getElementsByTagName('input')) {
blockInput(el);
}
for(el of form.getElementsByTagName('textarea')) {
blockInput(el);
}
sendBtn.disabled = true;
form.classList.add('processing');
});
});

window.unblockSend = function () {
sendBtn.disabled = false;
}
})();
24 changes: 17 additions & 7 deletions styles/01-layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ body {
grid-template-columns: auto 1fr;
gap: .5em;

&.processing {
cursor: wait;

label,
input,
textarea {
cursor: inherit;
}
}

&>label {
text-align: right;
}

&>textarea {
width: 100%;
min-height: 6em;
min-height: 8em;
max-height: 30em;
resize: vertical;
}
Expand Down Expand Up @@ -62,18 +72,18 @@ button,
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

&:disabled,
&[disabled] {
cursor: not-allowed;
}
}

input,
textarea,
button,
.button {
padding: 5px 10px;

&:disabled,
&[disabled] {
cursor: not-allowed;
}
}

body label {
Expand All @@ -99,7 +109,7 @@ button,
.fullwidth {
grid-column: span 1;
}

.second-col {
grid-column: 1;
}
Expand Down
3 changes: 2 additions & 1 deletion styles/03-colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ button,
}

&:disabled,
&[disabled] {
&[disabled],
&[readonly] {
color: var(--inputborder);

&:hover {
Expand Down
2 changes: 1 addition & 1 deletion views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<body <% if (locals.dark) { echo('class="dark"') }%>>
<div id="main">
<div class="container" class="panel">
<form class="form" method="post">
<form class="form" id="contactForm" method="post">
<label for="nameTxt">Name</label>
<input name="name" id="nameTxt" type="text" required />
<label for="emailTxt">Email</label>
Expand Down
10 changes: 5 additions & 5 deletions web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class Web {
{
nonce,
recaptcha: recaptchaKey,
dark: !!req.query.dark
dark: req?.query?.dark && req.query.dark != 'false'
},
function (err, html) {
if (!err) {
Expand Down Expand Up @@ -106,7 +106,7 @@ export default class Web {

let sendMail = async (req: express.Request, res: express.Response, next?: express.NextFunction) => {
const renderPage = createPageRenderer(res);

let dark = req?.query?.dark && req.query.dark != 'false';
if (req?.body?.email && req.body.name && req.body.message && emailValidator.test(req.body.email)) {
try {
console.log(await emailTransport.sendMail({
Expand All @@ -118,7 +118,7 @@ export default class Web {
}));
res.render('result',
{
dark: !!req.query.dark,
dark,
header: 'Message sent',
text: 'Your message has been received. Thank you!'
},
Expand All @@ -130,7 +130,7 @@ export default class Web {
res.status(500);
res.render('result',
{
dark: !!req.query.dark,
dark,
header: 'Error',
text: 'An error occurred while attempting to deliver your message. Try again later.'
},
Expand All @@ -142,7 +142,7 @@ export default class Web {
res.status(400);
res.render('result',
{
dark: !!req.query.dark,
dark,
header: 'Error',
text: 'There was something wrong with the form you submitted. Go back and try again.'
},
Expand Down

0 comments on commit c4326c4

Please sign in to comment.