generated from lmachens/storybook-html
-
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.
- Loading branch information
Showing
3 changed files
with
53 additions
and
5 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 |
---|---|---|
@@ -1,10 +1,20 @@ | ||
import "./cat.css"; | ||
import { helloCat } from "./randomcat"; | ||
import { displayCat } from "./randomcat"; | ||
import { createElement } from "../../utils/createElement"; | ||
import { getRandomCat } from "../../utils/api"; | ||
|
||
export default { | ||
title: "Components/Hello Cat", | ||
parameters: { layout: "centered" }, | ||
}; | ||
|
||
export const helloucat = () => helloCat; | ||
//Get Random Cat Gif from API | ||
export const CatFromAPI = (args, { loaded: { cat } }) => { | ||
return displayCat(cat); | ||
}; | ||
|
||
CatFromAPI.loaders = [ | ||
async () => ({ | ||
cat: await getRandomCat(), | ||
}), | ||
]; |
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,5 +1,9 @@ | ||
import { createElement } from "../../utils/createElement"; | ||
|
||
export const helloCat = createElement("p", { | ||
innerText: "Hello cats!", | ||
}); | ||
export function displayCat({ url }) { | ||
return createElement("img", { | ||
className: "catImg", | ||
src: url, | ||
alt: "cat", | ||
}); | ||
} |
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,34 @@ | ||
import { ClassificationType } from "typescript"; | ||
|
||
// Schema von CatAPI | ||
export type Cat = { | ||
breeds: string[]; | ||
categories: string[]; | ||
id: string; | ||
url: string; | ||
}; | ||
|
||
// Infos die ich brauch von der CatAPI | ||
export type RandomCat = { | ||
imgSrc: string; | ||
}; | ||
|
||
function convertToImage(randomCat: Cat): RandomCat { | ||
return { | ||
imgSrc: randomCat.url, | ||
}; | ||
} | ||
|
||
export async function getRandomCat() { | ||
const response = await fetch( | ||
`https://api.thecatapi.com/v1/images/search?mime_types=gif`, | ||
{ | ||
headers: { | ||
"x-api-key": "4ed34816-c19f-4144-9082-b2ffc3df0e40", | ||
}, | ||
} | ||
); | ||
const result = (await response.json()) as Cat; | ||
const test = convertToImage(result); | ||
return test; | ||
} |