simplified PostCard logic and fixed bug

- simplified locale-specific url generation logic for `PostCard`
- fixed bug where navigation fails on series contents
This commit is contained in:
Kim, Jimin 2022-04-17 22:24:12 +09:00
parent d1a33ccf1e
commit ac959933bb
6 changed files with 40 additions and 28 deletions

View file

@ -1,3 +1,4 @@
import { useContext } from "react"
import styled from "styled-components" import styled from "styled-components"
import { Link } from "react-router-dom" import { Link } from "react-router-dom"
@ -15,6 +16,7 @@ import TagList from "./TagList"
import MainContent from "./MainContent" import MainContent from "./MainContent"
import theming from "../styles/theming" import theming from "../styles/theming"
import { globalContext } from "../globalContext"
const PostCardContainer = styled(Link)` const PostCardContainer = styled(Link)`
display: block; display: block;
@ -66,7 +68,7 @@ const StyledTitle = styled.h1`
const StyledMetaContainer = styled.small`` const StyledMetaContainer = styled.small``
interface PostCardData extends PostData { interface PostCardData extends PostData {
url: string content_id: string
} }
interface Props { interface Props {
@ -75,40 +77,43 @@ interface Props {
const PostCard = (props: Props) => { const PostCard = (props: Props) => {
const { postData } = props const { postData } = props
const { content_id, wordCount, date, readTime, title, tags } = postData
const { globalState } = useContext(globalContext)
return ( return (
<StyledPostCard> <StyledPostCard>
<PostCardContainer to={postData.url}> <PostCardContainer
to={`/${globalState.locale}${content_id.replace(/(.kr)$/g, "")}`}
>
<StyledTitle> <StyledTitle>
{postData.title || "No title"} {title || "No title"}
{/* show "(series)" for urls that matches regex "/series/<series-title>" */} {/* show "(series)" for urls that matches regex "/series/<series-title>" */}
{/\/series\/[^/]*$/.test(postData.url) && " (series)"} {/\/series\/[^/]*$/.test(content_id) && " (series)"}
</StyledTitle> </StyledTitle>
<br /> <br />
<StyledMetaContainer> <StyledMetaContainer>
<TagList direction="left"> <TagList direction="left">
{postData.tags && {tags &&
postData.tags.map((tag) => { tags.map((tag) => {
return <Tag key={postData.title + tag} text={tag} /> return <Tag key={title + tag} text={tag} />
})} })}
</TagList> </TagList>
<hr /> <hr />
<FontAwesomeIcon icon={faCalendar} /> <FontAwesomeIcon icon={faCalendar} />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
{postData.date || "Unknown date"} {date || "Unknown date"}
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
<FontAwesomeIcon icon={faHourglass} /> <FontAwesomeIcon icon={faHourglass} />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
{postData.readTime {readTime ? readTime + " read" : "unknown read time"}
? postData.readTime + " read"
: "unknown read time"}
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
<FontAwesomeIcon icon={faBook} /> <FontAwesomeIcon icon={faBook} />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
{typeof postData.wordCount === "number" {typeof wordCount === "number"
? postData.wordCount + " words" ? wordCount + " words"
: "unknown length"} : "unknown length"}
</StyledMetaContainer> </StyledMetaContainer>
</PostCardContainer> </PostCardContainer>

View file

@ -56,8 +56,7 @@ const Home = () => {
<PostCard <PostCard
key={content_id} key={content_id}
postData={{ postData={{
// /<locale>/<content id without locale suffix> content_id: content_id,
url: `/${locale}${content_id.replace(/(.kr)$/g, "")}`,
...map.posts[content_id], ...map.posts[content_id],
}} }}
/> />

View file

@ -203,7 +203,7 @@ export default function Page() {
<PostCard <PostCard
key={post} key={post}
postData={{ postData={{
url: post, content_id: post,
...map.posts[post], ...map.posts[post],
}} }}
/> />

View file

@ -1,3 +1,4 @@
import { useContext } from "react"
import styled, { css } from "styled-components" import styled, { css } from "styled-components"
import { Link } from "react-router-dom" import { Link } from "react-router-dom"
@ -10,6 +11,8 @@ import {
import theming from "../../styles/theming" import theming from "../../styles/theming"
import { globalContext } from "../../globalContext"
const Container = styled.div` const Container = styled.div`
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
@ -54,15 +57,20 @@ const DisabledButton = styled.div`
} }
` `
const SeriesControlButtons = (props: { interface Props {
seriesHome: string seriesHome: string
prevURL?: string prevURL?: string
nextURL?: string nextURL?: string
}) => { }
function SeriesControlButtons({ prevURL, seriesHome, nextURL }: Props) {
const { globalState } = useContext(globalContext)
const { locale } = globalState
return ( return (
<Container> <Container>
{props.prevURL ? ( {prevURL ? (
<Link to={props.prevURL}> <Link to={`/${locale}${prevURL}`}>
<Button> <Button>
<FontAwesomeIcon icon={faArrowLeft} /> <FontAwesomeIcon icon={faArrowLeft} />
</Button> </Button>
@ -73,14 +81,14 @@ const SeriesControlButtons = (props: {
</DisabledButton> </DisabledButton>
)} )}
<Link to={props.seriesHome}> <Link to={`/${locale}${seriesHome}`}>
<Button> <Button>
<FontAwesomeIcon icon={faListUl} /> <FontAwesomeIcon icon={faListUl} />
</Button> </Button>
</Link> </Link>
{props.nextURL ? ( {nextURL ? (
<Link to={props.nextURL}> <Link to={`/${locale}${nextURL}`}>
<Button> <Button>
<FontAwesomeIcon icon={faArrowRight} /> <FontAwesomeIcon icon={faArrowRight} />
</Button> </Button>

View file

@ -79,6 +79,7 @@ export function checkURLValidity(
: content_id + ".kr" // add .kr suffix : content_id + ".kr" // add .kr suffix
switch (pageType) { switch (pageType) {
case PageType.SERIES:
case PageType.POST: { case PageType.POST: {
if (map.posts[content_id]) return URLValidity.VALID if (map.posts[content_id]) return URLValidity.VALID
@ -88,20 +89,19 @@ export function checkURLValidity(
break break
} }
case PageType.SERIES_HOME: case PageType.SERIES_HOME: {
case PageType.SERIES: {
const series_keys = Object.keys(map.series) const series_keys = Object.keys(map.series)
if ( if (
series_keys.some((seriesHomeURL) => series_keys.some((seriesHomeURL) =>
content_id.startsWith(seriesHomeURL) seriesHomeURL.startsWith(content_id)
) )
) )
return URLValidity.VALID return URLValidity.VALID
if ( if (
series_keys.some((seriesHomeURL) => series_keys.some((seriesHomeURL) =>
alt_content_id.startsWith(seriesHomeURL) seriesHomeURL.startsWith(alt_content_id)
) )
) )
return URLValidity.VALID_BUT_IN_OTHER_LOCALE return URLValidity.VALID_BUT_IN_OTHER_LOCALE

View file

@ -201,7 +201,7 @@ const Search = () => {
<PostCard <PostCard
key={res.ref} key={res.ref}
postData={{ postData={{
url: res.ref, content_id: res.ref,
...postData, ...postData,
}} }}
/> />