Skip to content

Commit f21654b

Browse files
committed
Fetched posts data and created interfaces for the data
Created the interfaces to have more control over types while developing
1 parent cf19d4a commit f21654b

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

src/components/App.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
import React from "react";
1+
import { useEffect, useState } from "react";
2+
import { IPost } from "../utils/IPost";
23

34
const App = () => {
4-
return <div>Created initial develop branch</div>;
5+
const [posts, setPosts] = useState<IPost[]>([]);
6+
7+
useEffect(() => {
8+
fetch('api/posts')
9+
.then(response => response.json())
10+
.then(results => setPosts(results.posts))
11+
.catch(error => console.error(`Oops, could not retrieve data. Please, try again later. \nError: ${error}`));
12+
}, []);
13+
14+
return <div>{/* Complete the exercise here. */}</div>;
515
}
616

717
export default App;

src/utils/IAuthor.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IAuthor {
2+
name: string,
3+
avatar: string,
4+
}

src/utils/ICategory.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface ICategory {
2+
id: string,
3+
name: string,
4+
}

src/utils/IPost.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { IAuthor } from "./IAuthor";
2+
import { ICategory } from "./ICategory";
3+
4+
export interface IPost {
5+
id: string,
6+
title: string,
7+
publishDate: string,
8+
author: IAuthor,
9+
summary: string,
10+
categories: ICategory[],
11+
}

0 commit comments

Comments
 (0)