refactor: refactor the controllers a bit to make them more consistent

This commit is contained in:
zyachel 2022-06-12 12:34:18 +05:30
parent 719988c587
commit 7f0406f054
5 changed files with 45 additions and 17 deletions

View file

@ -27,10 +27,14 @@ const sendErrorResponse = (err, req, res, devMode = false) => {
// 2. FOR WEBPAGES
else
res.status(err.statusCode).render('error', {
title: 'Error',
statusCode: err.statusCode,
message: err.message,
...(devMode && { stack: err.stack }),
data: {
statusCode: err.statusCode,
message: err.message,
...(devMode && { stack: err.stack }),
},
meta: {
title: 'Error',
},
});
};

View file

@ -11,10 +11,19 @@ import { nonSlugRoutes } from '../utils/constants.js';
// EXPORTS
////////////////////////////////////////////////////////
export const about = (req, res, next) => {
res.render('about', { title: 'About' });
res.render('about', {
meta: {
title: 'About',
},
});
};
export const privacy = (req, res, next) => {
res.render('privacy', { title: 'Privacy' });
res.render('privacy', {
meta: {
title: 'Privacy',
},
});
};
export const answers = catchAsyncErrors(async (req, res, next) => {
@ -23,23 +32,37 @@ export const answers = catchAsyncErrors(async (req, res, next) => {
if (nonSlugRoutes.includes(slug)) return next();
const answersData = await getAnswers(slug);
const title = answersData.question.text.spans.map(span => span.text).join('');
res.status(200).render('answers', {
title: answersData.question.text.spans.map(span => span.text).join(''),
data: answersData,
meta: {
title,
},
});
});
export const topic = catchAsyncErrors(async (req, res, next) => {
const topicData = await getTopic(req.params.slug);
res.status(200).render('topic', { title: topicData.name, data: topicData });
res.status(200).render('topic', {
data: topicData,
meta: {
title: topicData.name,
},
});
});
export const unimplemented = (req, res, next) => {
const message =
"This route isn't yet implemented. Check back sometime later!";
res.status(501).render('error', {
title: 'Not yet implemented',
statusCode: 501,
message: "This route isn't yet implemented. Check back sometime later!",
data: {
statusCode: 501,
message,
},
meta: {
title: 'Not yet implemented',
},
});
};

View file

@ -1,4 +1,4 @@
header.header(class=`${title === 'About' ? 'header__about': ''}`)
header.header(class=`${meta.title === 'About' ? 'header__about': ''}`)
.header__bar
a.header__link.header__logo(href='/') Quetre

View file

@ -6,7 +6,8 @@ html(lang='en')
meta(http-equiv='X-UA-Compatible', content='IE=edge')
meta(name='viewport', content='width=device-width, initial-scale=1.0')
//- appending title to name in case it exists, else showing name as the title of the page
title= title ? `${title} | Quetre`: 'Quetre'
- const pageTitle = meta.title ? `${meta.title} | Quetre`: 'Quetre'
title= pageTitle
meta(name="description", content="A libre front-end for Quora. See any answer without being tracked, without being required to log in, and without being bombarded by pesky ads.")
//- preloading css and fonts for performance gains
link(rel='preload', href='/fonts/WorkSans.woff2', as='font', type='font/woff2', crossorigin)

View file

@ -2,13 +2,13 @@ extends base
block content
main#main.main.error
p.error__code= statusCode
p.error__message= message
p.error__code= data.statusCode
p.error__message= data.message
//- will only apply in dev mode
if stack
if data.stack
.error__stack-box: pre.error__stack
code.error__text= stack
code.error__text= data.stack
p.error__return Go back to the 
a.error__link(href="/") Home Page