Skip to content

Commit ea95459

Browse files
committed
refine UI
1 parent 2ea5c80 commit ea95459

File tree

1 file changed

+50
-26
lines changed
  • Node/quickstarts/callable-functions-streaming/website

1 file changed

+50
-26
lines changed

Node/quickstarts/callable-functions-streaming/website/index.html

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
<title>title</title>
66
</head>
77
<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>
1012
<script type="module">
1113
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.2.0/firebase-app.js";
1214
import {
@@ -16,38 +18,48 @@
1618
} from "https://www.gstatic.com/firebasejs/11.2.0/firebase-functions.js";
1719

1820
const callableButton = document.getElementById("btn-call-func");
19-
const resultElement = document.getElementById("p-result");
21+
const resultElement = document.getElementById("forecasts");
2022

2123
callableButton.onclick = handleClick;
2224

2325
// 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+
);
2731
const app = initializeApp(firebaseConfig);
2832
const functions = getFunctions(app);
2933
connectFunctionsEmulator(functions, "127.0.0.1", 5001);
30-
3134

3235
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+
];
4052

4153
async function handleClick() {
4254
// reset result
43-
clearUi();
55+
initializeUi();
4456

4557
// [START stream_data_client]
4658
// Get the callable by passing an initialized functions SDK.
4759
const getForecast = httpsCallable(functions, "getForecast");
4860

4961
// Call the function with the `.stream()` method to start streaming.
50-
const {stream, data} = await getForecast.stream({
62+
const { stream, data } = await getForecast.stream({
5163
locations: favoriteLocations,
5264
});
5365

@@ -67,20 +79,32 @@
6779
// [END stream_data_client]
6880
}
6981

70-
function clearUi() {
82+
function initializeUi() {
7183
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";
7291
}
7392

7493
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);
84108
}
85109
</script>
86110
</body>

0 commit comments

Comments
 (0)