Skip to content

Commit

Permalink
Various changes
Browse files Browse the repository at this point in the history
Refactor the email handler and bring it into the src directory so it can be compiled;

Removed not needed files

-Change the npm run build command to run purgecss via a config file, otherwise too much is removed for some reason.
  • Loading branch information
Spencer14420 committed Nov 10, 2024
1 parent 23c0329 commit 2fd1fe6
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 59 deletions.
51 changes: 0 additions & 51 deletions assets/js/email-handler.js

This file was deleted.

7 changes: 3 additions & 4 deletions assets/js/email-handler.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php
if (isset($_POST["email"]) && isset($_POST["message"])) {
//Send email to [email protected]
$name = $_POST["name"];
$email = $_POST["email"];
$headers = "From: [email protected]\r\nReply-To: $email";
$headers = "From: admin@nm.capanda.ca\r\nReply-To: $email";
$message = "From: {$name} ({$email})\n\nMessage:\n" . wordwrap($_POST["message"], 70);

mail("info@capanda.ca", "Message from {$name} via capanda.ca", $message, $headers);
mail("admin@capanda.ca", "Message from {$name} via capanda.ca", $message, $headers);

//Send confirmation email to sender
$headers = "From: info@capanda.ca";
$headers = "From: admin@nm.capanda.ca";
$message = "Dear {$name} ({$email}),\n\nYour message (shown below) has been recieved. We will get back to you as soon as possible.\n\nSincerely,\nCAPANDA\n\nPlease note: This message was sent referencing your email address - If this was not you, please be aware of the use of your email address.\n\nYour message:\n" . wordwrap($_POST["message"], 70);

mail($email, "Your message to CAPANDA has been recieved", $message, $headers);
Expand Down
2 changes: 0 additions & 2 deletions assets/js/jquery-3.6.0.min.js

This file was deleted.

1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
rel="stylesheet">
<script src="assets/bootstrap.bundle.min.js"></script>
<script type="module" src="script.js"></script>
<script defer src="assets/js/email-handler.js"></script>

</head>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "nodemon -w src -w assets -w index.html -e css,html,js --ignore assets/slimmed-bootstrap.css --ignore script.js -x npm run build",
"build": "npx purgecss --css assets/bootstrap.min.css --content index.html 'src/**/*.js' 'assets/**/*.js' --output assets/slimmed-bootstrap.css && npx esbuild src/main.js --bundle --minify --outfile=script.js"
"build": "npx purgecss --config purgecss.config.js && npx esbuild src/main.js --bundle --minify --outfile=script.js"
},
"repository": {
"type": "git",
Expand Down
5 changes: 5 additions & 0 deletions purgecss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
content: ["index.html", "src/**/*.js", "assets/**/*.js"],
css: ["assets/bootstrap.min.css"],
output: "assets/slimmed-bootstrap.css",
};
72 changes: 72 additions & 0 deletions src/classes/ContactForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export class ContactForm {
constructor(serverScript) {
this.serverScript = serverScript;
this.successModal = new bootstrap.Modal(document.querySelector("#success"));
this.messageAlert = document.querySelector("#message-alert");
this.initializeEventListeners();
}

initializeEventListeners() {
document
.querySelector("#sendmessage")
.addEventListener("click", () => this.handleSubmit());
}

isEmail(email) {
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailPattern.test(email);
}

messageSuccess() {
["#name", "#email", "#message"].forEach((element) => {
document.querySelector(element).value = "";
});
document.querySelector("#contactCancel").click();
this.successModal.show();
}

async sendMessage(data) {
const formData = new FormData();
Object.entries(data).forEach(([key, value]) => formData.append(key, value));

try {
const response = await fetch(this.serverScript, {
method: "POST",
body: formData,
});
if (response.ok) {
this.messageSuccess();
}
} catch (error) {
console.error("Error sending message", error);
}
}

handleSubmit() {
const email = document.querySelector("#email").value;
const message = document.querySelector("#message").value;

if (!email || !this.isEmail(email)) {
this.displayAlert("Please enter a valid email address");
return;
}

if (!message) {
this.displayAlert("Please enter a message");
return;
}

const data = {
name: document.querySelector("#name").value,
email,
message,
};

this.sendMessage(data);
}

displayAlert(message) {
this.messageAlert.innerHTML = message;
this.messageAlert.style.display = "block";
}
}
5 changes: 5 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { CONFIG } from "./constants/config.js";
import { ContactForm } from "./classes/ContactForm.js";
import { derivedValues } from "./constants/derivedValues.js";
import { PanelManager } from "./classes/PanelManager.js";
import { UIManager } from "./classes/UIManager.js";

document.addEventListener("DOMContentLoaded", () => {
new ContactForm("assets/js/email-handler.php");
});

const panelManager = new PanelManager();

//Set panel positions when the page loads or the window is resized
Expand Down

0 comments on commit 2fd1fe6

Please sign in to comment.