-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
64 lines (56 loc) · 1.23 KB
/
weather.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
let state = reactive({
selectedCity: 'London',
weather: {
temperature: 'N/A',
humidity: 'N/A',
description: ''
}
});
const mockWeatherData = {
"New York": {
temperature: '15°C',
humidity: '55%',
description: 'Cloudy'
},
"London": {
temperature: '10°C',
humidity: '75%',
description: 'Rainy'
},
"Tokyo": {
temperature: '22°C',
humidity: '65%',
description: 'Sunny'
},
"Sydney": {
temperature: '25°C',
humidity: '60%',
description: 'Sunny'
}
};
function fetchWeather(city) {
setTimeout(() => {
const weather = mockWeatherData[city]
state.weather = weather
}, 500)
}
createEffect(function() {
fetchWeather(state.selectedCity);
})
createEffect(function() {
console.log('re-render')
render('#container', `<select onChange=updateSelectedCity(this.value)>
<option value="Tokyo"> Tokyo</option>
<option value="London"> London</option>
<option value="New York"> New York</option>
</select>
<div>
<p>Temperature: ${state.weather.temperature}</p>
<p>Humidity: ${state.weather.humidity}</p>
<p>Description: ${state.weather.description}</p>
</div>`)
})
function updateSelectedCity(city) {
state.selectedCity = city
fetchWeather(city)
}