Skip to content

Commit

Permalink
added the backend format for contact
Browse files Browse the repository at this point in the history
  • Loading branch information
ayush-848 committed Jun 30, 2024
1 parent 75d6730 commit c4205ed
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 68 deletions.
6 changes: 6 additions & 0 deletions .hintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
{
"image-alt": "off"
}
],
"axe/forms": [
"default",
{
"label": "off"
}
]
}
}
29 changes: 15 additions & 14 deletions assets/validation/validate.schema.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import zod from 'zod';

const validate = (schema) => async (req, res, next) => {
try {
const parsedBody = await schema.parseAsync(req.body);
req.body = parsedBody;
next();
} catch (err) {
console.log(err);
const yourerror = err.errors[0].message;
res.status(400).json({
msg: yourerror,
});
}
try {
const parsedBody = await schema.parseAsync(req.body);
req.body = parsedBody;
next();
} catch (err) {
console.log(err);
const yourerror = err.errors[0].message;
res.status(400).json({
msg: yourerror,
});
}
module.exports = validate;
};

export default validate;
10 changes: 4 additions & 6 deletions assets/validation/zodschema.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const zod = require('zod');
import { z } from 'zod';

const RegisterSchema = zod.object({
username: zod
export const RegisterSchema = z.object({
username: z
.string({ required_error: 'Email is required' })
.trim()
.email({ message: 'Invalid Email Address' })
.regex(/^[a-zA-Z0-9._%+-]+@gmail\.com$/, { message: 'Email must be a Gmail address ending with @gmail.com' })
.min(3, { message: 'Email should be at least 3 characters' })
.max(255, { message: 'Email can be at most 255 characters' }),
password: zod
password: z
.string({ required_error: 'Password is required' })
.trim()
.min(8, { message: 'Password must be at least 8 characters' })
Expand All @@ -25,5 +25,3 @@ const RegisterSchema = zod.object({
'Password must contain at least one special character (!@#$%^&*)',
}),
});

module.exports = RegisterSchema;
61 changes: 38 additions & 23 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1932,35 +1932,52 @@ <h2 class="h2 section-title has-underline">
</h2>
<div class="wrapper">
<div class="contact-card">
<form action="" id="contact-form">
<form action="http://localhost:3000/contact" method="POST" id="contact-form">
<input type="text" name="name" id="name" placeholder="Your Name" required class="input-field" size="60px">

<input type="email" name="email_address" id="email" placeholder="Your Email" required class="input-field">

<input type="text" name="subject" id="subject" placeholder="Subject" required class="input-field" maxlength=100>
<div id="subject-count" class="character-count">0/100</div>
<!-- <div id="subject-full" class="character-full">You have reached the character limit!</div> -->

<textarea name="message" id="message" placeholder="Your Message" class="input-field" maxlength=250></textarea>
<div id="message-count" class="character-count">0/250</div>
<!-- <div id="message-full" class="character-full">You have reached the character limit!</div> -->

<button type="submit" id="sendMessageBtn" class="btn btn-primary">Send Now</button>
<div id="success-message" style="display:none">Message sent successfully!</div>
</form>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
var form = document.getElementById('contact-form');
var message = document.getElementById('message');

form.addEventListener('submit', function(event) {
if (message.value.length < 50) {
alert("Message length can't be less than 50 characters.");
event.preventDefault(); // Prevent the form from being submitted
}});});
var form = document.getElementById('contact-form');
var message = document.getElementById('message');

form.addEventListener('submit', function(event) {
if (message.value.length < 50) {
alert("Message length can't be less than 50 characters.");
event.preventDefault();
} else {
event.preventDefault();
var formData = new FormData(form);

fetch(form.action, {
method: form.method,
body: JSON.stringify(Object.fromEntries(formData)),
headers: {
'Content-Type': 'application/json'
}
}).then(response => response.json())
.then(data => {
if (data.error) {
alert(data.error);
} else {
document.getElementById('success-message').style.display = 'block';
form.reset();
}
}).catch(error => {
alert('Failed to send message');
});
}
});
});
</script>

<ul class="contact-card">
<li>
<p class="card-title"><lord-icon class="invert-col" src="https://cdn.lordicon.com/iikoxwld.json" trigger="hover" colors="primary:#000000" style="width:25px;height:25px"></lord-icon> Address :</p>
Expand All @@ -1969,26 +1986,24 @@ <h2 class="h2 section-title has-underline">
Kolkata, India 700054
</address>
</li>

<li>
<p class="card-title"><lord-icon class="invert-col" src="https://cdn.lordicon.com/srsgifqc.json" trigger="hover" style="width:20px;height:20px;"></lord-icon> Phone :</p>
<a href="tel:1234567890" class="card-link">+91 9124421980</a>
</li>

<li>
<p class="card-title"><lord-icon class="invert-col" src="https://cdn.lordicon.com/xtzvywzp.json" trigger="hover" style="width:20px;height:20px;"></lord-icon> Email :</p>
<a href="mailto:[email protected]" class="card-link">[email protected]</a>
</li>
</ul>
</div>
<iframe
src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3686.5834653201455!2d88.40974607348684!3d22.482281936278092!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3a0273c5411d60d7%3A0x5d6e57763593194b!2sSouthern%20Cleve%20Housing%2C%2012%2C%20Bhagat%20Singh%20Nagar%2C%20Nayabad%2C%20Kolkata%2C%20West%20Bengal%20700150!5e0!3m2!1sen!2sin!4v1716043813992!5m2!1sen!2sin"
style="border:0; margin-bottom:30px;" allowfullscreen="" loading="lazy" referrerpolicy="no-referrer-when-downgrade">
</iframe>

src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3686.5834653201455!2d88.40974607348684!3d22.482281936278092!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3a0273c5411d60d7%3A0x5d6e57763593194b!2sSouthern%20Cleve%20Housing%2C%2012%2C%20Bhagat%20Singh%20Nagar%2C%20Nayabad%2C%20Kolkata%2C%20West%20Bengal%20700150!5e0!3m2!1sen!2sin!4v1716043813992!5m2!1sen!2sin"
style="border:0; margin-bottom:30px;" allowfullscreen="" loading="lazy" referrerpolicy="no-referrer-when-downgrade">
</iframe>
</div>

</section>


<!-- Rate Us Componenet -->
<div id="rate-us">
<div class="heading">
Expand Down
11 changes: 7 additions & 4 deletions package-lock.json

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

15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "swapreads",
"version": "1.0.0",
"description": "",
"description": "<div align=\"center\">",
"main": "f.js",
"type": "module",
"scripts": {
Expand All @@ -10,7 +10,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/pratikgurjar/SwapReads.git"
"url": "git+https://github.com/pratikgurjar/SwapReads.git"
},
"private": true,
"devDependencies": {
Expand All @@ -22,9 +22,16 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"mongoose": "^8.3.5",
"nodemailer": "^6.9.13",
"nodemailer": "^6.9.14",
"nodemon": "^3.1.1",
"validator": "^13.12.0",
"zod": "^3.23.8"
}
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/pratikgurjar/SwapReads/issues"
},
"homepage": "https://github.com/pratikgurjar/SwapReads#readme"
}
37 changes: 20 additions & 17 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const nodemailer = require("nodemailer");
const RegisterSchema = require("./assets/validation/zodschema");
const validate = require("./assets/validation/validate.schema");
const cors=require("cors")
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import nodemailer from 'nodemailer';
import { RegisterSchema } from './assets/validation/zodschema.js';
import validate from './assets/validation/validate.schema.js';

import cors from 'cors';


const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // For parsing application/x-www-form-urlencoded
app.use(cors());
app.use(cors());

const MONGO_URI = "mongodb+srv://nishantkaushal0708:[email protected]/";


const dbConnect = async () => {
try {
await mongoose.connect(MONGO_URI, {
Expand Down Expand Up @@ -133,16 +135,17 @@ dbConnect().then(() => {
}
});

const PORT = process.env.PORT || 3000;
// Subscribe endpoint
app.post('/subscribe', (req, res) => {
let email = req.body.email;
console.log(email);
res.json({ success: true, message: "Subscribed successfully" });
});

const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
});


app.post('/subscribe',(req,res)=>{
let email = req.body;
console.log(email);
})

app.listen(3000, () => console.log("Server is running on port 3000"));
app.listen(4000, () => console.log("Server is running on port 3000"));

0 comments on commit c4205ed

Please sign in to comment.