Skip to content

Commit fdf0db8

Browse files
authored
Merge pull request #2 from akhil-neoito/pokeapi-integration
feat: change api to newsdata.io
2 parents 3e87fcf + 19850df commit fdf0db8

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

src/lib/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const newsApiKey = 'f86040b77923411aa0e8a211627437ce';
1+
export const newsApiKey = 'pub_97975dc870625c92568f10bdbf1cb8a0f9a9';

src/pages/Articles/Articles.tsx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { formatDate } from '@/lib/helper';
44
import { useArticlesQuery } from '@/services/queries/article.query';
55
import type { Article } from '@/types/article';
66

7-
export type Filter = { page: number; pageSize: number; search?: string };
7+
export type Filter = { page: number; search?: string };
88

99
export interface ArticleListProps {
1010
articles: Article[];
@@ -19,31 +19,48 @@ const ArticleList: React.FC<ArticleListProps> = ({ articles }) => {
1919
<>
2020
{articles.map((article) => (
2121
<div
22-
key={article.url}
22+
key={article.link}
2323
className="my-1 px-1 w-full md:w-1/2 lg:my-4 lg:px-4 lg:w-1/3"
2424
>
2525
<article className="overflow-hidden rounded-lg shadow-lg">
26-
<a href={article.url} target="_blank" rel="noreferrer">
27-
<img
28-
alt="Placeholder"
29-
className="block h-auto w-full"
30-
src={article.urlToImage}
31-
/>
26+
<a href={article.link} target="_blank" rel="noreferrer">
27+
{article.image_url ? (
28+
<img
29+
alt="Placeholder"
30+
className="block h-auto w-full"
31+
src={article.image_url}
32+
/>
33+
) : (
34+
<svg
35+
xmlns="http://www.w3.org/2000/svg"
36+
className="h-[215px] w-[370px] bg-gray-100"
37+
fill="none"
38+
viewBox="0 0 24 24"
39+
stroke="currentColor"
40+
strokeWidth={2}
41+
>
42+
<path
43+
strokeLinecap="round"
44+
strokeLinejoin="round"
45+
d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
46+
/>
47+
</svg>
48+
)}
3249
</a>
3350

3451
<header className="flex items-center justify-between leading-tight p-2 md:p-4">
3552
<h1 className="text-lg">
3653
<a
3754
className="no-underline hover:underline text-black"
38-
href={article.url}
55+
href={article.link}
3956
target="_blank"
4057
rel="noreferrer"
4158
>
4259
{article.title}
4360
</a>
4461
</h1>
4562
<p className="text-grey-darker text-sm">
46-
{formatDate(article.publishedAt)}
63+
{formatDate(article.pubDate)}
4764
</p>
4865
</header>
4966

@@ -107,7 +124,7 @@ const Articles = () => {
107124
{isLoading ? (
108125
<div>Loading...</div>
109126
) : (
110-
<ArticleList articles={data?.articles ?? []} />
127+
<ArticleList articles={data?.results ?? []} />
111128
)}
112129
</div>
113130
</div>

src/services/api/article.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import type { GetArticlesProps, GetArticleResponse } from '@/types/article';
55
export const getArticles = async (
66
params: GetArticlesProps
77
): Promise<GetArticleResponse> => {
8-
const { search, page, pageSize } = params;
8+
const { search, page } = params;
99
const { data } = await axios.get<GetArticleResponse>(
10-
`https://newsapi.org/v2/everything?q=${search}&page=${page}&pageSize=${pageSize}&apiKey=${newsApiKey}`
10+
`https://newsdata.io/api/1/news?q=${search}&page=${page}&apiKey=${newsApiKey}`
1111
);
1212
return data;
1313
};

src/types/article.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
export interface GetArticleResponse {
22
status: string;
33
totalResults: number;
4-
articles: Article[];
4+
results: Article[];
5+
nextPage?: number;
56
}
67

78
export interface GetArticlesProps {
89
search?: string;
910
page: number;
10-
pageSize: number;
1111
}
1212

1313
export interface Article {
14-
source: Source;
15-
author: string;
1614
title: string;
15+
link: string;
16+
keywords: string[];
17+
creator: string[];
18+
video_url?: string;
1719
description: string;
18-
url: string;
19-
urlToImage: string;
20-
publishedAt: string;
2120
content: string;
22-
}
23-
24-
export interface Source {
25-
id?: null;
26-
name: string;
21+
pubDate: string;
22+
image_url?: string;
23+
source_id: string;
24+
country: string[];
25+
category: string[];
2726
}

0 commit comments

Comments
 (0)