Skip to content

Commit

Permalink
author name on articles, error page
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalnjs committed Oct 9, 2023
1 parent 45d82ac commit d46bbdd
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 23 deletions.
2 changes: 1 addition & 1 deletion frontend/pages/artworks.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<a href="<%- vars.domain %>/artworks/<%= artwork.slug %>">
<h3><%= artwork.title %></h3>
</a>
<p><a href="<%- vars.domain %>/tags/<%= artwork.tags[0].toLowerCase() %>"><%= artwork.tags[0] %></a> / <% var date = new Date(artwork.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %><% if ((artwork.author != "") && (artwork.author != null)) { %> / <%= artwork.author %><% } %></p>
<p><a href="<%- vars.domain %>/tags/<%= article.tags[0].toLowerCase() %>"><%= article.tags[0] %></a> / <% var date = new Date(article.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %><% if ((article.author != "") && (article.author != null)) { %> / <%= article.author %><% } %></p>
</div>
</div>
<% } %>
Expand Down
67 changes: 67 additions & 0 deletions frontend/pages/error.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Error</title>
<style>
body {
background-color: #f8f8f8;
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
color: #333;
height: 100vh;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
}
.container {
width: 60vw;
max-width: 600px;
margin: 0 auto;
padding: 40px 75px 45px 50px;
background-color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 36px;
margin: 5px 0 0 0;
}
p {
margin: 10px 0 0 0;
}
ul {
list-style: none;
padding-left: 4px;
}
i {
width: 16px;
margin-right: 5px;
text-align: end;
}
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>

<body>
<div class="container">
<h1>🏗️</h1>
<h1>Error</h1>
<p>Sorry, something went wrong and we are unable to load this app. Specifically, there was an <%= error %>. Please try again later or contact support.</p>
<ul>
<li><i class="fa-solid fa-database"></i> Database: <% if (error.includes("database")) { %>Server may be down, subscription may be expired, or host panel changed.<% } else { %>Checks passed, all looks good!<% } %></li>
<li><i class="fa-solid fa-server"></i> CMS: <% if (error.includes("CMS")) { %>Server may be down, subscription may be expired, or API access denied.<% } else { %>Checks passed, all looks good!<% } %></li>
<li><i class="fa-solid fa-chart-simple"></i> Google Analytics: <% if (error.includes("page views")) { %>Monthly limit may be reached, or property ID changed.<% } else { %>Checks passed, all looks good!<% } %></li>
</ul>
</div>
</body>

</html>
4 changes: 2 additions & 2 deletions frontend/pages/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<h3><%= article.title %></h3>
</a>
<h4><%= article.description %></h4>
<p><a href="<%- vars.domain %>/tags/<%= article.tags[0].toLowerCase() %>"><%= article.tags[0] %></a> / <% var date = new Date(article.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %></p>
<p><a href="<%- vars.domain %>/tags/<%= article.tags[0].toLowerCase() %>"><%= article.tags[0] %></a> / <% var date = new Date(article.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %><% if ((article.author != "") && (article.author != null)) { %> / <%= article.author %><% } %></p>
</div>
</div>
<% } %>
Expand All @@ -60,7 +60,7 @@
<a href="<%- vars.domain %>/artworks/<%= artwork.slug %>">
<h3><%= artwork.title %></h3>
</a>
<p><a href="<%- vars.domain %>/tags/<%= artwork.tags[0].toLowerCase() %>"><%= artwork.tags[0] %></a> / <% var date = new Date(artwork.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %><% if ((artwork.author != "") && (artwork.author != null)) { %> / <%= artwork.author %><% } %></p>
<p><a href="<%- vars.domain %>/tags/<%= article.tags[0].toLowerCase() %>"><%= article.tags[0] %></a> / <% var date = new Date(article.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %><% if ((article.author != "") && (article.author != null)) { %> / <%= article.author %><% } %></p>
</div>
</div>
<% } %>
Expand Down
6 changes: 3 additions & 3 deletions frontend/pages/search.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%- include('../partials/head.ejs'); %>
<% const filteredArticles = cms.articles.filter(article => {
const title = article.title.toLowerCase();
const description = article.description.toLowerCase();
const description = (article.description || "").toLowerCase();
const content = article.content.toLowerCase();
return title.includes(query) || description.includes(query) || content.includes(query);
});
Expand Down Expand Up @@ -62,7 +62,7 @@
<h3>Article: <%= article.title %></h3>
</a>
<h4><%= article.description %></h4>
<p><a href="<%- vars.domain %>/tags/<%= article.tags[0].toLowerCase() %>"><%= article.tags[0] %></a> / <% var date = new Date(article.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %></p>
<p><a href="<%- vars.domain %>/tags/<%= article.tags[0].toLowerCase() %>"><%= article.tags[0] %></a> / <% var date = new Date(article.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %><% if ((article.author != "") && (article.author != null)) { %> / <%= article.author %><% } %></p>
</div>
</div>
<% }); %>
Expand Down Expand Up @@ -93,7 +93,7 @@
<a href="<%- vars.domain %>/artworks/<%= artwork.slug %>">
<h3>Artwork: <%= artwork.title %></h3>
</a>
<p><a href="<%- vars.domain %>/tags/<%= artwork.tags[0].toLowerCase() %>"><%= artwork.tags[0] %></a> / <% var date = new Date(artwork.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %><% if ((artwork.author != "") && (artwork.author != null)) { %> / <%= artwork.author %><% } %></p>
<p><a href="<%- vars.domain %>/tags/<%= article.tags[0].toLowerCase() %>"><%= article.tags[0] %></a> / <% var date = new Date(article.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %><% if ((article.author != "") && (article.author != null)) { %> / <%= article.author %><% } %></p>
</div>
</div>
<% }); %>
Expand Down
12 changes: 4 additions & 8 deletions frontend/partials/suggested.ejs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
<% function ifYesterday(date) {
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
return date.toDateString() === yesterday.toDateString();
}; %>
<% var currentSlug = article.slug; %>
<div class="articles">
<h2>Suggested articles</h2>
<div class="grid">
<% for (article of cms.articles.sort(() => Math.random() - 0.5).slice(0, 3)) { %>
<div class="article"<% if (article.images[0] != null) { %> style="background-image: url('<%- vars.asset_prefix %><%= article.images[0].path %>')"<% } %>>
<% for (article of cms.articles.filter(a => a.slug !== currentSlug).sort(() => Math.random() - 0.5).slice(0, 3)) { %>
<div class="article" <% if (article.images[0] != null) { %> style="background-image: url('<%- vars.asset_prefix %><%= article.images[0].path %>')" <% } %>>
<div class="inner">
<a href="<%- vars.domain %>/articles/<%= article.slug %>">
<h3><%= article.title %></h3>
</a>
<h4><%= article.description %></h4>
<p><a href="<%- vars.domain %>/tags/<%= article.tags[0].toLowerCase() %>"><%= article.tags[0] %></a> / <% var date = new Date(article.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %></p>
<p><a href="<%- vars.domain %>/tags/<%= article.tags[0].toLowerCase() %>"><%= article.tags[0] %></a> / <% var date = new Date(article.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %><% if ((article.author != "") && (article.author != null)) { %> / <%= article.author %><% } %></p>
</div>
</div>
<% } %>
Expand Down
2 changes: 1 addition & 1 deletion frontend/public/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ footer .inner .bottom a {
.articles.searchResults .grid {
grid-template-columns: repeat(1, 1fr) !important;
grid-template-areas: "." !important;
gap: 10vh;
gap: 1vh;
}

.articles.searchResults .grid .article {
Expand Down
29 changes: 21 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ app.use((req, res, next) => {
.then(() => {
db.query('INSERT INTO pageviews (url, count) VALUES (?, 1) ON DUPLICATE KEY UPDATE count = IF(TIMESTAMPDIFF(HOUR, timestamp, NOW()) <= 1, count, count + 1), timestamp = IF(TIMESTAMPDIFF(HOUR, timestamp, NOW()) <= 1, timestamp, NOW());', [req.originalUrl], (err, results) => {
if (err) {
console.error('Error tracking pageview:', err);
console.error('error tracking pageview:', err);
};
});
})
.catch((err) => {
console.error('Error tracking pageview:', err);
console.error('error tracking pageview:', err);
});
}
next();
Expand All @@ -73,14 +73,14 @@ app.use((req, res, next) => {
db.query('SELECT count FROM pageviews WHERE url = ?', [req.originalUrl], (err, results) => {
if (err) {
console.error(err);
res.status(500).send('Error retrieving page views');
res.status(500).render('error', { error: 'error retrieving page views; database connection failed' });
return;
}
};
if (results.length === 1) {
req.pageViews = results[0].count;
} else {
req.pageViews = 0;
}
};
next();
});
});
Expand All @@ -89,12 +89,19 @@ app.use((err, req, res, next) => {
return next(err);
};
if (process.env.NODE_ENV === 'production') {
return res.status(500).render('error');
return res.status(500).render('error', { error: 'internal server error; check logs for more information' });
} else {
console.error(err.stack);
return res.status(500).send(err.stack);
};
});
app.use(async (req, res, next) => {
if (!(await cmsdata()).ok) {
res.status(500).render('error', { error: 'error connecting to CMS; CMS connection failed' });
return;
};
next();
});

function cmsdata() {
return fetch('https://cms.dangoweb.com/:southern-bell/api/gql', {
Expand All @@ -120,7 +127,9 @@ function cmsdata() {
};

async function startApp() {
var cms = JSON.parse(JSON.stringify((await cmsdata().then(res => res.json())).data).replaceAll('.spaces', 'clients'));
try {
var cms = JSON.parse(JSON.stringify((await cmsdata().then(res => res.json())).data).replaceAll('.spaces', 'clients'));
} catch { };

// Defaults & Environment Variables

Expand Down Expand Up @@ -450,7 +459,11 @@ async function startApp() {
});

app.listen(port, () => {
console.log(`${cms.siteDetails[0].title} listening on port ${port}`);
try {
console.log(`${cms.siteDetails[0].title} listening on port ${port}`);
} catch {
console.log(`Southern Bell listening on port ${port}`);
};
});
}

Expand Down

0 comments on commit d46bbdd

Please sign in to comment.