Skip to content

Commit

Permalink
Single Responsibility Principle
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Morgan-Tyghe committed Sep 25, 2020
1 parent a5b501e commit c409067
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 156 deletions.
34 changes: 2 additions & 32 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,8 @@

<p id="demo" class="lead text-muted">Showing Weather forecast for <div id="todaysdate"></div> <p id="weatherLocation"></p></p> <div id="todaysdate"></div>

<div class="weatherOuterContainer">
<div class="weatherInnerContainer">
<div id="i1"></div>
<div id="i2"></div>
<div id="i3"></div>
<div id="i4"></div>
<img id="i5"><img>
</div>
<div class="weatherInnerContainer">
<div id="i6"></div>
<div id="i7"></div>
<div id="i8"></div>
<div id="i9"></div>
<img id="i10"><img>

</div>
<div class="weatherInnerContainer">
<div id="i11"></div>
<div id="i12"></div>
<div id="i13"></div>
<div id="i14"></div>
<img id="i15"><img>

</div>
<div class="weatherInnerContainer">
<div id="i16"></div>
<div id="i17"></div>
<div id="i18"></div>
<div id="i19"></div>
<img id="i20"><img>

</div>
<div id="weatherOuterContainer" class="weatherOuterContainer">

</div>

</div>
Expand Down
126 changes: 50 additions & 76 deletions public/js/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/js/index.min.js.map

Large diffs are not rendered by default.

83 changes: 41 additions & 42 deletions src/Typescript/WeatherDay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,49 @@
// // sun/cloud
import axios from 'axios'

export async function weatherData(api : string) {
let indexOf = 1
let i = 0



try {
axios.get(api)
.then((response: any) => {
for (i = 0; i <= 3; i++) {
var nameValue = response.data['city']['name']
export function getWeatherData(api : string) {

var tempValue = response.data['list'][i.toString()]['main']['temp']

var dateValue = response.data['list'][i.toString()]['dt_txt']

var descValue = response.data['list'][i.toString()]['weather']['0']['description']

var iconValue = response.data['list'][i.toString()]['weather']['0']['icon']


var tempValueC = (parseInt(tempValue) - 273.15).toFixed(0) //Kelvin To Celsius
document.getElementById("i" + indexOf.toString()).innerHTML = nameValue
indexOf++
document.getElementById("i" + indexOf.toString()).innerHTML = tempValueC + 'C'
indexOf++
document.getElementById("i" + indexOf.toString()).innerHTML = dateValue
indexOf++
document.getElementById("i" + indexOf.toString()).innerHTML = descValue
indexOf++
(<HTMLImageElement>document.getElementById("i" + indexOf.toString())).src = 'https://openweathermap.org/img/wn/' + iconValue + '@2x.png'
indexOf++
if (indexOf > 20) { indexOf = 1 }

} i = 0

})

return api

} catch (error) {
alert(error)
}
}
return axios.get(api)
.then(function (response) {
return response.data
})
.catch(function(error){
console.log(error)
})
}




export function addDataToDivs(weatherData: any) {
let li = document.getElementsByClassName("weatherInnerContainer")
let nI = li.length
for (var i = 0; i < nI; i++) {


let tempInC = ((parseInt(weatherData['list'][i.toString()]['main']['temp'])) - 273.15).toFixed(0)
var tempDiv = document.createElement('div')
tempDiv.innerText = tempInC
li[i].appendChild(tempDiv)

var nameDiv = document.createElement('div')
nameDiv.innerText = weatherData['city']['name']
li[i].appendChild(nameDiv)

var timeDiv = document.createElement('div')
timeDiv.innerText = weatherData['list'][i.toString()]['dt_txt']
li[i].appendChild(timeDiv)

var descDiv = document.createElement('div')
descDiv.innerText = weatherData['list'][i.toString()]['weather']['0']['description']
li[i].appendChild(descDiv)

let iconDiv = document.createElement('img')
let iconValue = (weatherData['list'][i.toString()]['weather']['0']['icon']).toString()
let iconLink = 'https://openweathermap.org/img/wn/' + iconValue + '@2x.png'
iconDiv.src = iconLink
li[i].appendChild(iconDiv)

}
}
33 changes: 28 additions & 5 deletions src/Typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
// City button onClick -> axios GET -> handle request and construct
// 7 WeatherDay objects -> render those 7 objects on the page
import axios from 'axios'
import { weatherData } from './WeatherDay'
import { getWeatherData } from './WeatherDay'
import { addDataToDivs } from './WeatherDay'
import { getCity } from './City'
import { getApi } from './City'

var howManyReadings = 7


document.querySelectorAll('.allButtons').forEach(item => {
Expand All @@ -16,13 +18,34 @@ document.querySelectorAll('.allButtons').forEach(item => {
})

function main() {

var city = getCity()
var api = getApi(city)
weatherData(api)



getWeatherData(api).then(weatherData => {
clearDivs()
addDivs(howManyReadings)
addDataToDivs(weatherData)
})
}



function clearDivs() {

const myNode = document.getElementById("weatherOuterContainer")
while (myNode.firstChild) {
myNode.removeChild(myNode.lastChild);
}
}

function addDivs(howManyReadings: number) {
const myNode = document.getElementById("weatherOuterContainer")
for (let i = 0; i < howManyReadings; i++) {
myNode.appendChild (document.createElement('div')).classList.add("weatherInnerContainer")
}




}

0 comments on commit c409067

Please sign in to comment.