Skip to content

Commit

Permalink
✨ Add getRandomHeadline function
Browse files Browse the repository at this point in the history
  • Loading branch information
fre-ben committed Feb 13, 2021
1 parent 62a63e1 commit 8ea9d67
Show file tree
Hide file tree
Showing 9 changed files with 74 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 { displayHeadline } 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 displayHeadline(headline);
};

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

export function displayHeadline({ headline }) {
return createElement("p", {
className: "headline",
innerText: headline,
});
}
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.

42 changes: 40 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,43 @@ 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;
};

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

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

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;
}

0 comments on commit 8ea9d67

Please sign in to comment.