The contactUs
function handles incoming contact form submissions, stores the data in a Google Sheet, and redirects the user upon successful submission.
npm run import -- "contact us handler"
var importer = require('../Core');
var addRow = importer.import("add row data google sheet");
function contactUs(name, email, subject, message) {
console.log(`Adding contact row for ${req.body['name']}`)
return addRow(process.env.CONTACT_DOCID, {
timestamp: Date.now(),
name,
email,
subject,
message,
responded: 0,
})
.then(r => res.redirect(process.env.CONTACT_REDIRECT))
}
module.exports = contactUs;
// Importing required modules
const importer = require('../Core');
const addRow = importer.import('addRowDataGoogleSheet'); // Renamed for consistency
/**
* Handles contact us form submission
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
function contactUs(req, res) {
// Extracting required fields from request body
const { name, email, subject, message } = req.body;
// Logging contact form submission
console.log(`Adding contact row for ${name}`);
// Adding new contact row to Google Sheet
return addRow(process.env.CONTACT_DOCID, {
timestamp: Date.now(),
name,
email,
subject,
message,
responded: 0,
})
.then((response) => {
// Redirecting to a success page after adding row
res.redirect(process.env.CONTACT_REDIRECT);
})
.catch((error) => {
// TODO: Implement error handling for adding row to Google Sheet
console.error(error);
// TODO: Implement fallback logic for error scenarios
});
}
module.exports = contactUs;
This code defines a function contactUs
that handles incoming contact form submissions and adds them to a Google Sheet.
Functionality:
Imports:
addRow
from a custom module (../Core
) responsible for adding data to a Google Sheet.contactUs
Function:
name
, email
, subject
, and message
as input, representing the contact form data.addRow
to add a new row to the specified Google Sheet document (process.env.CONTACT_DOCID
) with the provided data and additional fields (timestamp
, responded
).process.env.CONTACT_REDIRECT
) upon successful submission.Export:
contactUs
function for use in other parts of the application.Purpose:
The code provides a mechanism for capturing contact form submissions and storing them in a Google Sheet for later processing or review.