Add video and guides on homepage

This commit is contained in:
Kamran Ahmed
2021-08-29 18:57:23 +02:00
parent 0e90d53b8e
commit 74ef38cdb6
6 changed files with 222 additions and 54 deletions

14
lib/author.ts Normal file
View File

@@ -0,0 +1,14 @@
import authors from '../content/authors.json';
export type AuthorType = {
username: string;
name: string;
twitter: string;
picture: string;
bio: string;
}
export function findAuthorByUsername(username: string): AuthorType | undefined {
return (authors as AuthorType[]).find(author => author.username === username);
}

View File

@@ -1,6 +1,7 @@
import guides from '../content/guides.json';
import authors from '../content/authors.json';
import formatDate from 'date-fns/format';
import { NextApiRequest } from 'next';
import { AuthorType, findAuthorByUsername } from './author';
export type GuideType = {
title: string;
@@ -8,12 +9,13 @@ export type GuideType = {
url: string;
fileName: string;
isPro: boolean;
author: string;
isDraft: boolean;
createdAt: string;
updatedAt: string;
formattedCreatedAt: string;
formattedUpdatedAt: string;
authorUsername: string;
author?: AuthorType;
};
export function getAllGuides(limit: number = 0): GuideType[] {
@@ -27,3 +29,23 @@ 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;
}

44
lib/video.ts Normal file
View File

@@ -0,0 +1,44 @@
import videos from '../content/videos.json';
import formatDate from 'date-fns/format';
import { NextApiRequest } from 'next';
export type VideoType = {
title: string;
description: string;
url: string;
fileName: string;
isPro: boolean;
duration: string;
createdAt: string;
updatedAt: string;
formattedCreatedAt: string;
formattedUpdatedAt: string;
};
export function getAllVideos(limit: number = 0): VideoType[] {
return (videos as VideoType[])
.sort((a, b) => (new Date(b.updatedAt) as any) - (new Date(a.updatedAt) as any))
.map(video => ({
...video,
formattedCreatedAt: formatDate(new Date(video.createdAt), 'MMMM d, yyyy'),
formattedUpdatedAt: formatDate(new Date(video.updatedAt), 'MMMM d, yyyy')
}))
.slice(0, limit ? limit : videos.length);
}
export function getRequestedGuide(req: NextApiRequest): VideoType | undefined {
const allVideos = getAllVideos();
const video = allVideos.find(video => video.url === req.url);
if (!video) {
return undefined;
}
try {
return video;
} catch (e) {
console.log(e);
}
return undefined;
}