Skip to content

Commit

Permalink
Merge pull request #2 from fre-ben/getRandomHeadline
Browse files Browse the repository at this point in the history
✨ Add getRandomHeadline function
  • Loading branch information
fre-ben authored Feb 13, 2021
2 parents 62a63e1 + a6b81b8 commit 288a88f
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 21 deletions.
6 changes: 6 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ const test = convertToImage(result);
console.log(result);
return test;
}

## Currents Api Token

dRj7MBwlYafKn4RJFGHIM--anhE1w_bpXLYKo7hdZIKJW0eX

https://api.currentsapi.services/v1/latest-news?language=de&apiKey=dRj7MBwlYafKn4RJFGHIM--anhE1w_bpXLYKo7hdZIKJW0eX
2 changes: 1 addition & 1 deletion src/components/cat/cat.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createElement } from "../../utils/createElement";
import { getRandomCat } from "../../utils/api";

export default {
title: "Components/Hello Cat",
title: "Components/Cat",
parameters: { layout: "centered" },
};

Expand Down
Empty file.
19 changes: 19 additions & 0 deletions src/components/headline/headline.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import "./headline.css";
import { displayGermanHeadline } from "./headline";
import { createElement } from "../../utils/createElement";
import { getRandomHeadlineGerman } from "../../utils/api";

export default {
title: "Components/Headline",
parameters: { layout: "centered" },
};

export const HeadlineFromAPI = (args, { loaded: { headline } }) => {
return displayGermanHeadline(headline);
};

HeadlineFromAPI.loaders = [
async () => ({
headline: await getRandomHeadlineGerman(),
}),
];
17 changes: 17 additions & 0 deletions src/components/headline/headline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createElement } from "../../utils/createElement";

export function displayGermanHeadline({ headline, link }) {
return createElement("div", {
className: "headline__container",
childs: [
createElement("p", {
className: "headline",
innerText: headline,
}),
createElement("a", {
href: link,
innerText: "Artikel lesen",
}),
],
});
}
3 changes: 0 additions & 3 deletions src/components/typography/typography.css

This file was deleted.

6 changes: 0 additions & 6 deletions src/components/typography/typography.html

This file was deleted.

9 changes: 0 additions & 9 deletions src/components/typography/typography.stories.js

This file was deleted.

44 changes: 42 additions & 2 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ClassificationType } from "typescript";

//TheCatAPI
// Schema von CatAPI
export type Cat = {
Expand Down Expand Up @@ -36,3 +34,45 @@ export async function getRandomCat() {
}

// News API
// Schema currentsAPI
export type News = {
status: string;
news: [
{
id: string;
title: string;
description: string;
url: string;
author: string;
image: string;
language: string;
category: string[];
published: string;
}
];
page: number;
};

// Was ich brauch von currentsAPI
export type Headline = {
headline: string;
link: string;
};

const randomHeadline = Math.floor(Math.random() * 30);

function convertToText(headline: News): Headline {
return {
headline: headline.news[randomHeadline].title,
link: headline.news[randomHeadline].url,
};
}

export async function getRandomHeadlineGerman() {
const response = await fetch(
`https://api.currentsapi.services/v1/latest-news?language=de&apiKey=dRj7MBwlYafKn4RJFGHIM--anhE1w_bpXLYKo7hdZIKJW0eX`
);
const result = (await response.json()) as News;
const headline = convertToText(result);
return headline;
}

1 comment on commit 288a88f

@vercel
Copy link

@vercel vercel bot commented on 288a88f Feb 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.