Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes NaN classified TPI percentages because we were dividing by zero. #3868

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions web/src/features/fba/components/infoPanel/FireZoneUnitSummary.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React from 'react'
import { Grid, Typography } from '@mui/material'
import { isNull, isUndefined } from 'lodash'
import { FireShape, FireZoneTPIStats, FireZoneThresholdFuelTypeArea } from 'api/fbaAPI'
Expand All @@ -19,18 +19,6 @@ const FireZoneUnitSummary = ({
selectedFireZoneUnit
}: FireZoneUnitSummaryProps) => {
const theme = useTheme()
const [midSlope, setMidSlope] = useState<number>(0)
const [upperSlope, setUpperSlope] = useState<number>(0)
const [valleyBottom, setValleyBottom] = useState<number>(0)

useEffect(() => {
if (!isNull(fireZoneTPIStats)) {
const total = fireZoneTPIStats.mid_slope + fireZoneTPIStats.upper_slope + fireZoneTPIStats.valley_bottom
setMidSlope(Math.round(fireZoneTPIStats.mid_slope/total*100))
setUpperSlope(Math.round(fireZoneTPIStats.upper_slope/total*100))
setValleyBottom(Math.round(fireZoneTPIStats.valley_bottom/total*100))
}
}, [fireZoneTPIStats])

if (isUndefined(selectedFireZoneUnit)) {
return <div data-testid="fire-zone-unit-summary-empty"></div>
Expand All @@ -55,9 +43,9 @@ const FireZoneUnitSummary = ({
</Typography>
) : (
<ElevationStatus
upper={upperSlope}
mid={midSlope}
bottom={valleyBottom}
upper={fireZoneTPIStats.upper_slope}
mid={fireZoneTPIStats.mid_slope}
bottom={fireZoneTPIStats.valley_bottom}
></ElevationStatus>)}
</Grid>
</Grid>
Expand Down
10 changes: 7 additions & 3 deletions web/src/features/fba/components/viz/ElevationStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ interface ElevationStatusProps {

const ElevationStatus = ({ bottom, mid, upper }: ElevationStatusProps) => {
const theme = useTheme()
const total = mid + upper + bottom
const mid_percent = mid === 0 ? 0 : Math.round(mid/total*100)
const upper_percent = upper === 0 ? 0 : Math.round(upper/total*100)
const bottom_percent = bottom === 0 ? 0 : Math.round(bottom/total*100)
return (
<Box sx={{ paddingBottom: theme.spacing(2), paddingTop: theme.spacing(2) }} data-testid="elevation-status">
<Grid container sx={{ minHeight: theme.spacing(19) }} xs={12}>
Expand Down Expand Up @@ -59,9 +63,9 @@ const ElevationStatus = ({ bottom, mid, upper }: ElevationStatusProps) => {
Proportion of Advisory Area:
</Typography>
</Grid>
<ElevationFlag percent={upper} testId='upper-slope' />
<ElevationFlag percent={mid} testId='mid-slope' />
<ElevationFlag percent={bottom} testId='valley-bottom' />
<ElevationFlag percent={upper_percent} testId='upper-slope' />
<ElevationFlag percent={mid_percent} testId='mid-slope' />
<ElevationFlag percent={bottom_percent} testId='valley-bottom' />
</Grid>
</Grid>
</Box>
Expand Down
22 changes: 20 additions & 2 deletions web/src/features/fba/components/viz/elevationStatus.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,28 @@ describe('ElevationStatus', () => {

const midSlope = getByTestId('mid-slope')
expect(midSlope).toBeInTheDocument()
expect(midSlope).toHaveTextContent("1%")
expect(midSlope).toHaveTextContent("33%")

const upperSlope = getByTestId('upper-slope')
expect(upperSlope).toBeInTheDocument()
expect(upperSlope).toHaveTextContent("2%")
expect(upperSlope).toHaveTextContent("67%")
})

it('should render all zero classifications', () => {
const { getByTestId } = render(
<ElevationStatus bottom={0} mid={0} upper={0} />
)

const valleyBottom = getByTestId('valley-bottom')
expect(valleyBottom).toBeInTheDocument()
expect(valleyBottom).toHaveTextContent("0%")

const midSlope = getByTestId('mid-slope')
expect(midSlope).toBeInTheDocument()
expect(midSlope).toHaveTextContent("0%")

const upperSlope = getByTestId('upper-slope')
expect(upperSlope).toBeInTheDocument()
expect(upperSlope).toHaveTextContent("0%")
})
})
Loading