From 7f07e915dea1dcbed9a9f1009522813699e18ffc Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Sat, 8 Oct 2022 13:27:09 +0200 Subject: [PATCH] Diary Page Selector --- components/Blog/DiaryPageSelector.tsx | 74 +++++++++++++++++++++++ styles/Blog/DiaryPageSelector.module.scss | 0 2 files changed, 74 insertions(+) create mode 100644 components/Blog/DiaryPageSelector.tsx create mode 100644 styles/Blog/DiaryPageSelector.module.scss diff --git a/components/Blog/DiaryPageSelector.tsx b/components/Blog/DiaryPageSelector.tsx new file mode 100644 index 0000000..32e6d81 --- /dev/null +++ b/components/Blog/DiaryPageSelector.tsx @@ -0,0 +1,74 @@ +import type { NextPage } from "next"; +import Link from "next/link"; +import { useRouter } from "next/router"; +import type { ChangeEvent, ChangeEventHandler } from "react"; +import { useEffect } from "react"; + +import styles from "../../styles/Blog/DiaryPageSelector.module.scss"; + +interface IContentNavBar { + pages: string[]; + name: string; + title: string; + pageSelected: number +} + +interface IContentNav extends IContentNavBar { + bottom?: boolean; +} + +const PageSelectorBar: NextPage = ({ pages, name, title, pageSelected }) => { + const router = useRouter(); + + // When we are on the main page no previous page exists, otherwise we need to check if the previous page is the main page + const prevLink = pageSelected === 0 ? null : pageSelected > 1 ? `/blog/diary/${name}/${pageSelected - 1}` : `/blog/diary/${name}`; + const nextLink = pageSelected < pages.length ? `/blog/diary/${name}/${pageSelected + 1}` : null; + + useEffect(() => { + if (prevLink) router.prefetch(prevLink); + if (nextLink) router.prefetch(nextLink); + }, [nextLink, prevLink, router]); + + const onSelection: ChangeEventHandler = async (e: ChangeEvent) => { + const selected = Number.parseInt((e.target as HTMLSelectElement).value) || 0; + + const link = selected === 0 ? `/blog/diary/${name}` : `/blog/diary/${name}/${selected}`; + await router.push(link); + }; + + const prev = {prevLink ? < {pageSelected - 1 === 0 ? `${title} (Main Page)` : pages[pageSelected - 2]} : null}; + const next = {nextLink ? {pages[pageSelected]} > : null}; + + const select = ( + + ); + + return ( +
+ {prev} +   |   + {select} +   |   + {next} +
+ ); +}; + +const PageSelector: NextPage = (content) => { + + if (content.bottom) return ; + + return ( +
+

{content.title}

+
    + {content.pages.map((e, idx) =>
  • {e}
  • )} +
+
+ ); +}; + +export default PageSelector; \ No newline at end of file diff --git a/styles/Blog/DiaryPageSelector.module.scss b/styles/Blog/DiaryPageSelector.module.scss new file mode 100644 index 0000000..e69de29