Skip to content

Commit

Permalink
🐛 Add working refresh button
Browse files Browse the repository at this point in the history
  • Loading branch information
fre-ben committed Feb 15, 2021
1 parent 6e7bb52 commit 3cebfc7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/pages/catlinez/catlinez.stories.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import "./catlinez.css";
import { displayHeader } from "../../components/header/header";
import { createElement } from "../../utils/createElement";
import { getRandomCat, getRandomHeadlineGerman } from "../../utils/api";
import {
getRandomCat,
getRandomHeadlineGerman,
randomNumber,
} from "../../utils/api";
import { displayCat } from "../../components/cat/randomcat";
import { displayGermanHeadline } from "../../components/headline/headline";

Expand All @@ -11,25 +15,25 @@ export default {
};

export const catlinez = (args, { loaded: { cat, headline } }) => {
const test = displayCat(cat);
const catElement = displayCat(cat);
const headlineElement = displayGermanHeadline(headline);

const randomButton = createElement("button", {
className: "randomButton",
innerText: "Refresh",
onclick: async () => {
location.reload();
// buildCatlinez.replaceChild(test, buildCatlinez.childNodes[1]);
const cat = await getRandomCat();
const headline = await getRandomHeadlineGerman();
const catElement = displayCat(cat);
const headlineElement = displayGermanHeadline(headline);
buildCatlinez.replaceChild(catElement, buildCatlinez.childNodes[1]);
buildCatlinez.replaceChild(headlineElement, buildCatlinez.childNodes[2]);
},
});

const buildCatlinez = createElement("div", {
className: "catlinez__container",
childs: [
displayHeader(),
test,
displayGermanHeadline(headline),
randomButton,
],
childs: [displayHeader(), catElement, headlineElement, randomButton],
});

return buildCatlinez;
Expand Down
6 changes: 5 additions & 1 deletion src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ export type Headline = {
link: string;
};

const randomHeadline = Math.floor(Math.random() * 30);
export function randomNumber() {
const num = Math.floor(Math.random() * 30);
return num;
}

function convertToText(headline: News): Headline {
const randomHeadline = randomNumber();
return {
headline: headline.news[randomHeadline].title,
link: headline.news[randomHeadline].url,
Expand Down

0 comments on commit 3cebfc7

Please sign in to comment.