Skip to content

Commit 08327df

Browse files
Merge pull request #1 from josh-stillman/js/add-news-feed
Js/add news feed
2 parents ba47847 + 44c6543 commit 08327df

File tree

9 files changed

+100
-96
lines changed

9 files changed

+100
-96
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NEXT_PUBLIC_API_URL=http://localhost:1337

.github/workflows/main.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ on:
88
# Allows you to run this workflow manually from the Actions tab
99
workflow_dispatch:
1010

11-
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
12-
permissions:
13-
contents: read
14-
pages: write
15-
id-token: write
16-
1711
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
1812
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
1913
concurrency:
@@ -24,6 +18,8 @@ jobs:
2418
# Build job
2519
build-and-deploy:
2620
runs-on: ubuntu-latest
21+
env:
22+
NEXT_PUBLIC_API_URL: ${{ secrets.API_URL }}
2723
steps:
2824
- name: Checkout
2925
uses: actions/checkout@v3

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"next": "13.4.15",
1919
"react": "18.2.0",
2020
"react-dom": "18.2.0",
21+
"swr": "2.2.2",
2122
"typescript": "5.1.6"
2223
},
2324
"devDependencies": {

src/app/page.module.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.main {
22
display: flex;
33
flex-direction: column;
4-
justify-content: space-between;
4+
justify-content: space-around;
55
align-items: center;
66
padding: 6rem;
77
min-height: 100vh;

src/app/page.tsx

Lines changed: 3 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,17 @@
1-
import Image from 'next/image';
1+
import { NewsFeed } from '../components/NewsFeed';
22
import styles from './page.module.css';
33
import Link from 'next/link';
44

55
export default function Home() {
66
return (
77
<main className={styles.main}>
88
<div className={styles.description}>
9-
<h1>
10-
Hello World we are&nbsp;
11-
<code className={styles.code}>deployed!!</code>
12-
</h1>
13-
14-
<h1>deployed through GITHUB ACTIONS!</h1>
15-
<strong>AGAIN?!</strong>
9+
<h1>Hello World we are deployed!</h1>
1610

1711
<Link href="/foobar">go to foobar page!</Link>
18-
<div>
19-
<a
20-
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
21-
target="_blank"
22-
rel="noopener noreferrer"
23-
>
24-
By{' '}
25-
<Image
26-
src="/vercel.svg"
27-
alt="Vercel Logo"
28-
className={styles.vercelLogo}
29-
width={100}
30-
height={24}
31-
priority
32-
/>
33-
</a>
34-
</div>
35-
</div>
36-
37-
<div className={styles.center}>
38-
<Image
39-
className={styles.logo}
40-
src="/next.svg"
41-
alt="Next.js Logo"
42-
width={180}
43-
height={37}
44-
priority
45-
/>
4612
</div>
4713

48-
<h1>hello world</h1>
49-
50-
<div className={styles.grid}>
51-
<a
52-
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
53-
className={styles.card}
54-
target="_blank"
55-
rel="noopener noreferrer"
56-
>
57-
<h2>
58-
Docs <span>-&gt;</span>
59-
</h2>
60-
<p>Find in-depth information about Next.js features and API.</p>
61-
</a>
62-
63-
<a
64-
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
65-
className={styles.card}
66-
target="_blank"
67-
rel="noopener noreferrer"
68-
>
69-
<h2>
70-
Learn <span>-&gt;</span>
71-
</h2>
72-
<p>Learn about Next.js in an interactive course with&nbsp;quizzes!</p>
73-
</a>
74-
75-
<a
76-
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
77-
className={styles.card}
78-
target="_blank"
79-
rel="noopener noreferrer"
80-
>
81-
<h2>
82-
Templates <span>-&gt;</span>
83-
</h2>
84-
<p>Explore the Next.js 13 playground.</p>
85-
</a>
86-
87-
<a
88-
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
89-
className={styles.card}
90-
target="_blank"
91-
rel="noopener noreferrer"
92-
>
93-
<h2>
94-
Deploy <span>-&gt;</span>
95-
</h2>
96-
<p>
97-
Instantly deploy your Next.js site to a shareable URL with Vercel.
98-
</p>
99-
</a>
100-
</div>
14+
<NewsFeed />
10115
</main>
10216
);
10317
}

src/components/NewsFeed.module.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.newsItemList {
2+
display: flex;
3+
flex-direction: column;
4+
justify-content: space-around;
5+
}
6+
7+
.newsItem {
8+
margin: 20px 0;
9+
}

src/components/NewsFeed.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use client';
2+
import useSWR from 'swr';
3+
import { NewsItemsResponse } from '../types/api';
4+
import styles from './NewsFeed.module.css';
5+
6+
const fetcher = (url: string) => fetch(url).then(res => res.json());
7+
8+
const params = new URLSearchParams();
9+
10+
params.set('sort', 'publishedAt:desc');
11+
12+
export function NewsFeed() {
13+
const { data, error, isLoading } = useSWR<NewsItemsResponse>(
14+
`${process.env.NEXT_PUBLIC_API_URL}/api/news-items?${params.toString()}`,
15+
fetcher
16+
);
17+
18+
if (isLoading) return <div>loading...</div>;
19+
20+
if (error || !data?.data) return <div>failed to load</div>;
21+
22+
return (
23+
<div className={styles.newsItemList}>
24+
<h1>NewsFeed! 🗞️</h1>
25+
26+
{data.data.map(({ attributes, id }) => (
27+
<div key={id} className={styles.newsItem}>
28+
<h2>{attributes.Title}</h2>
29+
30+
<h3>
31+
<i>{attributes.Body}</i>
32+
</h3>
33+
34+
<span>{new Date(attributes.publishedAt).toLocaleDateString()}</span>
35+
</div>
36+
))}
37+
</div>
38+
);
39+
}

src/types/api.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export interface NewsItem {
2+
id: number;
3+
attributes: {
4+
Title: string;
5+
Body: string;
6+
publishedAt: string;
7+
};
8+
}
9+
10+
export interface StrapiResponse {
11+
meta: {
12+
pagination: {
13+
page: number;
14+
pageSize: number;
15+
pageCount: number;
16+
total: number;
17+
};
18+
};
19+
}
20+
21+
export interface NewsItemsResponse extends StrapiResponse {
22+
data: NewsItem[];
23+
}

0 commit comments

Comments
 (0)