Files
internship_node/day17/routes/index.js
T

140 lines
3.3 KiB
JavaScript
Raw Normal View History

2025-07-21 22:23:26 +01:00
var express = require("express");
2022-02-06 22:32:46 -05:00
var router = express.Router();
2025-07-21 22:23:26 +01:00
const moment = require("moment-timezone");
2022-02-06 22:32:46 -05:00
2025-07-21 22:23:26 +01:00
// Helper: Group timezones by region for the timezone selection screen
function getTimezoneGroups() {
const regions = {
"USA/CANADA": [
"America/Los_Angeles",
"America/Denver",
"America/New_York",
"America/Halifax",
],
EUROPE: [
"Europe/Berlin",
"Europe/Helsinki",
"Europe/Dublin",
"Europe/Samara",
],
ASIA: ["Asia/Hong_Kong", "Asia/Jakarta", "Asia/Kabul", "Asia/Kathmandu"],
"SOUTH AMERICA": [
"America/Bogota",
"America/Campo_Grande",
"America/Caracas",
"America/Lima",
],
};
const groups = {};
for (const region in regions) {
groups[region] = regions[region].map((tz) => ({
2025-07-22 19:21:32 +01:00
name: tz,
label: tz.replace(/_/g, " ").replace("America/", ""),
time_am: moment().tz(tz).format("h:mma"),
time_24: moment().tz(tz).format("HH:mm"),
2025-07-21 22:23:26 +01:00
}));
}
return groups;
}
// Helper: All timezones for dropdown
function getAllTimezones() {
return moment.tz.names().map((tz) => ({
value: tz,
label: tz,
}));
}
// Helper: Generate week days and slots
2025-07-22 19:21:32 +01:00
function getWeekDaysAndSlots(selectedTz, weekOffset = 0) {
2025-07-21 22:23:26 +01:00
const weekDays = [];
2025-07-22 19:21:32 +01:00
const today = moment()
.tz(selectedTz)
.add(weekOffset * 7, "days");
2025-07-21 22:23:26 +01:00
for (let i = 0; i < 7; i++) {
const day = today.clone().add(i, "days");
weekDays.push({
label: day.format("dddd"),
date: day.format("MMMM D"),
dateISO: day.format("YYYY-MM-DD"),
slots: [
"9:00am",
"9:15am",
"9:30am",
"9:45am",
"10:00am",
"10:15am",
"10:30am",
"10:45am",
"11:00am",
"11:15am",
"11:30am",
"11:45am",
"12:00pm",
"12:15pm",
"12:30pm",
"12:45pm",
"1:00pm",
"1:15pm",
],
});
}
return { weekDays, maxSlots: 17 };
}
// Timezone selection screen
router.get("/timezone", function (req, res) {
res.render("timezone", {
timezones: getAllTimezones(),
timezoneGroups: getTimezoneGroups(),
});
});
// Calendar slot selection screen
router.get("/calendar", function (req, res) {
const selectedTimezone = req.query.tz || "America/New_York";
2025-07-22 19:21:32 +01:00
const weekOffset = parseInt(req.query.week) || 0;
const { weekDays, maxSlots } = getWeekDaysAndSlots(
selectedTimezone,
weekOffset
);
2025-07-21 22:23:26 +01:00
res.render("calendar", {
selectedTimezone,
weekDays,
maxSlots,
2025-07-22 19:21:32 +01:00
showPrevWeek: weekOffset > 0,
prevWeek: weekOffset - 1,
nextWeek: weekOffset + 1,
2025-07-21 22:23:26 +01:00
});
});
// Booking form screen
router.get("/book", function (req, res) {
const selectedTimezone = req.query.tz || "America/New_York";
2025-07-22 19:21:32 +01:00
const selectedDate = req.query.date || "";
const selectedTime = req.query.time || "";
2025-07-21 22:23:26 +01:00
res.render("booking-form", {
selectedTimezone,
2025-07-22 19:21:32 +01:00
selectedDate,
selectedTime,
2025-07-21 22:23:26 +01:00
});
});
// Booking POST (simulate success)
router.post("/book", function (req, res) {
// Here you would save booking info to DB
res.redirect("/success");
});
// Success screen
router.get("/success", function (req, res) {
res.render("success");
});
// Home page redirect to timezone selection
router.get("/", function (req, res) {
res.redirect("/timezone");
2022-02-06 22:32:46 -05:00
});
module.exports = router;