소스 검색

Add ability to search currencies by location.

Lee Morgan 4 년 전
부모
커밋
232965ad8a
2개의 변경된 파일74개의 추가작업 그리고 10개의 파일을 삭제
  1. 17 0
      views/currency/currency.css
  2. 57 10
      views/currency/display.html

+ 17 - 0
views/currency/currency.css

@@ -15,4 +15,21 @@ img{
 
 .comment{
     padding-left: 25px;
+}
+
+#locations{
+    display: flex;
+    margin: 15px;
+}
+
+.locationBtn{
+    margin: 10px;
+    padding: 5px;
+    background: none;
+    border: 1px solid black;
+    cursor: pointer;
+}
+
+.selected{
+    background: green;
 }

+ 57 - 10
views/currency/display.html

@@ -6,24 +6,53 @@
         <title>Lee Morgan</title>
     </head>
     <body>
-        <div id="container">
-            <template id="currency">
-                <div class="currency">
-                    <h1 class="location"></h1>
-                    <p class="info"></p>
-                    <img class="frontImage">
-                    <img class="backImage">
-                    <p class="comment"></p>
-                </div>
-            </template>
+        <div id="locations">
+            <button id="allLocations" class="locationBtn selected">ALL</button>
         </div>
 
+        <div id="container"></div>
+
+        <template id="currency">
+            <div class="currency">
+                <h1 class="location"></h1>
+                <p class="info"></p>
+                <img class="frontImage">
+                <img class="backImage">
+                <p class="comment"></p>
+            </div>
+        </template>
+
         <script>
+            let filterLocations = (location, currencies)=>{
+                let container = document.getElementById("container");
+                let template = document.getElementById("currency").content.children[0];
+
+                while(container.children.length > 0){
+                    container.removeChild(container.firstChild);
+                }
+
+                for(let i = 0; i < currencies.length; i++){
+                    if(currencies[i].location.toLowerCase() === location || location === "all"){
+                        let currency = template.cloneNode(true);
+                        currency.querySelector(".location").innerText = currencies[i].location.toUpperCase();
+                        currency.querySelector(".info").innerText = `${currencies[i].value} ${currencies[i].name} (${currencies[i].year})`;
+                        currency.querySelector(".frontImage").src = currencies[i].frontImage;
+                        currency.querySelector(".backImage").src = currencies[i].backImage;
+                        currency.querySelector(".comment").innerText = currencies[i].comment;
+                        container.appendChild(currency);
+                    }
+                }
+            }
+
             fetch("/currency/data")
                 .then(r=>r.json())
                 .then((response)=>{
+                    document.getElementById("allLocations").onclick = ()=>{filterLocations("all", response)};
+
                     let container = document.getElementById("container");
+                    let locationsContainer = document.getElementById("locations");
                     let template = document.getElementById("currency").content.children[0];
+                    let locations = [];
 
                     for(let i = 0; i < response.length; i++){
                         let currency = template.cloneNode(true);
@@ -33,6 +62,24 @@
                         currency.querySelector(".backImage").src = response[i].backImage;
                         currency.querySelector(".comment").innerText = response[i].comment;
                         container.appendChild(currency);
+                        
+                        if(!locations.includes(response[i].location.toLowerCase())){
+                            locations.push(response[i].location.toLowerCase());
+
+                            let button = document.createElement("button");
+                            button.classList.add("locationBtn");
+                            button.innerText = response[i].location.toUpperCase();
+                            button.onclick = ()=>{
+                                let pLocations = document.querySelectorAll(".locationBtn");
+                                for(let j = 0; j < pLocations.length; j++){
+                                    pLocations[j].classList.remove("selected");
+                                }
+
+                                button.classList.add("selected");
+                                filterLocations(response[i].location.toLowerCase(), response);
+                            };
+                            locationsContainer.appendChild(button);
+                        }
                     }
                 })
                 .catch((err)=>{});