diff --git a/backend/src/main/kotlin/org/kotlin/everywhere/realworld/api.kt b/backend/src/main/kotlin/org/kotlin/everywhere/realworld/api.kt index fea9983df6..0cd5e06c98 100644 --- a/backend/src/main/kotlin/org/kotlin/everywhere/realworld/api.kt +++ b/backend/src/main/kotlin/org/kotlin/everywhere/realworld/api.kt @@ -20,6 +20,7 @@ class Api : Kenet() { val articleCreate by c() val articleEditShow by c() val articleEdit by c() + val articleShow by c() // index val feedList by c() @@ -135,6 +136,24 @@ class ArticleEditReq( @Serializable class ArticleEditRes(override val errors: List = listOf(), val slug: String? = null) : ErrorsRes +@Serializable +class ArticleShowReq(val slug: String) + +@Serializable +class ArticleShowRes(val data: Data? = null) { + @Serializable + class Data( + val userPk: Int?, + val userName: String?, + val userProfilePictureUrl: String?, + val pk: Int, + val title: String, + val description: String, + val article: String, + val tags: List, + val lastUpdatedAt: String, + ) +} @Serializable class FeedListReq(val accessToken: String?) @@ -274,6 +293,22 @@ fun Api.init() { ArticleEditRes(slug = article.slug) } + articleShow { req -> + val article = articles.firstOrNull { it.slug == req.slug } ?: return@articleShow ArticleShowRes() + val articleUser = users.firstOrNull { it.pk == article.userPk } + ArticleShowRes(data = ArticleShowRes.Data( + userPk = articleUser?.pk, + userName = articleUser?.name, + userProfilePictureUrl = articleUser?.profilePictureUrl, + pk = article.pk, + title = article.title, + description = article.description, + article = article.article, + tags = article.tags, + lastUpdatedAt = (article.updatedAt ?: article.createdAt).toString() + )) + } + feedList { req -> val user = users.firstOrNull { it.accessTokens.contains(req.accessToken) } val feeds = articles diff --git a/frontend/src/page/artcle.tsx b/frontend/src/page/artcle.tsx index 04ddd6e00d..1ded702825 100644 --- a/frontend/src/page/artcle.tsx +++ b/frontend/src/page/artcle.tsx @@ -1,11 +1,47 @@ -import { ReactElement } from "react"; +import { useHistory, useParams } from "react-router-dom"; +import { useEffect, useState } from "react"; +import { api } from "../api"; + +export const ArticlePage = () => { + const history = useHistory(); + const { slug } = useParams<{ slug?: string }>(); + const [article, setArticle] = useState<{ + article: string; + description: string; + lastUpdatedAt: string; + pk: number; + tags: string[]; + title: string; + userName: string | null; + userPk: number | null; + userProfilePictureUrl: string | null; + } | null>(null); + + useEffect(() => { + const fallback = () => history.replace({ pathname: "/" }); + if (!slug?.length) { + fallback(); + return; + } + api.articleShow({ slug: slug }).then((res) => { + if (!res.data) { + alert("Cannot find an article"); + fallback(); + return; + } + setArticle(res.data); + }); + }, [history, slug]); + + if (article == null) { + return <>; + } -export function ArticlePage(): ReactElement { return (
-

How to build webapps that scale

+

{article.title}

@@ -13,17 +49,17 @@ export function ArticlePage(): ReactElement {
- Eric Simons + {article.userName} - January 20th + {article.lastUpdatedAt}
  
@@ -33,12 +69,8 @@ export function ArticlePage(): ReactElement {
-

- Web development technologies have evolved at an incredible clip - over the past few years. -

-

Introducing RealWorld.

-

It's a great solution for learning how other frameworks work.

+

{article.description}

+ {article.article}
@@ -138,4 +170,4 @@ export function ArticlePage(): ReactElement {
); -} +};