|
1 | | -import React, { Fragment } from "react"; |
| 1 | +import React, { useEffect, useState } from "react"; |
2 | 2 | import dayjs from "dayjs"; |
3 | 3 | import { type IPublication, useApi } from "../api"; |
| 4 | +import { useParams } from "react-router-dom"; |
| 5 | +import { FullScreenLoading } from "../components/full-screen-loading"; |
4 | 6 |
|
5 | | -interface IProps { |
6 | | - match?: { |
7 | | - params: { |
8 | | - publication: string; |
9 | | - }; |
10 | | - }; |
| 7 | +interface Props { |
11 | 8 | data?: IPublication; |
12 | 9 | } |
13 | 10 |
|
14 | | -const PublicationPage: React.FC<IProps> = ({ |
15 | | - match, |
16 | | - data: publicationData, |
17 | | -}) => { |
18 | | - const [isLoading, setIsLoading] = React.useState(false); |
19 | | - const [data, setData] = React.useState<IPublication>(); |
20 | | - const [distanceToTop, setDistanceToTop] = React.useState(0); |
21 | | - const columnRef = React.useRef<HTMLDivElement>(); |
| 11 | +function PublicationPage(props: Props) { |
| 12 | + const { data: publicationData } = props; |
| 13 | + const { publication } = useParams(); |
| 14 | + const [isLoading, setIsLoading] = useState(false); |
| 15 | + const [data, setData] = useState<IPublication>(); |
22 | 16 | const { getPublication } = useApi(); |
23 | | - React.useEffect(() => { |
| 17 | + |
| 18 | + useEffect(() => { |
24 | 19 | getData(); |
25 | 20 | }, []); |
26 | | - React.useEffect(() => { |
27 | | - setDistanceToTop( |
28 | | - -(window.pageYOffset + columnRef.current?.getBoundingClientRect().top) + |
29 | | - 532 |
30 | | - ); |
31 | | - }, [data]); |
32 | 21 |
|
33 | 22 | const getData = async () => { |
34 | | - if (match) { |
| 23 | + if (publication) { |
35 | 24 | setIsLoading(true); |
36 | | - setData(await getPublication(match.params.publication)); |
| 25 | + setData(await getPublication(publication)); |
37 | 26 | setIsLoading(false); |
38 | 27 | } else { |
39 | 28 | setData(publicationData); |
40 | 29 | } |
41 | 30 | }; |
42 | 31 |
|
43 | | - return ( |
44 | | - <Fragment> |
45 | | - {data && ( |
46 | | - <> |
47 | | - <img |
48 | | - src={data.image} |
49 | | - alt={data.image_description} |
50 | | - style={{ |
51 | | - objectFit: "cover", |
52 | | - minHeight: "100%", |
53 | | - maxHeight: 689, |
54 | | - }} |
55 | | - /> |
56 | | - <div |
57 | | - ref={columnRef} |
58 | | - style={{ |
59 | | - alignItems: "center", |
60 | | - position: "relative", |
61 | | - right: 0, |
62 | | - left: 0, |
63 | | - top: distanceToTop, |
64 | | - marginBottom: distanceToTop + 80, |
65 | | - }} |
66 | | - > |
67 | | - <div |
68 | | - style={{ |
69 | | - position: "absolute", |
70 | | - top: -50, |
71 | | - right: 0, |
72 | | - left: 0, |
73 | | - display: "flex", |
74 | | - }} |
75 | | - > |
76 | | - <div |
77 | | - style={{ |
78 | | - width: "100%", |
79 | | - margin: "auto", |
80 | | - }} |
81 | | - ></div> |
82 | | - </div> |
83 | | - |
84 | | - {dayjs(data.created_at).locale("pt-br").format("LLLL")} |
| 32 | + if (isLoading) { |
| 33 | + return <FullScreenLoading />; |
| 34 | + } |
85 | 35 |
|
86 | | - <div |
87 | | - style={{ |
88 | | - maxWidth: 509, |
89 | | - paddingBottom: 25, |
90 | | - }} |
91 | | - > |
92 | | - {data.title} |
93 | | - </div> |
94 | | - <div |
95 | | - style={{ |
96 | | - whiteSpace: "pre-line", |
97 | | - }} |
98 | | - dangerouslySetInnerHTML={{ __html: data.body }} |
99 | | - /> |
100 | | - </div> |
101 | | - </> |
102 | | - )} |
103 | | - </Fragment> |
| 36 | + return ( |
| 37 | + <main className="pt-8 pb-16 lg:pt-16 lg:pb-24 antialiased"> |
| 38 | + <div className="flex justify-between px-4 mx-auto max-w-screen-xl "> |
| 39 | + <article className="mx-auto w-full max-w-2xl format format-sm sm:format-base lg:format-lg format-blue dark:format-invert"> |
| 40 | + <header className="mb-4 lg:mb-6 not-format"> |
| 41 | + <address className="flex items-center mb-6 not-italic"> |
| 42 | + <div className="inline-flex items-center mr-3 text-sm text-gray-900 dark:text-white"> |
| 43 | + <div> |
| 44 | + <p className="text-base text-gray-500 dark:text-gray-400"> |
| 45 | + <time> |
| 46 | + {dayjs(data?.created_at).locale("pt-br").format("LLLL")} |
| 47 | + </time> |
| 48 | + </p> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | + </address> |
| 52 | + <h1 className="mb-4 text-3xl font-extrabold leading-tight text-gray-900 lg:mb-6 lg:text-4xl dark:text-white"> |
| 53 | + {data?.title} |
| 54 | + </h1> |
| 55 | + </header> |
| 56 | + <p className="lead">{data?.description}</p> |
| 57 | + {data?.body} |
| 58 | + <figure className="mt-4"> |
| 59 | + <img src={data?.image} alt={data?.image_description} /> |
| 60 | + <figcaption>{data?.image_description}</figcaption> |
| 61 | + </figure> |
| 62 | + </article> |
| 63 | + </div> |
| 64 | + </main> |
104 | 65 | ); |
105 | | -}; |
| 66 | +} |
106 | 67 |
|
107 | 68 | export { PublicationPage as default }; |
0 commit comments