-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
125 lines (103 loc) · 3.2 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Global Variables */
const generate = document.querySelector('#generate');
const reset = document.querySelector("#reset");
const majorUI = document.querySelector(".inputs");
const resultUI = document.querySelector(".results");
/* Personal API Key from OpenWeatherMap */
const API_KEY = 'd049ed25ecfb2a1d010c09c7c67a6402'
// Create a new date instance dynamically with JS
let date = new Date();
let newDate = `${date.getMonth()+1}.${date.getDate()}.${date.getFullYear()}`;
/* Function to GET data */
const getWeatherData = async (URL) => {
/*
Solution Steps:
1. Fetch API From Open Weather Map
2. Convert it to JSON Object to send it to server.
3. Catch error if happen.
*/
try {
const response = await fetch(URL);
const data = await response.json();
return data
} catch (error) {
console.error(`Error: ${error}`);
}
}
/* Function to POST data */
const postWeatherData = async(URL="", data={}) => {
const response = await fetch (URL, {
method: "POST",
credentials: "same-origin",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
try {
const theData = await response.json();
return theData;
} catch (error) {
console.error(`Error: ${error}`);
}
}
const displayData = async () => {
majorUI.classList.add("d-none");
resultUI.classList.remove("d-none");
let country = document.querySelector("#country");
let city = document.querySelector("#city");
let temp = document.querySelector("#temp");
let date = document.querySelector("#date");
let feeling = document.querySelector("#feeling");
try {
const response = await fetch("/getData");
const responseJSON = await response.json();
// Display data into user
country.innerHTML = responseJSON.country;
feeling.innerHTML = responseJSON.feeling
city.innerHTML = responseJSON.city;
temp.innerHTML = responseJSON.temp;
date.innerHTML = response.newDate;
} catch (error) {
console.error(`Error: ${error}`);
}
}
// Function called by event listener.
const main = async () => {
/**
Solution Steps:
1. read user inputs
2. read weather from api
3. parse major data.
4. create data object to post in server
4. post data object data into server
4. read weather data from server
5. update user interface with new data
*/
var zip = document.querySelector("#zip").value;
var feeling = document.querySelector("#feeling").value;
const URL = `https://api.openweathermap.org/data/2.5/weather?zip=${zip}&appid=${API_KEY}&units=metric`;
getWeatherData(URL).then( data => {
var country = data.sys.country;
var city = data.name;
var temp = parseInt(data.main.temp);
const data_obj = {
newDate:newDate,
country:country,
city:city,
temp:temp,
feeling:feeling
};
// console.log(data_obj); // it works;
postWeatherData("/addData", data_obj).then( data => {
displayData(`Data : ${data}`);
})
})
};
const resetData = () => {
// console.log("test")
}
// Event listener to add function to existing HTML DOM element
generate.addEventListener("click", main);
reset.addEventListener("click", resetData);