-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
665 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { ArticleSummaryLarge } from '@/components/ArticleSummaryLarge'; | ||
import { ArticleSummarySmall } from '@/components/ArticleSummarySmall'; | ||
import { MainContainer } from '@/components/MainContainer'; | ||
import { db } from '@/utils/db'; | ||
import type { InferGetServerSidePropsType } from 'next'; | ||
import { type ParsedUrlQuery } from 'querystring'; | ||
import Link from 'next/link'; | ||
|
||
interface IParams extends ParsedUrlQuery { | ||
lang: string; | ||
} | ||
|
||
export async function getServerSideProps({ params }: { params: IParams }) { | ||
const { lang } = params; | ||
|
||
if (lang === 'en') { | ||
return { | ||
redirect: { | ||
destination: '/', | ||
permanent: true, | ||
}, | ||
}; | ||
} | ||
|
||
const articles = await db | ||
.selectFrom('articleTranslations as at') | ||
.innerJoin('articles as a', 'a.id', 'at.articleId') | ||
.innerJoin('articleImages as ai', 'a.articleImageId', 'ai.id') | ||
.select([ | ||
'a.id', | ||
'a.createdAt', | ||
'at.title', | ||
'at.slug', | ||
'at.body', | ||
'at.category', | ||
'at.language', | ||
'ai.imageUrl', | ||
]) | ||
.where('at.title', 'is not', null) | ||
.where('at.isPublished', '=', true) | ||
.where('at.language', '=', lang) | ||
.orderBy('a.createdAt', 'desc') | ||
.limit(25) | ||
.execute(); | ||
|
||
let today = new Date(); // get the current date | ||
let sevenDaysAgo = new Date(today); // create a copy of the current date | ||
|
||
sevenDaysAgo.setDate(today.getDate() - 7); // subtract 7 days | ||
|
||
const popularArticles = await db | ||
.selectFrom('articles') | ||
.innerJoin('articleImages', 'articles.articleImageId', 'articleImages.id') | ||
.select([ | ||
'articles.id', | ||
'articles.createdAt', | ||
'articles.title', | ||
'articles.slug', | ||
'articles.body', | ||
'articles.category', | ||
'articleImages.imageUrl', | ||
]) | ||
.where('title', 'is not', null) | ||
.where('isPublished', '=', true) | ||
.where('articles.createdAt', '>', sevenDaysAgo) | ||
.orderBy('pageViews', 'desc') | ||
.limit(8) | ||
.execute(); | ||
|
||
return { | ||
props: { | ||
articles, | ||
popularArticles, | ||
lang, | ||
}, | ||
}; | ||
} | ||
|
||
const Page = ( | ||
props: InferGetServerSidePropsType<typeof getServerSideProps>, | ||
) => { | ||
// get first three articles | ||
// const firstThreeArticles = props.articles.slice(0, 3); | ||
|
||
const { articles, popularArticles, lang } = props; | ||
|
||
return ( | ||
<MainContainer> | ||
{/* <HeaderIndex articles={firstThreeArticles} /> */} | ||
<div className="grid grid-cols-11 gap-6"> | ||
<div className="col-span-11 lg:col-span-7 bg-gray-50 shadow-md mb-12 divide-y divide-slate-200 last:pb-0 mt-12"> | ||
{articles.map((article, i) => { | ||
if (i % 5 === 0) { | ||
return ( | ||
<ArticleSummaryLarge article={article} key={article.slug} /> | ||
); | ||
} else { | ||
return ( | ||
<ArticleSummarySmall article={article} key={article.slug} /> | ||
); | ||
} | ||
})} | ||
</div> | ||
<div className="lg:col-span-4 mt-12 bg-gray-50 shadow-md p-4 self-start"> | ||
<div className="font-bold text-xl">Most Read</div> | ||
<div className="space-y-4 mt-4 pt-4 border-t"> | ||
{popularArticles.map((article) => { | ||
return ( | ||
<Link | ||
href={`/${lang}/${article.slug}`} | ||
key={article.id} | ||
className="space-x-4 flex" | ||
> | ||
<div | ||
className={`border border-gray-200 rounded-md w-20 h-20 flex-shrink-0 `} | ||
style={{ | ||
backgroundImage: `url(${article.imageUrl})`, | ||
backgroundPosition: 'center', | ||
backgroundSize: 'cover', | ||
}} | ||
/> | ||
<div> | ||
<div className="font-semibold font-serif group-hover:text-gray-500 text-sm"> | ||
{article.title} | ||
</div> | ||
<div className="flex mr-6 mt-3"> | ||
<p className="text-cyan-700 text-xs"> | ||
{article.category} | ||
</p> | ||
</div> | ||
</div> | ||
</Link> | ||
); | ||
})} | ||
</div> | ||
</div> | ||
</div> | ||
</MainContainer> | ||
); | ||
}; | ||
|
||
export default Page; |
Oops, something went wrong.