Skip to content

Commit

Permalink
changing stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
abutler911 committed Oct 24, 2023
1 parent 22c58ec commit ed77a20
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
26 changes: 25 additions & 1 deletion routes/affirmationRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
17 changes: 11 additions & 6 deletions views/affirmation-setup.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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]?

0 comments on commit ed77a20

Please sign in to comment.