From 2c46000979b2c9906c5066821c43ddf0ae7ff090 Mon Sep 17 00:00:00 2001 From: Daniel Andreas Wang Date: Thu, 12 Mar 2026 19:01:51 +0100 Subject: set up news fetching --- src/stores/news.ts | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'src/stores/news.ts') 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([]) + const loading = ref(false) + const error = ref(null) + + async function fetchTopHeadlines(params: Record = {}) { + 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 = {}) { + 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 } }) -- cgit