Make the guides page working
This commit is contained in:
34
lib/guide.ts
34
lib/guide.ts
@ -4,6 +4,7 @@ import { NextApiRequest } from 'next';
|
||||
import { AuthorType, findAuthorByUsername } from './author';
|
||||
|
||||
export type GuideType = {
|
||||
id: string;
|
||||
title: string;
|
||||
description: string;
|
||||
url: string;
|
||||
@ -18,6 +19,19 @@ export type GuideType = {
|
||||
author?: AuthorType;
|
||||
};
|
||||
|
||||
export function getGuideById(id: string): GuideType | undefined {
|
||||
const allGuides = getAllGuides();
|
||||
const foundGuide = allGuides.find(guide => guide.id === id);
|
||||
if (!foundGuide) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
...foundGuide,
|
||||
author: findAuthorByUsername(foundGuide.authorUsername)
|
||||
};
|
||||
}
|
||||
|
||||
export function getAllGuides(limit: number = 0): GuideType[] {
|
||||
return (guides as GuideType[])
|
||||
.filter(guide => !guide.isDraft)
|
||||
@ -29,23 +43,3 @@ export function getAllGuides(limit: number = 0): GuideType[] {
|
||||
}))
|
||||
.slice(0, limit ? limit : guides.length);
|
||||
}
|
||||
|
||||
|
||||
export function getRequestedGuide(req: NextApiRequest): GuideType | undefined {
|
||||
const allGuides = getAllGuides();
|
||||
const guide = allGuides.find(guide => guide.url === req.url);
|
||||
if (!guide) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
return {
|
||||
...guide,
|
||||
author: findAuthorByUsername(guide.authorUsername)
|
||||
};
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
Reference in New Issue
Block a user