Skip to content

Commit

Permalink
Merge pull request #10 from amankrokx/email
Browse files Browse the repository at this point in the history
added email feature
  • Loading branch information
amankrokx authored May 26, 2024
2 parents 0d76491 + f14221a commit 30bc465
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
51 changes: 43 additions & 8 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
{
"database": {
"rules": "database.rules.json"
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**",
"**/src/**"
],
"rewrites": [
{
"source": "mail",
"function": "mail"
}
]
},
"storage": {
"rules": "storage.rules"
},
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
],
"emulators": {
"functions": {
"port": 5001
},
"hosting": {
"public": "public",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**", "**/src/**"]
"ui": {
"enabled": true
},
"storage": {
"rules": "storage.rules"
}
"singleProjectMode": true
}
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ onAuthStateChanged(auth, user => {
}
})
if (user) {
window.userDisplayName = user.displayName
window.userEmail = user.email
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/firebase.User
// console.log(user)
Expand Down
6 changes: 5 additions & 1 deletion src/modules/db.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ref, set, update, onValue, get, child, onChildAdded, query, limitToLast, off } from "firebase/database"
import { child, get, limitToLast, off, onChildAdded, onValue, query, ref, set, update } from "firebase/database"
import sendMail from "./sendMail.js"

class DBs {
constructor(db, uid, hid) {
Expand Down Expand Up @@ -137,6 +138,8 @@ class DBs {
this.cart = { value: 0 }
this.writeToPath(`users/${this.uid}/bookingRequests/${this.hallid}/${t}`, clone)
clone.uid = this.uid
// send email to admin
sendMail(window.userDisplayName || window.userEmail || "RNSHalls User")
this.writeToPath(`admin/bookingRequests/${this.hallid}/${t}`, clone)
for (let i = 0; i < this.timeEntry.length; i++) this.timeEntry[i].classList.remove("selected")
this.bookingCounter(0)
Expand Down Expand Up @@ -499,3 +502,4 @@ class DBs {
}

export { DBs }

33 changes: 33 additions & 0 deletions src/modules/sendMail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
*
* @param {string?} name user's name
* @param {string?} message
// * @param {string} email Email address of the Admin
*/
function sendMail(name = "RNSHalls User", message = "", email = "[email protected]") {
// send email
if (message === "") {
message = `${new Date()}\nNew Hall Booking request for RNS Halls by ${name}\nVisit at https://rnshalls.web.app or https://halls.rnsit.ac.in\n\n RNS Halls`
}
fetch("/mail", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
redirect: "follow",
body: JSON.stringify({
name,
email,
message,
}),
}).then((response) => {
if (response.ok) {
console.log("Email sent successfully")
} else {
console.error("Error sending email")
alert("Warn: Admin not notified. Please inform them manually if urgent.")
}
})
}

export default sendMail

0 comments on commit 30bc465

Please sign in to comment.