Skip to content

Commit

Permalink
Add breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
maryamaljanabi committed Feb 24, 2021
1 parent 19a8195 commit 01bfc7a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/node_modules
.env
.package-lock.json
.adminbro
/.adminbro
24 changes: 22 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ app.use(async (req, res, next) => {
}
});

// add breadcrumbs
get_breadcrumbs = function (url) {
var rtn = [{ name: "Home", url: "/" }],
acc = "", // accumulative url
arr = url.substring(1).split("/");

for (i = 0; i < arr.length; i++) {
acc = i != arr.length - 1 ? acc + "/" + arr[i] : null;
rtn[i + 1] = {
name: arr[i].charAt(0).toUpperCase() + arr[i].slice(1),
url: acc,
};
}
return rtn;
};
app.use(function (req, res, next) {
req.breadcrumbs = get_breadcrumbs(req.originalUrl);
next();
});

//routes config
const indexRouter = require("./routes/index");
const productsRouter = require("./routes/products");
Expand All @@ -87,10 +107,10 @@ app.use(function (err, req, res, next) {
res.render("error");
});

var port = process.env.PORT || 5000;
var port = process.env.PORT || 3000;
app.set("port", port);
app.listen(port, () => {
console.log("Server online...");
console.log("Server running at port " + port);
});

module.exports = app;
5 changes: 5 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ hr.gradient-style {
color: var(--dark-blue);
}

/* BREADCRUMBS STYLE */
.breadcrumb-item a:hover {
color: var(--dark-blue) !important;
}

/* SHOPPING CART STYLES */
.container.cart {
margin: 1em auto;
Expand Down
3 changes: 3 additions & 0 deletions routes/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ router.get("/", async (req, res) => {
successMsg,
errorMsg,
current: page,
breadcrumbs: null,
home: "/products/?",
pages: Math.ceil(count / perPage),
});
Expand Down Expand Up @@ -60,6 +61,7 @@ router.get("/search", async (req, res) => {
successMsg,
errorMsg,
current: page,
breadcrumbs: null,
home: "/products/search?search=" + req.query.search + "&",
pages: Math.ceil(count / perPage),
});
Expand Down Expand Up @@ -92,6 +94,7 @@ router.get("/:slug", async (req, res) => {
successMsg,
errorMsg,
current: page,
breadcrumbs: req.breadcrumbs,
home: "/products/" + req.params.slug.toString() + "/?",
pages: Math.ceil(count / perPage),
});
Expand Down
16 changes: 16 additions & 0 deletions views/shop/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@
<% } %>
</div>
</div>
<!-- Breadcrumbs -->
<div class="row">
<div class="col-md-12 m-auto">
<%if (breadcrumbs) { %>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<% breadcrumbs.forEach(crumb => { if(!crumb.name.startsWith("?")){%>
<li class="breadcrumb-item" aria-current="page">
<a href="<%=crumb.url%>"><%= crumb.name %></a>
</li>
<%}})%>
</ol>
</nav>
<%} %>
</div>
</div>
<!-- Page banner image -->
<% if(pageName == 'All Products') { %>
<div
Expand Down

0 comments on commit 01bfc7a

Please sign in to comment.