diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/router/index.ts | 2 | ||||
| -rw-r--r-- | src/stores/news.ts | 46 |
2 files changed, 46 insertions, 2 deletions
diff --git a/src/router/index.ts b/src/router/index.ts index 0f02263..b1ec72f 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -2,7 +2,7 @@ import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' const router = createRouter({ - history: createWebHistory() + history: createWebHistory(), routes: [ { path: '/', diff --git a/src/stores/news.ts b/src/stores/news.ts index 31866ba..c84b005 100644 --- a/src/stores/news.ts +++ b/src/stores/news.ts @@ -1,5 +1,49 @@ -import { ref, computed } from 'vue' +import { ref } from 'vue' import { defineStore } from 'pinia' +export interface Article { + source: { id: string | null; name: string } + author: string | null + title: string + description: string | null + url: string + urlToImage: string | null + publishedAt: string + content: string | null +} + export const useNewsStore = defineStore('news', () => { + const articles = ref<Article[]>([]) + const loading = ref(false) + const error = ref<string | null>(null) + + async function fetchTopHeadlines(params: Record<string, string> = {}) { + loading.value = true + error.value = null + const query = new URLSearchParams(params) + const response = await fetch(`/api/top-headlines?${query}`) + const data = await response.json() + if (!response.ok) { + error.value = data.message ?? 'Failed to fetch top headlines' + } else { + articles.value = data.articles + } + loading.value = false + } + + async function fetchEverything(params: Record<string, string> = {}) { + loading.value = true + error.value = null + const query = new URLSearchParams(params) + const response = await fetch(`/api/everything?${query}`) + const data = await response.json() + if (!response.ok) { + error.value = data.message ?? 'Failed to fetch articles' + } else { + articles.value = data.articles + } + loading.value = false + } + + return { articles, loading, error, fetchTopHeadlines, fetchEverything } }) |
