feat: train, news and weather
This commit is contained in:
25
src/hooks/useNewsApi.tsx
Normal file
25
src/hooks/useNewsApi.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import { useEffect, type Dispatch } from "react"
|
||||
import { actions, type Action, type State } from "../state"
|
||||
import type {Article} from "../types/article"
|
||||
|
||||
const newsUrl = 'https://newsdata.io/api/1/latest?apikey=pub_26997f21bb174c7cbab59b3651533429&q=nivelle &country=be'
|
||||
|
||||
export type UseNewsApiProps = {
|
||||
dispatch: Dispatch<Action>,
|
||||
state: State
|
||||
}
|
||||
|
||||
export const useNewsApi = ({dispatch}: UseNewsApiProps) => {
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
try {
|
||||
dispatch(actions.loadNews({}));
|
||||
const answer = await fetch(newsUrl);
|
||||
const { results: news } = await answer.json() as { results: Article[] }
|
||||
dispatch(actions.loadNewsSuccess({ news }));
|
||||
} catch(error) {
|
||||
dispatch(actions.loadNewsError({ error: error as Error}));
|
||||
}
|
||||
})()
|
||||
}, [])
|
||||
}
|
||||
Reference in New Issue
Block a user