summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/pages/articles/Article.tsx
blob: 1a96ada8bf2a5585d22de744eca9570e42bda3d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { useState, useEffect } from "react";
import { useParams } from "react-router-dom";

type article = {
  id: number;
  title: string;
  photoUrl: string;
  content: string;
  createdOn: string;
  updateOn: string;
};

export default function Article() {
  const { id } = useParams();
  const [articleData, setArticleData] = useState<article>();

  useEffect(() => {
    const url = `${import.meta.env.VITE_API_TITLE}/api/v1/article/${id}`;
    fetch(url)
      .then((response) => {
        if (response.ok) {
          return response.json();
        }
        throw new Error("Network response was not ok.");
      })
      .then((response) => setArticleData(response)); //.catch(() => navigate("/"));
  }, [id]);

  return (
    <>
      <h1>{articleData?.title}</h1>
      <div>{articleData?.content}</div>
    </>
  );
}