hooks#useTracker JavaScript Examples
The following examples show how to use
hooks#useTracker.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.js From my-coronavirus-map with MIT License | 4 votes |
IndexPage = () => {
const { data: stats = {} } = useTracker({
api: 'all',
});
const { data: countries = [] } = useTracker({
api: 'countries',
});
const hasCountries = Array.isArray( countries ) && countries.length > 0;
const dashboardStats = [
{
primary: {
label: 'Total Cases',
value: stats ? commafy( stats?.cases ) : '-',
},
secondary: {
label: 'Per 1 Million',
value: stats ? commafy( stats?.casesPerOneMillion ) : '-',
},
},
{
primary: {
label: 'Total Deaths',
value: stats ? commafy( stats?.deaths ) : '-',
},
secondary: {
label: 'Per 1 Million',
value: stats ? commafy( stats?.deathsPerOneMillion ) : '-',
},
},
{
primary: {
label: 'Total Tests',
value: stats ? commafy( stats?.tests ) : '-',
},
secondary: {
label: 'Per 1 Million',
value: stats ? commafy( stats?.testsPerOneMillion ) : '-',
},
},
{
primary: {
label: 'Active Cases',
value: stats ? commafy( stats?.active ) : '-',
},
},
{
primary: {
label: 'Critical Cases',
value: stats ? commafy( stats?.critical ) : '-',
},
},
{
primary: {
label: 'Recovered Cases',
value: stats ? commafy( stats?.recovered ) : '-',
},
},
];
/**
* mapEffect
* @description Fires a callback once the page renders
* @example Here this is and example of being used to zoom in and set a popup on load
*/
async function mapEffect({ leafletElement: map } = {}) {
if ( !hasCountries || !map ) return;
map.eachLayer(( layer ) => {
if ( layer?.options?.name === 'OpenStreetMap' ) return;
map.removeLayer( layer );
});
const geoJson = {
type: 'FeatureCollection',
features: countries.map(( country = {}) => {
const { countryInfo = {} } = country;
const { lat, long: lng } = countryInfo;
return {
type: 'Feature',
properties: {
...country,
},
geometry: {
type: 'Point',
coordinates: [lng, lat],
},
};
}),
};
const geoJsonLayers = new L.GeoJSON( geoJson, {
pointToLayer: ( feature = {}, latlng ) => {
const { properties = {} } = feature;
let updatedFormatted;
let casesString;
const { country, updated, cases, deaths, recovered } = properties;
casesString = `${cases}`;
if ( cases > 1000000 ) {
casesString = `${casesString.slice( 0, -6 )}M+`;
} else if ( cases > 1000 ) {
casesString = `${casesString.slice( 0, -3 )}K+`;
}
if ( updated ) {
updatedFormatted = new Date( updated ).toLocaleString();
}
const html = `
<span class="icon-marker">
<span class="icon-marker-tooltip">
<h2>${country}</h2>
<ul>
<li><strong>Confirmed:</strong> ${cases}</li>
<li><strong>Deaths:</strong> ${deaths}</li>
<li><strong>Recovered:</strong> ${recovered}</li>
<li><strong>Last Update:</strong> ${updatedFormatted}</li>
</ul>
</span>
${casesString}
</span>
`;
return L.marker( latlng, {
icon: L.divIcon({
className: 'icon',
html,
}),
riseOnHover: true,
});
},
});
geoJsonLayers.addTo( map );
}
const mapSettings = {
center: CENTER,
defaultBaseMap: 'OpenStreetMap',
zoom: DEFAULT_ZOOM,
mapEffect,
};
return (
<Layout pageName="home">
<Helmet>
<title>Home Page</title>
</Helmet>
<div className="tracker">
<Map {...mapSettings} />
<div className="tracker-stats">
<ul>
{ dashboardStats.map(({ primary = {}, secondary = {} }, i ) => {
return (
<li key={`Stat-${i}`} className="tracker-stat">
{ primary.value && (
<p className="tracker-stat-primary">
{ primary.value }
<strong>{ primary.label }</strong>
</p>
) }
{ secondary.value && (
<p className="tracker-stat-secondary">
{ secondary.value }
<strong>{ secondary.label }</strong>
</p>
) }
</li>
);
}) }
</ul>
</div>
<div className="tracker-last-updated">
<p>Last Updated: { stats ? friendlyDate( stats?.updated ) : '-' }</p>
</div>
</div>
<Container type="content" className="text-center home-start">
<h2>Still Getting Started?</h2>
<p>Run the following in your terminal!</p>
<pre>
<code>gatsby new [directory] https://github.com/colbyfayock/gatsby-starter-leaflet</code>
</pre>
<p className="note">Note: Gatsby CLI required globally for the above command</p>
</Container>
</Layout>
);
}