-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make news get data from tgno-backend api (#2)
* feat: use api to get news * feat: get news from api * chor: run pnpm update * fix: better layout on desktop * feat: add .env.example file * feat: add limiter to the newscomp
- Loading branch information
Showing
12 changed files
with
325 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# This file is used to set the environment variables for the development environment | ||
# Remember to update /src/env.d.ts with the same variables for IntelliSense for TypeScript | ||
|
||
# API_URL="http://localhost:8000/" | ||
API_URL="https://dev.tg.no/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# This file is used to set the environment variables for the development environment | ||
# Remember to update /src/env.d.ts with the same variables for IntelliSense for TypeScript | ||
|
||
API_URL= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<main class="px-24 max-w-7xl mx-auto w-full max-sm:px-5"> | ||
<main class="px-24 max-w-7xl mx-auto w-full sm:flex-1 max-sm:px-5"> | ||
<slot/> | ||
</main> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
/// <reference path="../.astro/types.d.ts" /> | ||
/// <reference path="../.astro/types.d.ts" /> | ||
|
||
interface ImportMetaEnv { | ||
readonly API_URL: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
import H1 from "../../components/H1.astro"; | ||
import Main from "../../components/Main.astro"; | ||
import Layout from "../../layouts/Layout.astro"; | ||
interface Article { | ||
id: number; | ||
meta: { | ||
type: string; | ||
detail_url: string; | ||
html_url: string; | ||
slug: string; | ||
first_published_at: string; | ||
}; | ||
title: string; | ||
intro: string; | ||
body: string; | ||
tags: Array<{ | ||
id: number; | ||
name: string; | ||
slug: string; | ||
}>; | ||
main_image: { | ||
url: string; | ||
full_url: string; | ||
width: number; | ||
height: number; | ||
alt: string; | ||
}; | ||
} | ||
const { slug } = Astro.params; | ||
const fetchArticleIdBySlug = async (slug: string): Promise<number | null> => { | ||
const response = await fetch( | ||
`${import.meta.env.API_URL}api/v2/news/?&slug=${slug}` | ||
); | ||
const data = await response.json(); | ||
const articles: Article[] = data.items; | ||
if (articles.length > 0) { | ||
return articles[0].id; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
if (!slug) { | ||
throw new Error('Slug is not defined'); | ||
} | ||
const articleId = await fetchArticleIdBySlug(slug); | ||
if (!articleId) { | ||
throw new Error('Article not found'); | ||
} | ||
const fetchArticleById = async (id: number): Promise<Article> => { | ||
const response = await fetch( | ||
`${import.meta.env.API_URL}api/v2/news/${id}/` | ||
); | ||
const data = await response.json(); | ||
return data; | ||
}; | ||
const article = await fetchArticleById(articleId); | ||
--- | ||
|
||
<Layout title=`The Gathering - ${article.title}`> | ||
<Main> | ||
<!-- Article Title --> | ||
<H1 text=`${article.title}`/> | ||
<!-- Display Tags --> | ||
<p class="text-sm text-gray-600 mb-2"> | ||
{article.tags | ||
.map((tag) => { | ||
return tag.name | ||
.split(' ') | ||
.map((word) => word.charAt(0).toUpperCase() + word.slice(1)) | ||
.join(' '); | ||
}) | ||
.join(', ')} | ||
</p> | ||
<!-- Publish Date --> | ||
<time datetime={article.meta.first_published_at} class="text-sm text-gray-500 mb-4 block"> | ||
{new Date(article.meta.first_published_at).toLocaleDateString()} | ||
</time> | ||
<!-- Main Image --> | ||
<img | ||
src={article.main_image.full_url} | ||
alt={article.main_image.alt} | ||
class="w-auto h-auto mb-4" | ||
/> | ||
<!-- Article Content --> | ||
<div class="text-white" set:html={article.body}></div> | ||
</Main> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
import Main from '../../components/Main.astro'; | ||
import NewsComp from '../../components/NewsComp.astro'; | ||
import Layout from '../../layouts/Layout.astro'; | ||
--- | ||
|
||
|
||
<Layout title="The Gathering - News"> | ||
<Main> | ||
<NewsComp /> | ||
</Main> | ||
</Layout> |