Skip to content

Commit d98c513

Browse files
authored
Merge pull request #1244 from cmu-delphi/rzatserkovnyi/export
Set COVIDcast export values based on URL parameters
2 parents 9dbf917 + 333a79c commit d98c513

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/modes/exportdata/ExportData.svelte

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,37 @@
2727
}
2828
}
2929
30+
// Pre-fill the form from URL parameters.
31+
// Supported parameters: data_source, signal, start_day, end_day, geo_type, geo_value, as_of
32+
const urlParams = new URLSearchParams(window.location.search);
33+
34+
// Overwrite default source & sensor if these are present in the URL
35+
if (urlParams.has('data_source')) {
36+
sourceValue = urlParams.get('data_source');
37+
38+
if (urlParams.has('signal')) {
39+
sensorValue = urlParams.get('signal');
40+
}
41+
}
42+
3043
$: isWeekly = sensor ? sensor.isWeeklySignal : false;
3144
$: formatDate = isWeekly ? (d) => formatWeek(d).replace('W', '') : formatDateISO;
3245
33-
let geoType = 'county';
46+
let geoType = urlParams.has('geo_type') ? urlParams.get('geo_type') : 'county';
3447
3548
let startDate = new Date();
3649
let endDate = new Date();
3750
3851
function initDate(date) {
3952
const param = new DateParam(date);
40-
endDate = param.sparkLineTimeFrame.max;
41-
startDate = param.sparkLineTimeFrame.min;
53+
54+
// Populate date based on URL params or, if absent, the current date
55+
startDate = urlParams.has('start_day') ? new Date(urlParams.get('start_day')) : param.sparkLineTimeFrame.min;
56+
endDate = urlParams.has('end_day') ? new Date(urlParams.get('end_day')) : param.sparkLineTimeFrame.max;
57+
58+
// Also normalize the dates to the current timezone
59+
startDate = new Date(startDate.getTime() - startDate.getTimezoneOffset() * -60000);
60+
endDate = new Date(endDate.getTime() - endDate.getTimezoneOffset() * -60000);
4261
}
4362
$: initDate($currentDateObject);
4463
@@ -59,12 +78,20 @@
5978
6079
let geoValuesMode = 'all';
6180
let geoValues = [];
81+
let geoURLSet = false;
6282
$: geoItems = [...(infosByLevel[geoType] || []), ...(geoType === 'county' ? infosByLevel.state : [])];
6383
$: {
6484
if (geoItems != null) {
6585
geoValues = [];
6686
geoValuesMode = guessMode(geoItems.length);
6787
}
88+
89+
// Populate region based on URL params... but let the user override this later
90+
if (urlParams.has('geo_value') && !geoURLSet) {
91+
let infos = infosByLevel[geoType].filter((d) => d.propertyId == urlParams.get('geo_value'));
92+
addRegion(infos[0]);
93+
geoURLSet = true;
94+
}
6895
}
6996
7097
function flatIds(geoValues) {
@@ -90,6 +117,13 @@
90117
}
91118
}
92119
$: usesAsOf = asOfMode !== 'latest' && asOfDate instanceof Date;
120+
// set as_of based on URL params, if it's a valid date
121+
if (urlParams.has('as_of') && !isNaN(new Date(urlParams.get('as_of')))) {
122+
asOfMode = 'single';
123+
asOfDate = new Date(urlParams.get('as_of'));
124+
// Also normalize the dates to the current timezone
125+
asOfDate = new Date(asOfDate.getTime() - asOfDate.getTimezoneOffset() * -60000);
126+
}
93127
94128
let form = null;
95129
@@ -104,6 +138,14 @@
104138
);
105139
});
106140
}
141+
// Fix up the UI if we got an inactive sensor from the URL parameters.
142+
if (sensor && !sensor.active) {
143+
showInActive = true;
144+
// Force an update to sourceValue to set related reactive statements correctly.
145+
let temp = sourceValue;
146+
sourceValue = '';
147+
sourceValue = temp;
148+
}
107149
});
108150
109151
function addRegion(detail) {

0 commit comments

Comments
 (0)