1+ import axios from "axios" ;
2+
13const 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+
2252const 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
3373export { Card , cardAppender }
0 commit comments