diff --git a/routes/affirmationRoutes.js b/routes/affirmationRoutes.js index db72b38..0850f88 100644 --- a/routes/affirmationRoutes.js +++ b/routes/affirmationRoutes.js @@ -2,14 +2,38 @@ const express = require("express"); const router = express.Router(); const User = require("../models/User"); +function camelCaseToSpaces(str) { + return str.replace(/([A-Z])/g, " $1").toLowerCase(); +} + router.get("/affirmation-setup", async (req, res) => { let user = req.user; + if (!user || !user.affirmations) { const userId = req.session.passport.user; user = await User.findById(userId); + user = user ? JSON.parse(JSON.stringify(user)) : null; // Deep clone to plain object } - res.render("affirmation-setup", { user: user.toObject() }); + // Debugging Step 1: Check the Conversion + console.log( + "Is Plain Object?", + user instanceof Object && + !(user instanceof Array) && + !(user instanceof Function) + ); + + const categoriesWithSpaces = {}; + if (user && user.affirmations) { + for (const [key, value] of Object.entries(user.affirmations)) { + categoriesWithSpaces[camelCaseToSpaces(key)] = value; + } + } + + // Debugging Step 2: Check the Data + console.log("Categories with Spaces:", categoriesWithSpaces); + + res.render("affirmation-setup", { user, categoriesWithSpaces }); }); router.get("/enter-affirmations", (req, res) => { diff --git a/views/affirmation-setup.pug b/views/affirmation-setup.pug index 418e158..c5ef74a 100644 --- a/views/affirmation-setup.pug +++ b/views/affirmation-setup.pug @@ -13,12 +13,17 @@ block content // Check if affirmations exist if user.affirmations - each category, categoryName in user.affirmations - h3= categoryName - ul.affirmation-list - each affirmation, point in category - li #{point}: #{affirmation} + each category, categoryName in categoriesWithSpaces + .card.mb-4 + .card-header + h3= categoryName + .card-body + ul.affirmation-list.list-group + each affirmation, point in category + li.list-group-item #{point}: #{affirmation} + .card-footer + a.btn.btn-primary(href="/edit-affirmations/#{categoryName}") Edit // Button to enter new affirmations a.btn.btn-primary(href="/enter-affirmations") Enter New Affirmations else - p.prompt You have no affirmations. Would you like to #[a(href="/enter-affirmations") enter some]? + .alert.alert-warning You have no affirmations. Would you like to #[a(href="/enter-affirmations") enter some]?