Skip to content

Commit 0af29a3

Browse files
committed
feat: add 15
1 parent 7d34296 commit 0af29a3

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

15-lorem-ipsum/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>15 - Lorem Ipsum</title>
7+
<link rel="stylesheet" href="styles.css">
8+
<script src="script.js" defer></script>
9+
</head>
10+
<body>
11+
<div id="loremIpsumGenerator">
12+
<label for="paragraphsCount">How many Paragraphs?</label>
13+
<input min="0" type="number" id="paragraphsCount">
14+
<button id="getLoremIpsum">Go</button>
15+
16+
<div id="result"></div>
17+
</div>
18+
</body>
19+
</html>

15-lorem-ipsum/script.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
const getLoremIpsum = numberOfParagraphs => {
3+
fetch(`https://baconipsum.com/api/?type=meat-and-filler&paras=${numberOfParagraphs}`)
4+
.then(response => response.json())
5+
.then(loremIpsumTextArray => {
6+
updateResult(loremIpsumTextArray);
7+
})
8+
.catch(error => {
9+
showError(error);
10+
});
11+
};
12+
13+
const updateResult = textArray => {
14+
const resultElement = document.getElementById('result');
15+
resultElement.innerHTML = '';
16+
resultElement.innerHTML = textArray
17+
.map(paragraph => `<p>${paragraph}</p>`)
18+
.join('');
19+
addCopyButton(textArray.join(''));
20+
};
21+
22+
const showError = error => {
23+
const resultElement = document.getElementById('result');
24+
resultElement.innerHTML = '';
25+
resultElement.innerHTML = `<p class="error">${error.message}</p>`
26+
}
27+
28+
const addCopyButton = text => {
29+
const resultElement = document.getElementById('result');
30+
const copyBtn = document.createElement('button');
31+
32+
copyBtn.textContent = 'Copy';
33+
copyBtn.classList.add('copy');
34+
copyBtn.onclick = () => {
35+
navigator.clipboard.writeText(text);
36+
copyBtn.textContent = 'Copied!';
37+
setTimeout(() => {
38+
copyBtn.textContent = 'Copy';
39+
}, 2000);
40+
}
41+
42+
resultElement.appendChild(copyBtn);
43+
}
44+
45+
const getLoremIpsumElement = document.getElementById('getLoremIpsum');
46+
const paragraphsCountElement = document.getElementById('paragraphsCount');
47+
48+
getLoremIpsumElement.addEventListener('click', () => {
49+
getLoremIpsum(parseInt(paragraphsCountElement.value));
50+
paragraphsCountElement.value = '';
51+
52+
})

15-lorem-ipsum/styles.css

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#loremIpsumGenerator {
2+
font-family: sans-serif;
3+
margin: 1rem;
4+
}
5+
6+
#paragraphsCount {
7+
width: 4rem;
8+
}
9+
10+
#result {
11+
margin-top: 1rem;
12+
padding: 1rem;
13+
border: 1px dashed gray;
14+
font-size: .75rem;
15+
position: relative;
16+
}
17+
18+
.copy {
19+
position: absolute;
20+
right: .25rem;
21+
top: .25rem;
22+
border: 0;
23+
text-transform: uppercase;
24+
font-size: .5rem;
25+
padding: .25rem .5rem;
26+
cursor: pointer;
27+
}
28+
29+
.copy:hover {
30+
background-color: blanchedalmond;
31+
}

0 commit comments

Comments
 (0)