|
| 1 | +export const getDataFromSearch = () => { |
| 2 | + const searchBar = document.getElementById("search__bar"); |
| 3 | + const properText = toProperText(searchBar.value); |
| 4 | + return properText; |
| 5 | +}; |
| 6 | +const toProperText = (text) => { |
| 7 | + const improperText = text; |
| 8 | + const properText = text[0].toUpperCase() + text.slice(1).toLowerCase(); |
| 9 | + return properText; |
| 10 | +}; |
| 11 | + |
| 12 | +// Dom Element |
| 13 | + |
| 14 | +export const DisplayDataInApp = (weatherObj) => { |
| 15 | + const main = document.getElementById("main"); |
| 16 | + const footer = document.getElementById("footer"); |
| 17 | + // console.log(main); |
| 18 | + |
| 19 | + //temp data |
| 20 | + clearDisplay(main); |
| 21 | + const temp = createElem("div", "temp"); |
| 22 | + const tempValue = createElem("h1"); |
| 23 | + const unit = createElem("p"); |
| 24 | + content(tempValue, `${Math.round(weatherObj.temp) || "0"}°`); |
| 25 | + content(unit, "C"); |
| 26 | + const Location = createElem("div", "location"); |
| 27 | + const LocationText = createElem("p"); |
| 28 | + content(LocationText, weatherObj.location); |
| 29 | + |
| 30 | + const icon = createElem("div", "weather-icon"); |
| 31 | + const i = createElem("i"); |
| 32 | + const iClass = transformWeatherIcon(i, weatherObj.icon || "01d"); |
| 33 | + |
| 34 | + addChild(temp, tempValue, unit); |
| 35 | + addChild(Location, LocationText); |
| 36 | + addChild(icon, i); |
| 37 | + addChild(main, temp, Location, icon); |
| 38 | + |
| 39 | + //footer data |
| 40 | + clearDisplay(footer); |
| 41 | + const humidity = createElem("div", "humdity"); |
| 42 | + const humidityValue = createElem("h2"); |
| 43 | + const humidityText = createElem("p"); |
| 44 | + content(humidityValue, `${weatherObj.humidity || "0"} %`); |
| 45 | + content(humidityText, "Humidity"); |
| 46 | + addChild(humidity, humidityValue, humidityText); |
| 47 | + |
| 48 | + const wind = createElem("div", "wind"); |
| 49 | + const windValue = createElem("h2"); |
| 50 | + const windText = createElem("p"); |
| 51 | + content(windValue, `${weatherObj.wind || "0"} m/s`); |
| 52 | + content(windText, "wind-speed"); |
| 53 | + addChild(wind, windValue, windText); |
| 54 | + |
| 55 | + addChild(footer, humidity, wind); |
| 56 | + fadeIn(); |
| 57 | +}; |
| 58 | +/* |
| 59 | +<footer id="footer"> |
| 60 | + <div class="humidity" id="humidity"> |
| 61 | + <h2>58%</h2> |
| 62 | + <p>humidity</p> |
| 63 | + </div> |
| 64 | + <div class="wind" id="wind"> |
| 65 | + <h2>52mph</h2> |
| 66 | + <p>wind-speed</p> |
| 67 | + </div> |
| 68 | + </footer> |
| 69 | +*/ |
| 70 | +const fadeIn = () => { |
| 71 | + const main = document.getElementById("main"); |
| 72 | + main.classList.toggle("zero-vis"); |
| 73 | + main.classList.toggle("fade-in"); |
| 74 | + const footer = document.getElementById("footer"); |
| 75 | + footer.classList.toggle("zero-vis"); |
| 76 | + footer.classList.toggle("fade-in"); |
| 77 | +}; |
| 78 | + |
| 79 | +const createElem = (tag, className) => { |
| 80 | + const Elem = document.createElement(tag); |
| 81 | + if (className) { |
| 82 | + Elem.classList.add(className); |
| 83 | + } |
| 84 | + return Elem; |
| 85 | +}; |
| 86 | + |
| 87 | +const content = (elem, text) => { |
| 88 | + elem.textContent = text; |
| 89 | +}; |
| 90 | + |
| 91 | +function addChild(value, ...args) { |
| 92 | + return args.forEach((args) => { |
| 93 | + value.appendChild(args); |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +const clearDisplay = (tag) => { |
| 98 | + let lastChild = tag.lastElementChild; |
| 99 | + while (lastChild) { |
| 100 | + tag.removeChild(lastChild); |
| 101 | + lastChild = tag.lastElementChild; |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +const transformWeatherIcon = (i, icon) => { |
| 106 | + const firstTwoChars = icon.slice(0, 2); |
| 107 | + const lastChar = icon.slice(2); |
| 108 | + // console.log(firstTwoChars); |
| 109 | + switch (firstTwoChars) { |
| 110 | + case "01": |
| 111 | + if (lastChar === "d") { |
| 112 | + i.classList.add("fa", "fa-sun-o"); |
| 113 | + } else { |
| 114 | + i.classList.add("fa", "fa-moon"); |
| 115 | + } |
| 116 | + break; |
| 117 | + case "02": |
| 118 | + if (lastChar === "d") { |
| 119 | + i.classList.add("fa", "fa-cloud-sun"); |
| 120 | + } else { |
| 121 | + i.classList.add("fa", "fa-cloud-moon"); |
| 122 | + } |
| 123 | + break; |
| 124 | + case "03": |
| 125 | + i.classList.add("fa-solid", "fa-cloud"); |
| 126 | + break; |
| 127 | + case "04": |
| 128 | + i.classList.add("fa-brands", "fa-soundcloud"); |
| 129 | + break; |
| 130 | + case "09": |
| 131 | + i.classList.add("fa-solid", "fa-cloud-showers-water"); |
| 132 | + break; |
| 133 | + case "10": |
| 134 | + if (lastChar === "d") { |
| 135 | + i.classList.add("fa-solid", "fa-cloud-sun-rain"); |
| 136 | + } else { |
| 137 | + i.classList.add("fa-solid", "fa-cloud-moon-rain"); |
| 138 | + } |
| 139 | + break; |
| 140 | + case "11": |
| 141 | + i.classList.add("fa-solid", "fa-cloud-bolt"); |
| 142 | + break; |
| 143 | + case "13": |
| 144 | + i.classList.add("fa-solid", "fa-snowflake"); |
| 145 | + break; |
| 146 | + case "50": |
| 147 | + i.classList.add("fa-solid", "fa-bars-staggered"); |
| 148 | + break; |
| 149 | + default: |
| 150 | + i.classList.add("far", "fa-question"); |
| 151 | + } |
| 152 | +}; |
| 153 | + |
| 154 | +// const div = document.createElement("div"); |
| 155 | +// const h1 = document.createElement("h1"); |
| 156 | +// const p = document.createElement("p"); |
| 157 | +// h1.textContent = "hello"; |
| 158 | +// p.textContent = "no"; |
| 159 | +// div.appendChild(h1); |
| 160 | +// div.appendChild(p); |
| 161 | +// div.classList.add("temp"); |
| 162 | +// main.appendChild(div); |
| 163 | +// main.appendChild(div); |
0 commit comments