-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(website): show blog posts on
/blog
route (#76)
finally..
- Loading branch information
Showing
25 changed files
with
594 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
"use client"; | ||
|
||
import { | ||
useContext, | ||
useEffect, | ||
type FC, | ||
} from "react"; | ||
import { api, IsNotFoundContext } from "@khenzii-dev/providers"; | ||
import { | ||
Loading, | ||
Paragraph, | ||
Button, | ||
Icon, | ||
Anchor, | ||
Header, | ||
Flex, | ||
} from "@khenzii-dev/ui/atoms"; | ||
import { MarkdownRenderer } from "@khenzii-dev/ui/molecules"; | ||
import { Tags } from "@khenzii-dev/ui/organisms"; | ||
import { blogTagToUiTag } from "@khenzii-dev/utils"; | ||
import { useMobile } from "@khenzii-dev/hooks"; | ||
import style from "@khenzii-dev/styles/blog_post.module.scss"; | ||
|
||
const formatDate = (date: Date): string => { | ||
const day = date.getDate().toString().padStart(2, "0"); | ||
const month = (date.getMonth() + 1).toString().padStart(2, "0"); | ||
const year = date.getFullYear(); | ||
const hour = date.getHours().toString().padStart(2, "0"); | ||
const minute = date.getMinutes().toString().padStart(2, "0"); | ||
|
||
return `${day}/${month}/${year} - ${hour}:${minute}`; | ||
}; | ||
|
||
type BlogPostProps = { | ||
params: { id: string }; | ||
}; | ||
|
||
const BlogPost: FC<BlogPostProps> = ({ params }) => { | ||
const { setIsNotFound } = useContext(IsNotFoundContext); | ||
const mobile = useMobile(); | ||
|
||
const { | ||
data: postData, | ||
isLoading: postIsLoading, | ||
isError: postError, | ||
} = api.blog.blogPost.getPost.useQuery({ | ||
id: params.id, | ||
}); | ||
const { data: tagsData } = api.blog.blogTag.getTags.useQuery(); | ||
|
||
useEffect(() => { | ||
if (postIsLoading) return; | ||
|
||
if (postError || postData === null || postData === undefined) { | ||
setIsNotFound(true); | ||
return; | ||
} | ||
|
||
return () => setIsNotFound(false); | ||
}, [ | ||
postIsLoading, | ||
postError, | ||
postData, | ||
setIsNotFound, | ||
]); | ||
|
||
if (postIsLoading) return ( | ||
<Loading size={200} /> | ||
); | ||
|
||
if (postError) return ( | ||
<Flex | ||
direction={"column"} | ||
align={"center"} | ||
styles={{ paddingInline: "10px" }} | ||
> | ||
<Header>Error!</Header> | ||
<Paragraph | ||
fontSize={1.5} | ||
styles={{ textAlign: "center" }} | ||
> | ||
{"Something went wrong while trying to fetch the post!"} | ||
</Paragraph> | ||
|
||
<Anchor href={"/"} prefetch> | ||
<Button style={{ width: "fit-content", padding: "10px" }}> | ||
<Icon iconName={"house"} size={2.5} /> | ||
</Button> | ||
</Anchor> | ||
</Flex> | ||
); | ||
|
||
if (postData === null || postData === undefined) return ( | ||
<Paragraph fontSize={1.5} styles={{ textAlign: "center" }}> | ||
{"This route doesn't exist."} | ||
</Paragraph> | ||
); | ||
|
||
return ( | ||
<Flex | ||
direction={"column"} | ||
className={style.post_container} | ||
> | ||
<Anchor href={"/blog"} styles={{ width: "fit-content" }} prefetch> | ||
<Button style={{ width: "fit-content", padding: "10px" }}> | ||
<Icon iconName={"arrow-left-short"} size={mobile ? 1.75 : 2.5} /> | ||
</Button> | ||
</Anchor> | ||
|
||
<Flex direction={mobile ? "column" : "row"}> | ||
<Paragraph | ||
fontSize={mobile ? 2 : 3} | ||
styles={{ lineBreak: "auto", hyphens: "auto" }} | ||
> | ||
{postData.title} | ||
</Paragraph> | ||
|
||
<hr className={style.post_line} /> | ||
|
||
<Flex align={"center"}> | ||
<Icon iconName={"clock"} size={mobile ? 1.5 : 2} /> | ||
<Paragraph fontSize={mobile ? 1.5 : 2}>{formatDate(postData.created_at)}</Paragraph> | ||
</Flex> | ||
</Flex> | ||
|
||
<Tags | ||
tags={blogTagToUiTag(postData.tagIDs, tagsData)} | ||
size={mobile ? 1.5 : 2} | ||
clickable={false} | ||
showOnlyActive | ||
/> | ||
|
||
<MarkdownRenderer sizeMultiplier={mobile ? 1 : 1.25}>{postData.content}</MarkdownRenderer> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export default BlogPost; | ||
|
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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import { Paragraph } from "@khenzii-dev/ui/atoms"; | ||
import type { Metadata } from "next"; | ||
import { Posts } from "@khenzii-dev/ui/organisms"; | ||
|
||
export const metadata: Metadata = { | ||
title: "Blog", | ||
}; | ||
|
||
const Blog = () => ( | ||
<Paragraph fontSize={1.5} styles={{ textAlign: "center" }}> | ||
This page is not yet available! | ||
</Paragraph> | ||
<Posts /> | ||
); | ||
|
||
export default Blog; |
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 |
---|---|---|
@@ -1,24 +1,38 @@ | ||
"use client"; | ||
|
||
import { useContext, useEffect } from "react"; | ||
import { IsNotFoundContext } from "@khenzii-dev/providers"; | ||
import { Footer, Nav } from "@khenzii-dev/ui/organisms"; | ||
import { Paragraph } from "@khenzii-dev/ui/atoms"; | ||
import style from "@khenzii-dev/styles/layout_website.module.scss"; | ||
|
||
const NotFound = () => ( | ||
<> | ||
<nav className={style.nav}> | ||
<Nav/> | ||
</nav> | ||
const NotFound = () => { | ||
const { setIsNotFound } = useContext(IsNotFoundContext); | ||
|
||
useEffect(() => { | ||
setIsNotFound(true); | ||
|
||
<main className={style.content}> | ||
<Paragraph fontSize={1.5} styles={{ textAlign: "center" }}> | ||
{"This route doesn't exist."} | ||
</Paragraph> | ||
</main> | ||
return () => setIsNotFound(false); | ||
}, [setIsNotFound]); | ||
|
||
<footer className={style.footer}> | ||
<Footer/> | ||
</footer> | ||
</> | ||
); | ||
return ( | ||
<> | ||
<nav className={style.nav}> | ||
<Nav/> | ||
</nav> | ||
|
||
<main className={style.content}> | ||
<Paragraph fontSize={1.5} styles={{ textAlign: "center" }}> | ||
{"This route doesn't exist."} | ||
</Paragraph> | ||
</main> | ||
|
||
<footer className={style.footer}> | ||
<Footer/> | ||
</footer> | ||
</> | ||
); | ||
}; | ||
|
||
export default NotFound; | ||
|
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./trpc_provider"; | ||
export * from "./session_provider_wrapper"; | ||
export * from "./is_not_found_provider"; |
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,36 @@ | ||
"use client"; | ||
|
||
import { | ||
createContext, | ||
useState, | ||
type Dispatch, | ||
type SetStateAction, | ||
type ReactNode, | ||
type FC, | ||
} from "react"; | ||
|
||
export type IsNotFoundContextProps = { | ||
isNotFound: boolean; | ||
setIsNotFound: Dispatch<SetStateAction<boolean>>; | ||
}; | ||
|
||
export const IsNotFoundContext = createContext<IsNotFoundContextProps>({ | ||
isNotFound: false, | ||
setIsNotFound: () => { | ||
console.log("IsNotFoundContext needs to be initialized before usage!"); | ||
}, | ||
}); | ||
|
||
export type IsNotFoundProviderProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
export const IsNotFoundProvider: FC<IsNotFoundProviderProps> = ({ children }) => { | ||
const [isNotFound, setIsNotFound] = useState(false); | ||
|
||
return ( | ||
<IsNotFoundContext.Provider value={{ isNotFound, setIsNotFound }}> | ||
{children} | ||
</IsNotFoundContext.Provider> | ||
); | ||
}; |
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
Oops, something went wrong.