Add videos listing page
This commit is contained in:
17
lib/guide.ts
17
lib/guide.ts
@@ -1,6 +1,5 @@
|
||||
import guides from '../content/guides.json';
|
||||
import formatDate from 'date-fns/format';
|
||||
import { NextApiRequest } from 'next';
|
||||
import { AuthorType, findAuthorByUsername } from './author';
|
||||
|
||||
export type GuideType = {
|
||||
@@ -13,23 +12,16 @@ export type GuideType = {
|
||||
isDraft: boolean;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
formattedCreatedAt: string;
|
||||
formattedUpdatedAt: string;
|
||||
formattedCreatedAt?: string;
|
||||
formattedUpdatedAt?: string;
|
||||
authorUsername: string;
|
||||
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)
|
||||
};
|
||||
return allGuides.find(guide => guide.id === id);
|
||||
}
|
||||
|
||||
export function getAllGuides(limit: number = 0): GuideType[] {
|
||||
@@ -39,7 +31,8 @@ export function getAllGuides(limit: number = 0): GuideType[] {
|
||||
.map(guide => ({
|
||||
...guide,
|
||||
formattedCreatedAt: formatDate(new Date(guide.createdAt), 'MMMM d, yyyy'),
|
||||
formattedUpdatedAt: formatDate(new Date(guide.updatedAt), 'MMMM d, yyyy')
|
||||
formattedUpdatedAt: formatDate(new Date(guide.updatedAt), 'MMMM d, yyyy'),
|
||||
author: findAuthorByUsername(guide.authorUsername)
|
||||
}))
|
||||
.slice(0, limit ? limit : guides.length);
|
||||
}
|
||||
|
27
lib/video.ts
27
lib/video.ts
@@ -1,8 +1,9 @@
|
||||
import videos from '../content/videos.json';
|
||||
import formatDate from 'date-fns/format';
|
||||
import { NextApiRequest } from 'next';
|
||||
import { AuthorType, findAuthorByUsername } from './author';
|
||||
|
||||
export type VideoType = {
|
||||
id: string;
|
||||
title: string;
|
||||
description: string;
|
||||
url: string;
|
||||
@@ -11,8 +12,10 @@ export type VideoType = {
|
||||
duration: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
formattedCreatedAt: string;
|
||||
formattedUpdatedAt: string;
|
||||
formattedCreatedAt?: string;
|
||||
formattedUpdatedAt?: string;
|
||||
authorUsername: string;
|
||||
author?: AuthorType;
|
||||
};
|
||||
|
||||
export function getAllVideos(limit: number = 0): VideoType[] {
|
||||
@@ -21,24 +24,14 @@ export function getAllVideos(limit: number = 0): VideoType[] {
|
||||
.map(video => ({
|
||||
...video,
|
||||
formattedCreatedAt: formatDate(new Date(video.createdAt), 'MMMM d, yyyy'),
|
||||
formattedUpdatedAt: formatDate(new Date(video.updatedAt), 'MMMM d, yyyy')
|
||||
formattedUpdatedAt: formatDate(new Date(video.updatedAt), 'MMMM d, yyyy'),
|
||||
author: findAuthorByUsername(video.authorUsername)
|
||||
}))
|
||||
.slice(0, limit ? limit : videos.length);
|
||||
}
|
||||
|
||||
|
||||
export function getRequestedGuide(req: NextApiRequest): VideoType | undefined {
|
||||
export function getVideoById(id: string): 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;
|
||||
return allVideos.find(guide => guide.id === id);
|
||||
}
|
||||
|
Reference in New Issue
Block a user