Skip to content

Commit cf3c78e

Browse files
committed
finished the sprint challenge with my man chris adams
1 parent 6b34f10 commit cf3c78e

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/components/card.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import axios from "axios";
2+
13
const Card = (article) => {
24
// TASK 5
35
// ---------------------
@@ -17,8 +19,36 @@ const Card = (article) => {
1719
// </div>
1820
// </div>
1921
//
22+
const card = document.createElement('div');
23+
const headline = document.createElement('div');
24+
const author = document.createElement('div');
25+
const imgContianer = document.createElement('div');
26+
const img = document.createElement('img');
27+
const byAuthor = document.createElement('span');
28+
29+
card.classList.add("card");
30+
headline.classList.add("headline");
31+
author.classList.add("author");
32+
imgContianer.classList.add("img-container");
33+
34+
headline.textContent = article.headline;
35+
img.src = article.authorPhoto;
36+
byAuthor.textContent = article.authorName;
37+
38+
imgContianer.append(img);
39+
author.append(imgContianer);
40+
author.append(byAuthor);
41+
card.append(headline);
42+
card.append(author);
43+
44+
card.addEventListener("click", () =>{
45+
console.log(article.headline);
46+
})
47+
return card;
48+
2049
}
2150

51+
2252
const cardAppender = (selector) => {
2353
// TASK 6
2454
// ---------------------
@@ -28,6 +58,16 @@ const cardAppender = (selector) => {
2858
// Create a card from each and every article object in the response, using the Card component.
2959
// Append each card to the element in the DOM that matches the selector passed to the function.
3060
//
61+
62+
axios.get(`http://localhost:5001/api/articles`)
63+
.then(res => {
64+
const testArticles = Object.values(res.data.articles)
65+
const flattenArticles = testArticles.flatMap(x => x);
66+
const cardContainer = document.querySelector(selector);
67+
flattenArticles.forEach(article => {
68+
cardContainer.append(Card(article));
69+
})
70+
})
3171
}
3272

3373
export { Card, cardAppender }

src/components/header.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ const Header = (title, date, temp) => {
1111
// <span class="temp">{ temp }</span>
1212
// </div>
1313
//
14+
const heading = document.createElement("div");
15+
const dateSpan = document.createElement("span");
16+
const h1Title = document.createElement("h1");
17+
const tempSpan = document.createElement("span");
18+
19+
heading.classList.add("header");
20+
dateSpan.classList.add("date");
21+
tempSpan.classList.add("temp");
22+
23+
heading.appendChild(dateSpan);
24+
heading.appendChild(h1Title);
25+
heading.appendChild(tempSpan);
26+
27+
dateSpan.textContent = date;
28+
h1Title.textContent = title;
29+
tempSpan.textContent = temp;
30+
31+
return heading;
1432
}
1533

1634
const headerAppender = (selector) => {
@@ -20,6 +38,8 @@ const headerAppender = (selector) => {
2038
// It should create a header using the Header component above, passing arguments of your choosing.
2139
// It should append the header to the element in the DOM that matches the given selector.
2240
//
41+
const headerApp = document.querySelector(selector);
42+
headerApp.appendChild(Header());
2343
}
2444

2545
export { Header, headerAppender }

src/components/tabs.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const Tabs = (topics) => {
1+
import axios from "axios";
2+
3+
const URL = 'http://localhost:5001/api/topics';
4+
5+
function Tabs(topics) {
26
// TASK 3
37
// ---------------------
48
// Implement this function which takes an array of strings ("topics") as its only argument.
@@ -13,6 +17,24 @@ const Tabs = (topics) => {
1317
// <div class="tab">technology</div>
1418
// </div>
1519
//
20+
// const topicsDiv = document.createElement("div");
21+
// topicsDiv.classList.add("topics");
22+
// for (let i = 0; i < topics.length; i++){
23+
// const div = document.createElement("div");
24+
// div.classList.add("tab");
25+
// iv.textContent = topics[i];
26+
// topicsDiv.appendChild(div);
27+
// }
28+
// return topicsDiv;
29+
const topicsDiv = document.createElement("div");
30+
topicsDiv.classList.add("topics");
31+
topics.forEach(topic => {
32+
const newDiv = document.createElement("div");
33+
newDiv.classList.add("tab");
34+
newDiv.textContent = topic;
35+
topicsDiv.append(newDiv);
36+
});
37+
return topicsDiv;
1638
}
1739

1840
const tabsAppender = (selector) => {
@@ -23,6 +45,11 @@ const tabsAppender = (selector) => {
2345
// Find the array of topics inside the response, and create the tabs using the Tabs component.
2446
// Append the tabs to the element in the DOM that matches the selector passed to the function.
2547
//
48+
axios.get(URL)
49+
.then(res => {
50+
const topics = document.querySelector(selector);
51+
topics.append(Tabs(res.data.topics));
52+
})
2653
}
2754

2855
export { Tabs, tabsAppender }

0 commit comments

Comments
 (0)