Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

Commit

Permalink
Fix: Prevent LD state page from crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
shuklaayush committed Jul 6, 2020
1 parent 9c7c859 commit b18aaa4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
28 changes: 18 additions & 10 deletions src/components/DeltaBarGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ function DeltaBarGraph({timeseries, statistic, lookback}) {
.domain([
Math.min(
0,
min(dates, (date) => getDeltaStatistic(timeseries[date], statistic))
min(dates, (date) => getDeltaStatistic(timeseries?.[date], statistic))
),
Math.max(
1,
max(dates, (date) => getDeltaStatistic(timeseries[date], statistic))
max(dates, (date) => getDeltaStatistic(timeseries?.[date], statistic))
),
])
.range([chartBottom, margin.top]);
Expand All @@ -72,7 +72,7 @@ function DeltaBarGraph({timeseries, statistic, lookback}) {
.selectAll('text')
.attr('y', 0)
.attr('dy', (date, i) =>
getDeltaStatistic(timeseries[date], statistic) < 0 ? '-1em' : '1.5em'
getDeltaStatistic(timeseries?.[date], statistic) < 0 ? '-1em' : '1.5em'
)
.style('text-anchor', 'middle')
.attr('fill', COLORS[statistic]);
Expand All @@ -94,7 +94,7 @@ function DeltaBarGraph({timeseries, statistic, lookback}) {
xScale(date),
yScale(0),
xScale.bandwidth(),
yScale(0) - yScale(getDeltaStatistic(timeseries[date], statistic)),
yScale(0) - yScale(getDeltaStatistic(timeseries?.[date], statistic)),
r
)
)
Expand All @@ -111,14 +111,14 @@ function DeltaBarGraph({timeseries, statistic, lookback}) {
.attr('class', 'label')
.attr('x', (date) => xScale(date) + xScale.bandwidth() / 2)
.text((date) =>
formatNumber(getDeltaStatistic(timeseries[date], statistic))
formatNumber(getDeltaStatistic(timeseries?.[date], statistic))
);

textSelection
.transition(t)
.attr('fill', COLORS[statistic])
.attr('y', (date) => {
const val = getDeltaStatistic(timeseries[date], statistic);
const val = getDeltaStatistic(timeseries?.[date], statistic);
return yScale(val) + (val < 0 ? 15 : -6);
});

Expand All @@ -127,14 +127,20 @@ function DeltaBarGraph({timeseries, statistic, lookback}) {
.attr(
'dy',
(date) =>
`${getDeltaStatistic(timeseries[date], statistic) < 0 ? 1.2 : -1.2}em`
`${
getDeltaStatistic(timeseries?.[date], statistic) < 0 ? 1.2 : -1.2
}em`
)
.attr('x', (date) => xScale(date) + xScale.bandwidth() / 2)
.text((date, i) => {
if (i === 0) return '';
const prevVal = getDeltaStatistic(timeseries[dates[i - 1]], statistic);
const prevVal = getDeltaStatistic(
timeseries?.[dates[i - 1]],
statistic
);
if (!prevVal) return '';
const delta = getDeltaStatistic(timeseries[date], statistic) - prevVal;
const delta =
getDeltaStatistic(timeseries?.[date], statistic) - prevVal;
return `${delta > 0 ? '+' : ''}${formatNumber(
(100 * delta) / Math.abs(prevVal)
)}%`;
Expand All @@ -160,7 +166,9 @@ function DeltaBarGraph({timeseries, statistic, lookback}) {
}

const isEqual = (prevProps, currProps) => {
if (!currProps.timeseries) {
if (currProps.forceRender) {
return false;
} else if (!currProps.timeseries && prevProps.timeseries) {
return true;
} else if (currProps.timeseries && !prevProps.timeseries) {
return false;
Expand Down
4 changes: 3 additions & 1 deletion src/components/Minigraphs.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ function Minigraphs({timeseries, date: timelineDate}) {
}

const isEqual = (prevProps, currProps) => {
if (!currProps.timeseries) {
if (currProps.forceRender) {
return false;
} else if (!currProps.timeseries && prevProps.timeseries) {
return true;
} else if (currProps.timeseries && !prevProps.timeseries) {
return false;
Expand Down
12 changes: 8 additions & 4 deletions src/components/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ function State(props) {
stateCode: stateCode,
districtName: null,
});
setShowAllDistricts(false);
}
}, [regionHighlighted.stateCode, stateCode]);

const {data: timeseries} = useSWR(
const {data: timeseries, error: timeseriesResponseError} = useSWR(
`${API_ROOT_URL}/timeseries-${stateCode}.min.json`,
fetcher,
{
Expand Down Expand Up @@ -130,6 +131,7 @@ function State(props) {
<Minigraphs
timeseries={timeseries?.[stateCode]?.dates}
{...{stateCode}}
forceRender={!!timeseriesResponseError}
/>
</div>

Expand All @@ -150,13 +152,13 @@ function State(props) {

<span ref={stateMetaElement} />

{data && timeseries && isStateMetaVisible && (
{data && isStateMetaVisible && (
<StateMeta
{...{
stateCode,
data,
timeseries,
}}
timeseries={timeseries?.[stateCode]?.dates}
/>
)}
</div>
Expand Down Expand Up @@ -252,8 +254,9 @@ function State(props) {
)}
<DeltaBarGraph
timeseries={timeseries?.[stateCode]?.dates}
{...{stateCode, lookback}}
statistic={mapStatistic}
{...{stateCode, lookback}}
forceRender={!!timeseriesResponseError}
/>
</div>
</div>
Expand Down Expand Up @@ -281,6 +284,7 @@ function State(props) {
regionHighlighted,
setRegionHighlighted,
}}
forceRender={!!timeseriesResponseError}
/>
</Suspense>
</React.Fragment>
Expand Down
6 changes: 4 additions & 2 deletions src/components/StateMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function StateMeta({stateCode, data, timeseries}) {
const prevWeekDate = format(sub(getIndiaDate(), {weeks: 1}), 'yyyy-MM-dd');

const prevWeekConfirmed = getStatistic(
timeseries[stateCode]?.[prevWeekDate],
timeseries?.[prevWeekDate],
'total',
'confirmed'
);
Expand Down Expand Up @@ -182,7 +182,9 @@ function StateMeta({stateCode, data, timeseries}) {
}

const isEqual = (prevProps, currProps) => {
if (prevProps.stateCode !== currProps.stateCode) {
if (currProps.timeseries && !prevProps.timeseries) {
return false;
} else if (prevProps.stateCode !== currProps.stateCode) {
return false;
}
return true;
Expand Down
4 changes: 3 additions & 1 deletion src/components/TimeseriesExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ function TimeseriesExplorer({
}

const isEqual = (prevProps, currProps) => {
if (!currProps.timeseries) {
if (currProps.forceRender) {
return false;
} else if (!currProps.timeseries && prevProps.timeseries) {
return true;
} else if (currProps.timeseries && !prevProps.timeseries) {
return false;
Expand Down

0 comments on commit b18aaa4

Please sign in to comment.