|
5 | 5 | <title>title</title>
|
6 | 6 | </head>
|
7 | 7 | <body>
|
8 |
| - <button id="btn-call-func">Call function</button> |
9 |
| - <p id="p-result"></p> |
| 8 | + <h1>Many forecasts</h1> |
| 9 | + <p>Click the button to get the forecast for all your favorite locations.</p> |
| 10 | + <button id="btn-call-func">Get forecasts</button> |
| 11 | + <div id="forecasts"></div> |
10 | 12 | <script type="module">
|
11 | 13 | import { initializeApp } from "https://www.gstatic.com/firebasejs/11.2.0/firebase-app.js";
|
12 | 14 | import {
|
|
16 | 18 | } from "https://www.gstatic.com/firebasejs/11.2.0/firebase-functions.js";
|
17 | 19 |
|
18 | 20 | const callableButton = document.getElementById("btn-call-func");
|
19 |
| - const resultElement = document.getElementById("p-result"); |
| 21 | + const resultElement = document.getElementById("forecasts"); |
20 | 22 |
|
21 | 23 | callableButton.onclick = handleClick;
|
22 | 24 |
|
23 | 25 | // https://firebase.google.com/docs/hosting/reserved-urls#sdk_auto-configuration
|
24 |
| - const firebaseConfig = await fetch('/__/firebase/init.json').then(response => { |
25 |
| - return response.json() |
26 |
| - }); |
| 26 | + const firebaseConfig = await fetch("/__/firebase/init.json").then( |
| 27 | + (response) => { |
| 28 | + return response.json(); |
| 29 | + } |
| 30 | + ); |
27 | 31 | const app = initializeApp(firebaseConfig);
|
28 | 32 | const functions = getFunctions(app);
|
29 | 33 | connectFunctionsEmulator(functions, "127.0.0.1", 5001);
|
30 |
| - |
31 | 34 |
|
32 | 35 | const favoriteLocations = [
|
33 |
| - // Google HQ |
34 |
| - { latitude: 37.4220199895279, longitude: -122.08531347325561 }, |
35 |
| - // Yosemite Valley |
36 |
| - { latitude: 37.745192257741984, longitude: -119.5945133017153 }, |
37 |
| - // Old Faithful |
38 |
| - { latitude: 44.46037818049411, longitude: -110.82802255265777 }, |
39 |
| - ]; |
| 36 | + { |
| 37 | + name: "The Googleplex", |
| 38 | + latitude: 37.4220199895279, |
| 39 | + longitude: -122.08531347325561, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Yosemite Valley", |
| 43 | + latitude: 37.745192257741984, |
| 44 | + longitude: -119.5945133017153, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Old Faithful", |
| 48 | + latitude: 44.46037818049411, |
| 49 | + longitude: -110.82802255265777, |
| 50 | + }, |
| 51 | + ]; |
40 | 52 |
|
41 | 53 | async function handleClick() {
|
42 | 54 | // reset result
|
43 |
| - clearUi(); |
| 55 | + initializeUi(); |
44 | 56 |
|
45 | 57 | // [START stream_data_client]
|
46 | 58 | // Get the callable by passing an initialized functions SDK.
|
47 | 59 | const getForecast = httpsCallable(functions, "getForecast");
|
48 | 60 |
|
49 | 61 | // Call the function with the `.stream()` method to start streaming.
|
50 |
| - const {stream, data} = await getForecast.stream({ |
| 62 | + const { stream, data } = await getForecast.stream({ |
51 | 63 | locations: favoriteLocations,
|
52 | 64 | });
|
53 | 65 |
|
|
67 | 79 | // [END stream_data_client]
|
68 | 80 | }
|
69 | 81 |
|
70 |
| - function clearUi() { |
| 82 | + function initializeUi() { |
71 | 83 | resultElement.innerHTML = "";
|
| 84 | + callableButton.disabled = true; |
| 85 | + callableButton.innerText = "Streaming forecasts..."; |
| 86 | + } |
| 87 | + |
| 88 | + function finalizeUi() { |
| 89 | + callableButton.disabled = false; |
| 90 | + callableButton.innerText = "Get forecasts"; |
72 | 91 | }
|
73 | 92 |
|
74 | 93 | function updateUi(forecastData) {
|
75 |
| - resultElement.innerHTML += '<pre>' + JSON.stringify( |
76 |
| - { |
77 |
| - latitude: forecastData.latitude, |
78 |
| - longitude: forecastData.longitude, |
79 |
| - forecast: forecastData.forecast.properties.periods[0], |
80 |
| - }, |
81 |
| - null, |
82 |
| - 2 |
83 |
| - ); + '</pre>' |
| 94 | + const newWeatherCard = document.createElement("div"); |
| 95 | + |
| 96 | + const locationName = document.createElement("h2"); |
| 97 | + locationName.innerHTML = favoriteLocations.find( |
| 98 | + (v) => v.latitude === forecastData.latitude |
| 99 | + ).name; |
| 100 | + |
| 101 | + const forecast = document.createElement("p"); |
| 102 | + console.log(forecastData); |
| 103 | + forecast.innerHTML = |
| 104 | + forecastData.forecast.properties.periods[0].detailedForecast; |
| 105 | + |
| 106 | + newWeatherCard.append(locationName, forecast); |
| 107 | + resultElement.appendChild(newWeatherCard); |
84 | 108 | }
|
85 | 109 | </script>
|
86 | 110 | </body>
|
|
0 commit comments