Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script type="module" src="script.js"></script>
<title>Programmer humour</title>
</head>

<body>
<main>
<h1>Progammer humour image below</h1>
<img src="https://placeholderimage.co/600x400/222222/00ff00?text=Loading+Comic..." alt="Loading XKCD comic">
</main>
</body>

</html>
41 changes: 41 additions & 0 deletions fetch/programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const imageElement = document.querySelector("img");
const endpoint = `https://xkcd.now.sh/?comic=latest`;

const getImage = async () => {
try {
const response = await fetch(endpoint);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}

const data = await response.json();
return data;
} catch (error) {
console.error(error.message);
showLoadMessage("Sorry can't load image at present...");
}
};

const renderImage = async () => {
const imgData = await getImage();
imageElement.src = imgData?.img || "";
imageElement.alt = imgData?.alt || "Comic image";
};

renderImage();

const showLoadMessage = (message, duration = 3000) => {
const existingMessage = document.querySelector(".app-message");
if (existingMessage) existingMessage.remove();

const messageBox = document.createElement("div");
messageBox.className = "app-message";
messageBox.textContent = message;
document.body.appendChild(messageBox);

setTimeout(() => {
messageBox.remove();
}, duration);
};

window.addEventListener("DOMContentLoaded", getImage);
63 changes: 63 additions & 0 deletions fetch/programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* CSS Reset */

*,
*::before,
*::after {
box-sizing: border-box;
}

* {
margin: 0;
padding: 0;
}

html {
font-size: 100%;
line-height: 1.5;
}

img {
display: block;
width: 100%;
height: auto;
}

body {
min-height: 100vh;
display: grid;
place-items: center;
font-family: Arial, sans-serif;
background: #f4f4f4;
color: #222;
}

main {
width: 90%;
max-width: 600px;
}

h1 {
text-align: center;
font-size: 1em;
margin: 1rem 0;
}

/* shows loading message */
.app-message {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #b53a3a;
color: white;
padding: 0.8rem 1rem;
border-radius: 6px;
z-index: 1000;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

@media (min-width: 600px) {
h1 {
font-size: 1.8em;
}
}