From 91a3a41ee43cfebbaeb8f9f4aa0969479a2952c2 Mon Sep 17 00:00:00 2001 From: developomp Date: Mon, 2 Aug 2021 10:02:29 +0900 Subject: [PATCH] added next and prev button --- source/src/pages/Page.tsx | 140 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 133 insertions(+), 7 deletions(-) diff --git a/source/src/pages/Page.tsx b/source/src/pages/Page.tsx index c7ee886..e294f50 100644 --- a/source/src/pages/Page.tsx +++ b/source/src/pages/Page.tsx @@ -1,47 +1,160 @@ import React from "react" import marked from "marked" import { Helmet } from "react-helmet-async" +import { Link } from "react-router-dom" import styled from "styled-components" -import posts from "../data/map.json" +import map from "../data/map.json" import Tag from "../components/Tag" import NotFound from "./NotFound" import Spinner from "../components/Spinner" +import theming from "../theming" + const StyledTitle = styled.h1` margin-bottom: 1rem; ` +const StyledNextPrevContainer = styled.div` + display: flex; + justify-content: space-between; + size: 100%; +` + +const StyledLink = styled(Link)` + ${theming.styles.navbarButtonStyle} + + background-color: ${(props) => + theming.theme(props.theme.currentTheme, { + light: "#EEEEEE", + dark: "#202225", + })}; + + height: 1rem; + width: 2rem; + margin-top: 2rem; + + line-height: 1rem; + text-align: center; +` + +const StyledDisabledLink = styled.div` + font-size: 1rem; + border-radius: 0.5rem; + float: left; + padding: 14px 16px; + text-decoration: none; + transition: transform 0.1s linear; + color: grey; + background-color: ${(props) => + theming.theme(props.theme.currentTheme, { + light: "#EEEEEE", + dark: "#202225", + })}; + + height: 1rem; + width: 2rem; + margin-top: 2rem; + + line-height: 1rem; + text-align: center; + user-select: none; +` + interface PageProps {} interface PageState { // eslint-disable-next-line @typescript-eslint/no-explicit-any fetchedPage: any isUnsearchable: boolean + isSeries: boolean + seriesData: { + seriesHome: string + prev: string | null + next: string | null + } | null loading: boolean } +interface NextPrevProps { + prevURL: string | null + nextURL: string | null +} + +class NextPrev extends React.Component { + render() { + return ( + + {this.props.prevURL ? ( + prev + ) : ( + prev + )} + {this.props.nextURL ? ( + next + ) : ( + next + )} + + ) + } +} + export default class Page extends React.Component { constructor(props) { super(props) this.state = { - isUnsearchable: false, fetchedPage: undefined, + isUnsearchable: false, + isSeries: false, + seriesData: null, loading: true, } } async componentDidMount() { - const url = location.pathname.replace(/\/$/, "") // remove trailing slash let _isUnsearchable = false + let _isSeries = false + + const url = location.pathname.replace(/\/$/, "") // remove trailing slash + + if (url.startsWith("/series")) _isSeries = true + + if (_isSeries) { + const seriesURL = url.slice(0, url.lastIndexOf("/")) + if (seriesURL in map.series) { + const _curr: number = map.series[seriesURL].order.indexOf(url) + let _prev: number | null = _curr - 1 + let _next: number | null = _curr + 1 + + if (_prev < 0) _prev = null + if (_next > map.series[seriesURL].order.length - 1) _next = null + + this.setState({ + seriesData: { + seriesHome: seriesURL, + prev: + _prev != null + ? map.series[seriesURL].order[_prev] + : null, + next: + _next != null + ? map.series[seriesURL].order[_next] + : null, + }, + }) + } + } // fetch page - let fetchedPage = posts.posts[url] + let fetchedPage = map.posts[url] if (!fetchedPage) { - fetchedPage = posts.unsearchable[url] + fetchedPage = map.unsearchable[url] + _isUnsearchable = true this.setState({ isUnsearchable: true }) + if (!fetchedPage) { this.setState({ loading: false, @@ -64,6 +177,7 @@ export default class Page extends React.Component { } this.setState({ + isSeries: _isSeries, fetchedPage: fetchedPage, loading: false, }) @@ -91,7 +205,20 @@ export default class Page extends React.Component { /> -
+
+ {this.state.isSeries ? ( + + ) : ( + <> + )} {this.state.fetchedPage.title} @@ -121,7 +248,6 @@ export default class Page extends React.Component { <>Published on {this.state.fetchedPage.date} )} - {/* Horizontal Separator */}
{