Skip to content

Commit

Permalink
🚧 WIP getRandomCat gif, not working
Browse files Browse the repository at this point in the history
  • Loading branch information
fre-ben committed Feb 13, 2021
1 parent ccdc7b6 commit 72d5bbb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/components/cat/cat.stories.ts
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(),
}),
];
10 changes: 7 additions & 3 deletions src/components/cat/randomcat.ts
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",
});
}
34 changes: 34 additions & 0 deletions src/utils/api.ts
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;
}

0 comments on commit 72d5bbb

Please sign in to comment.