diff --git a/spotlight-client/src/contentModels/DemographicsByCategoryMetric.ts b/spotlight-client/src/contentModels/DemographicsByCategoryMetric.ts index a72261b3..b800793e 100644 --- a/spotlight-client/src/contentModels/DemographicsByCategoryMetric.ts +++ b/spotlight-client/src/contentModels/DemographicsByCategoryMetric.ts @@ -15,9 +15,22 @@ // along with this program. If not, see . // ============================================================================= -import { DemographicsByCategoryRecord } from "../metricsApi"; +import { + DemographicsByCategoryRecord, + recordIsTotalByDimension, +} from "../metricsApi"; import Metric from "./Metric"; export default class DemographicsByCategoryMetric extends Metric< DemographicsByCategoryRecord -> {} +> { + get records(): DemographicsByCategoryRecord[] | undefined { + let recordsToReturn = this.getOrFetchRecords(); + if (!recordsToReturn) return undefined; + + recordsToReturn = recordsToReturn.filter( + recordIsTotalByDimension(this.demographicView) + ); + return recordsToReturn; + } +} diff --git a/spotlight-client/src/contentModels/HistoricalPopulationBreakdownMetric.ts b/spotlight-client/src/contentModels/HistoricalPopulationBreakdownMetric.ts index 1c643b7f..c25a8fc9 100644 --- a/spotlight-client/src/contentModels/HistoricalPopulationBreakdownMetric.ts +++ b/spotlight-client/src/contentModels/HistoricalPopulationBreakdownMetric.ts @@ -15,9 +15,22 @@ // along with this program. If not, see . // ============================================================================= -import { HistoricalPopulationBreakdownRecord } from "../metricsApi"; +import { + HistoricalPopulationBreakdownRecord, + recordIsTotalByDimension, +} from "../metricsApi"; import Metric from "./Metric"; export default class HistoricalPopulationBreakdownMetric extends Metric< HistoricalPopulationBreakdownRecord -> {} +> { + get records(): HistoricalPopulationBreakdownRecord[] | undefined { + let recordsToReturn = this.getOrFetchRecords(); + if (!recordsToReturn) return undefined; + + recordsToReturn = recordsToReturn.filter( + recordIsTotalByDimension(this.demographicView) + ); + return recordsToReturn; + } +} diff --git a/spotlight-client/src/contentModels/Metric.ts b/spotlight-client/src/contentModels/Metric.ts index 2a2417ff..99303a70 100644 --- a/spotlight-client/src/contentModels/Metric.ts +++ b/spotlight-client/src/contentModels/Metric.ts @@ -29,8 +29,6 @@ import { DemographicFields, DemographicView, LocalityFields, - recordIsTotalByDimension, - recordMatchesLocality, } from "../metricsApi"; import { MetricRecord, CollectionMap } from "./types"; @@ -149,25 +147,13 @@ export default abstract class Metric { * Returns fetched, transformed, and (optionally) filtered data for this metric. * Will automatically initiate a fetch if necessary. */ - get records(): RecordFormat[] | undefined { - let recordsToReturn = this.allRecords; - if (recordsToReturn) { - if (this.localityId) { - recordsToReturn = recordsToReturn.filter( - // TS can't seem to resolve this conditional type even after the conditional - recordMatchesLocality(this.localityId as string) - ); - } - - if (this.demographicView) { - recordsToReturn = recordsToReturn.filter( - // TS can't seem to resolve this conditional type even after the conditional - recordIsTotalByDimension(this.demographicView as DemographicView) - ); - } - return recordsToReturn; - } + protected getOrFetchRecords(): RecordFormat[] | undefined { + if (this.allRecords) return this.allRecords; if (!this.isLoading || !this.error) this.fetch(); return undefined; } + + get records(): RecordFormat[] | undefined { + return this.getOrFetchRecords(); + } } diff --git a/spotlight-client/src/contentModels/RecidivismRateMetric.ts b/spotlight-client/src/contentModels/RecidivismRateMetric.ts index 4e8792f1..61d8292a 100644 --- a/spotlight-client/src/contentModels/RecidivismRateMetric.ts +++ b/spotlight-client/src/contentModels/RecidivismRateMetric.ts @@ -15,9 +15,17 @@ // along with this program. If not, see . // ============================================================================= -import { RecidivismRateRecord } from "../metricsApi"; +import { RecidivismRateRecord, recordIsTotalByDimension } from "../metricsApi"; import Metric from "./Metric"; -export default class RecidivismRateMetric extends Metric< - RecidivismRateRecord -> {} +export default class RecidivismRateMetric extends Metric { + get records(): RecidivismRateRecord[] | undefined { + let recordsToReturn = this.getOrFetchRecords(); + if (!recordsToReturn) return undefined; + + recordsToReturn = recordsToReturn.filter( + recordIsTotalByDimension(this.demographicView) + ); + return recordsToReturn; + } +} diff --git a/spotlight-client/src/contentModels/SentenceTypeByLocationMetric.ts b/spotlight-client/src/contentModels/SentenceTypeByLocationMetric.ts index 7cc853bc..9f28fd79 100644 --- a/spotlight-client/src/contentModels/SentenceTypeByLocationMetric.ts +++ b/spotlight-client/src/contentModels/SentenceTypeByLocationMetric.ts @@ -15,9 +15,27 @@ // along with this program. If not, see . // ============================================================================= -import { SentenceTypeByLocationRecord } from "../metricsApi"; +import { + recordIsTotalByDimension, + recordMatchesLocality, + SentenceTypeByLocationRecord, +} from "../metricsApi"; import Metric from "./Metric"; export default class SentenceTypeByLocationMetric extends Metric< SentenceTypeByLocationRecord -> {} +> { + get records(): SentenceTypeByLocationRecord[] | undefined { + let recordsToReturn = this.getOrFetchRecords(); + if (!recordsToReturn) return undefined; + + recordsToReturn = recordsToReturn.filter( + recordMatchesLocality(this.localityId) + ); + + recordsToReturn = recordsToReturn.filter( + recordIsTotalByDimension(this.demographicView) + ); + return recordsToReturn; + } +} diff --git a/spotlight-client/src/contentModels/SupervisionSuccessRateDemographicsMetric.ts b/spotlight-client/src/contentModels/SupervisionSuccessRateDemographicsMetric.ts index 40d1da9a..df44a41b 100644 --- a/spotlight-client/src/contentModels/SupervisionSuccessRateDemographicsMetric.ts +++ b/spotlight-client/src/contentModels/SupervisionSuccessRateDemographicsMetric.ts @@ -15,9 +15,27 @@ // along with this program. If not, see . // ============================================================================= -import { SupervisionSuccessRateDemographicsRecord } from "../metricsApi"; +import { + recordIsTotalByDimension, + recordMatchesLocality, + SupervisionSuccessRateDemographicsRecord, +} from "../metricsApi"; import Metric from "./Metric"; export default class SupervisionSuccessRateDemographicsMetric extends Metric< SupervisionSuccessRateDemographicsRecord -> {} +> { + get records(): SupervisionSuccessRateDemographicsRecord[] | undefined { + let recordsToReturn = this.getOrFetchRecords(); + if (!recordsToReturn) return undefined; + + recordsToReturn = recordsToReturn.filter( + recordMatchesLocality(this.localityId) + ); + + recordsToReturn = recordsToReturn.filter( + recordIsTotalByDimension(this.demographicView) + ); + return recordsToReturn; + } +} diff --git a/spotlight-client/src/contentModels/SupervisionSuccessRateMonthlyMetric.ts b/spotlight-client/src/contentModels/SupervisionSuccessRateMonthlyMetric.ts index a6ea23d6..ac60c267 100644 --- a/spotlight-client/src/contentModels/SupervisionSuccessRateMonthlyMetric.ts +++ b/spotlight-client/src/contentModels/SupervisionSuccessRateMonthlyMetric.ts @@ -15,9 +15,23 @@ // along with this program. If not, see . // ============================================================================= -import { SupervisionSuccessRateMonthlyRecord } from "../metricsApi"; +import { + recordMatchesLocality, + SupervisionSuccessRateMonthlyRecord, +} from "../metricsApi"; import Metric from "./Metric"; export default class SupervisionSuccessRateMonthlyMetric extends Metric< SupervisionSuccessRateMonthlyRecord -> {} +> { + get records(): SupervisionSuccessRateMonthlyRecord[] | undefined { + let recordsToReturn = this.getOrFetchRecords(); + if (!recordsToReturn) return undefined; + + recordsToReturn = recordsToReturn.filter( + recordMatchesLocality(this.localityId) + ); + + return recordsToReturn; + } +} diff --git a/spotlight-client/src/metricsApi/utils.ts b/spotlight-client/src/metricsApi/utils.ts index ca49d1b7..b2e3d8c4 100644 --- a/spotlight-client/src/metricsApi/utils.ts +++ b/spotlight-client/src/metricsApi/utils.ts @@ -102,8 +102,7 @@ export const NOFILTER_KEY: NoFilterIdentifier = "nofilter"; */ export function recordIsTotalByDimension( demographicView: DemographicView - // eslint-disable-next-line @typescript-eslint/no-explicit-any -): (record: Record) => boolean { +): (record: DemographicFields) => boolean { if (demographicView === NOFILTER_KEY) return () => true; const keysEnum = { ...DIMENSION_DATA_KEYS }; @@ -135,8 +134,7 @@ export function recordIsTotalByDimension( */ export function recordMatchesLocality( locality: string - // eslint-disable-next-line @typescript-eslint/no-explicit-any -): (record: Record) => boolean { +): (record: LocalityFields) => boolean { if (locality === NOFILTER_KEY) return () => true; return (record) => record.locality === locality; }