|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { useParams } from 'react-router'; |
| 3 | +import styled from 'styled-components'; |
| 4 | +import usePosts from "hooks/blogs" |
| 5 | +import { Posts } from 'blogs' |
| 6 | + |
| 7 | +const Container = styled.div` |
| 8 | + display: flex; |
| 9 | + flex-direction: column; |
| 10 | + justify-content: center; |
| 11 | + align-items: center; |
| 12 | + margin: 70px; |
| 13 | +`; |
| 14 | + |
| 15 | +const Title = styled.p` |
| 16 | + font-family: "IBM Plex Sans"; |
| 17 | + font-style: italic; |
| 18 | + font-size: 36pt; |
| 19 | + margin: 12pt 0; |
| 20 | + color: #232636; |
| 21 | + margin: 0 0 30px 0; |
| 22 | +`; |
| 23 | + |
| 24 | +const Text = styled.p` |
| 25 | + color: black |
| 26 | + font-family: "IBM Plex Sans"; |
| 27 | + font-size: 18px; |
| 28 | + margin: 0 0 20px 0; |
| 29 | + padding: 0; |
| 30 | +`; |
| 31 | + |
| 32 | +const Content = styled.p` |
| 33 | + margin: 0 15vw 0 15vw; |
| 34 | +`; |
| 35 | + |
| 36 | +const SubInfo = styled.p` |
| 37 | + display: flex; |
| 38 | + flex-direction: column; |
| 39 | + justify-content: start; |
| 40 | +`; |
| 41 | + |
| 42 | +const BlogImage = styled.img` |
| 43 | + height: 20%; |
| 44 | + margin: 0 0 20px 0; |
| 45 | +`; |
| 46 | + |
| 47 | +const BodyText = styled.pre` |
| 48 | + font-family: Consolas, monospace; |
| 49 | + margin: 0 0 20px 0; |
| 50 | + padding: 0; |
| 51 | + width: 70vw; |
| 52 | + white-space: pre-wrap; |
| 53 | +`; |
| 54 | + |
| 55 | +interface RouteParams { |
| 56 | + id: string; |
| 57 | +} |
| 58 | + |
| 59 | +const BlogPage: React.FC = () => { |
| 60 | + const [blogFound, setBlogFound] = useState<Posts>( |
| 61 | + { author: "", category: "", closed: "", date: "", id: -1, |
| 62 | + image: "", content: "", summary: "", title: "", visibility: ""}) |
| 63 | + |
| 64 | + const stringId: RouteParams = useParams(); |
| 65 | + const blogId: number = parseInt(stringId.id, 10); |
| 66 | + const {posts} = usePosts(); |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + // filter to get the blog with specific id |
| 70 | + console.log(posts) |
| 71 | + posts.every(blog => { |
| 72 | + console.log(blog.id) |
| 73 | + if (blog.id === blogId){ |
| 74 | + setBlogFound(blog); |
| 75 | + return false; |
| 76 | + } |
| 77 | + return true; |
| 78 | + }) |
| 79 | + }, [posts, blogId]) |
| 80 | + |
| 81 | + return ( |
| 82 | + <Container> |
| 83 | + {blogFound.id === -1 || blogFound.visibility === "Hidden" ? <Text>Sorry, the blog you're looking for doesnt exist</Text> |
| 84 | + : |
| 85 | + <> |
| 86 | + <Title>{blogFound.title}</Title> |
| 87 | + <BlogImage src={blogFound.image} alt={blogFound.title} /> |
| 88 | + <SubInfo> |
| 89 | + <Text><b>Author:</b> {blogFound.author}</Text> |
| 90 | + <Text><b>Published Date:</b> {blogFound.date}</Text> |
| 91 | + </SubInfo> |
| 92 | + <Content> |
| 93 | + <BodyText> |
| 94 | + <Text>{blogFound.content}</Text> |
| 95 | + </BodyText> |
| 96 | + </Content> |
| 97 | + </> |
| 98 | + } |
| 99 | + </Container> |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | +export default BlogPage; |
0 commit comments