Skip to content

Commit

Permalink
Pass in rootStore to fetchMetrics in order to get token in fetch.
Browse files Browse the repository at this point in the history
  • Loading branch information
terryttsai committed May 5, 2022
1 parent 823b079 commit 552814e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
5 changes: 4 additions & 1 deletion spotlight-client/src/DataStore/TenantStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export default class TenantStore {
try {
this.tenants.set(
this.currentTenantId,
createTenant({ tenantId: this.currentTenantId })
createTenant({
tenantId: this.currentTenantId,
rootStore: this.rootStore,
})
);
} catch (error) {
if (!error.message.includes(ERROR_MESSAGES.disabledTenant)) {
Expand Down
7 changes: 7 additions & 0 deletions spotlight-client/src/contentModels/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
MetricTypeId,
TenantId,
} from "../contentApi/types";
import RootStore from "../DataStore/RootStore";
import {
createDemographicCategories,
DemographicCategories,
Expand Down Expand Up @@ -89,6 +90,7 @@ export type BaseMetricConstructorOptions<RecordFormat extends MetricRecord> = {
localityLabels: RecordFormat extends LocalityFields
? LocalityLabels
: undefined;
rootStore?: RootStore;
};

/**
Expand Down Expand Up @@ -125,6 +127,8 @@ export default abstract class Metric<RecordFormat extends MetricRecord>

error?: Error;

rootStore?: RootStore;

// filter properties
private readonly demographicCategories: DemographicCategories;

Expand Down Expand Up @@ -152,6 +156,7 @@ export default abstract class Metric<RecordFormat extends MetricRecord>
defaultDemographicView,
defaultLocalityId,
localityLabels,
rootStore,
}: BaseMetricConstructorOptions<RecordFormat>) {
makeObservable<Metric<RecordFormat>, "allRecords">(this, {
allRecords: observable.ref,
Expand All @@ -174,6 +179,7 @@ export default abstract class Metric<RecordFormat extends MetricRecord>
this.tenantId = tenantId;
this.sourceFileName = sourceFileName;
this.dataTransformer = dataTransformer;
this.rootStore = rootStore;

// initialize filters
this.demographicCategories = createDemographicCategories(demographicFilter);
Expand All @@ -191,6 +197,7 @@ export default abstract class Metric<RecordFormat extends MetricRecord>
sourceFileName: this.sourceFileName,
tenantId: this.tenantId,
transformFn: this.dataTransformer,
rootStore: this.rootStore,
});
return records;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
TenantId,
DemographicCategoryFilter,
} from "../contentApi/types";
import RootStore from "../DataStore/RootStore";
import {
createDemographicCategories,
RaceIdentifier,
Expand Down Expand Up @@ -144,6 +145,7 @@ type ConstructorOpts = {
defaultCategory?: RaceIdentifier;
content: RacialDisparitiesNarrativeContent;
categoryFilter?: DemographicCategoryFilter["raceOrEthnicity"];
rootStore?: RootStore;
};

/**
Expand Down Expand Up @@ -183,6 +185,8 @@ export default class RacialDisparitiesNarrative implements Hydratable {

error?: Error;

rootStore?: RootStore;

// filters
readonly allCategories: RaceOrEthnicityCategory[];

Expand All @@ -201,6 +205,7 @@ export default class RacialDisparitiesNarrative implements Hydratable {
defaultCategory,
content,
categoryFilter,
rootStore,
}: ConstructorOpts) {
this.tenantId = tenantId;
this.selectedCategory = defaultCategory || "BLACK";
Expand All @@ -215,6 +220,7 @@ export default class RacialDisparitiesNarrative implements Hydratable {
this.allCategories = createDemographicCategories({
raceOrEthnicity: categoryFilter,
}).raceOrEthnicity;
this.rootStore = rootStore;

makeAutoObservable<RacialDisparitiesNarrative, "records">(this, {
records: observable.ref,
Expand All @@ -233,6 +239,7 @@ export default class RacialDisparitiesNarrative implements Hydratable {
sourceFileName: "racial_disparities",
tenantId: this.tenantId,
transformFn: createRacialDisparitiesRecords,
rootStore: this.rootStore,
});
runInAction(() => {
this.records = fetchedData.reduce((mapping, record) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export default class SupervisionSuccessRateMetric extends Metric<SupervisionSucc
sourceFileName: this.demographicSourceFileName,
tenantId: this.tenantId,
transformFn: this.demographicDataTransformer,
rootStore: this.rootStore,
}),
]);

Expand Down
14 changes: 11 additions & 3 deletions spotlight-client/src/contentModels/Tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import retrieveContent from "../contentApi/retrieveContent";
import { SystemNarrativeTypeIdList, TenantId } from "../contentApi/types";
import RootStore from "../DataStore/RootStore";
import createMetricMapping from "./createMetricMapping";
import RacialDisparitiesNarrative from "./RacialDisparitiesNarrative";
import { createSystemNarrative } from "./SystemNarrative";
Expand Down Expand Up @@ -85,18 +86,21 @@ export default class Tenant {

type TenantFactoryOptions = {
tenantId: TenantId;
rootStore?: RootStore;
};

function getMetricsForTenant(
allTenantContent: ReturnType<typeof retrieveContent>,
tenantId: TenantId
tenantId: TenantId,
rootStore?: RootStore
) {
return createMetricMapping({
localityLabelMapping: allTenantContent.localities,
metadataMapping: allTenantContent.metrics,
topologyMapping: allTenantContent.topologies,
tenantId,
demographicFilter: allTenantContent.demographicCategories,
rootStore,
});
}

Expand All @@ -123,17 +127,21 @@ function getSystemNarrativesForTenant({
/**
* Factory function for creating an instance of the `Tenant` specified by `tenantId`.
*/
export function createTenant({ tenantId }: TenantFactoryOptions): Tenant {
export function createTenant({
tenantId,
rootStore,
}: TenantFactoryOptions): Tenant {
const allTenantContent = retrieveContent({ tenantId });

const metrics = getMetricsForTenant(allTenantContent, tenantId);
const metrics = getMetricsForTenant(allTenantContent, tenantId, rootStore);

const racialDisparitiesNarrative =
allTenantContent.racialDisparitiesNarrative &&
RacialDisparitiesNarrative.build({
tenantId,
content: allTenantContent.racialDisparitiesNarrative,
categoryFilter: allTenantContent.demographicCategories?.raceOrEthnicity,
rootStore,
});

return new Tenant({
Expand Down
24 changes: 24 additions & 0 deletions spotlight-client/src/contentModels/createMetricMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ import SupervisionSuccessRateMetric from "./SupervisionSuccessRateMetric";
import { ERROR_MESSAGES } from "../constants";
import { NOFILTER_KEY, TOTAL_KEY } from "../demographics";
import { colors } from "../UiLibrary";
import RootStore from "../DataStore/RootStore";

type MetricMappingFactoryOptions = {
localityLabelMapping?: TenantContent["localities"];
metadataMapping: TenantContent["metrics"];
topologyMapping?: TenantContent["topologies"];
tenantId: TenantId;
demographicFilter?: TenantContent["demographicCategories"];
rootStore?: RootStore;
};
/**
* Factory function for converting a mapping of content objects by metric ID
Expand All @@ -74,6 +76,7 @@ export default function createMetricMapping({
topologyMapping,
tenantId,
demographicFilter,
rootStore,
}: MetricMappingFactoryOptions): MetricMapping {
const metricMapping: MetricMapping = new Map();

Expand Down Expand Up @@ -113,6 +116,7 @@ export default function createMetricMapping({
localityLabels: localityLabelMapping.Sentencing,
dataTransformer: sentencePopulationCurrent,
sourceFileName: "sentence_type_by_district_by_demographics",
rootStore,
})
);
else throw new Error(totalLabelError);
Expand All @@ -134,6 +138,7 @@ export default function createMetricMapping({
localityLabels: localityLabelMapping.Sentencing,
dataTransformer: sentenceTypesCurrent,
sourceFileName: "sentence_type_by_district_by_demographics",
rootStore,
})
);
break;
Expand All @@ -155,6 +160,7 @@ export default function createMetricMapping({
dataTransformer: prisonPopulationCurrent,
sourceFileName:
"incarceration_population_by_facility_by_demographics",
rootStore,
})
);
else throw new Error(totalLabelError);
Expand All @@ -177,6 +183,7 @@ export default function createMetricMapping({
dataTransformer: prisonPopulationCurrent,
sourceFileName:
"community_corrections_population_by_facility_by_demographics",
rootStore,
})
);
else throw new Error(totalLabelError);
Expand All @@ -198,6 +205,7 @@ export default function createMetricMapping({
dataTransformer: probationPopulationCurrent,
sourceFileName:
"supervision_population_by_district_by_demographics",
rootStore,
})
);
else throw new Error(totalLabelError);
Expand All @@ -219,6 +227,7 @@ export default function createMetricMapping({
dataTransformer: parolePopulationCurrent,
sourceFileName:
"supervision_population_by_district_by_demographics",
rootStore,
})
);
else throw new Error(totalLabelError);
Expand All @@ -236,6 +245,7 @@ export default function createMetricMapping({
localityLabels: undefined,
dataTransformer: prisonPopulationHistorical,
sourceFileName: "incarceration_population_by_month_by_demographics",
rootStore,
})
);
break;
Expand All @@ -252,6 +262,7 @@ export default function createMetricMapping({
localityLabels: undefined,
dataTransformer: probationPopulationHistorical,
sourceFileName: "supervision_population_by_month_by_demographics",
rootStore,
})
);
break;
Expand All @@ -268,6 +279,7 @@ export default function createMetricMapping({
localityLabels: undefined,
dataTransformer: parolePopulationHistorical,
sourceFileName: "supervision_population_by_month_by_demographics",
rootStore,
})
);
break;
Expand All @@ -291,6 +303,7 @@ export default function createMetricMapping({
mapData: topologyMapping?.ProgramRegions,
dataTransformer: probationProgramParticipationCurrent,
sourceFileName: "active_program_participation_by_region",
rootStore,
})
);
break;
Expand All @@ -314,6 +327,7 @@ export default function createMetricMapping({
mapData: topologyMapping?.ProgramRegions,
dataTransformer: paroleProgramParticipationCurrent,
sourceFileName: "active_program_participation_by_region",
rootStore,
})
);
break;
Expand All @@ -336,6 +350,7 @@ export default function createMetricMapping({
demographicDataTransformer: probationSuccessRateDemographics,
demographicSourceFileName:
"supervision_success_by_period_by_demographics",
rootStore,
})
);
break;
Expand All @@ -358,6 +373,7 @@ export default function createMetricMapping({
demographicDataTransformer: paroleSuccessRateDemographics,
demographicSourceFileName:
"supervision_success_by_period_by_demographics",
rootStore,
})
);
break;
Expand All @@ -380,6 +396,7 @@ export default function createMetricMapping({
demographicDataTransformer: paroleTerminationRateDemographics,
demographicSourceFileName:
"supervision_terminations_by_period_by_demographics",
rootStore,
})
);
break;
Expand All @@ -397,6 +414,7 @@ export default function createMetricMapping({
dataTransformer: probationRevocationReasons,
sourceFileName:
"supervision_revocations_by_period_by_type_by_demographics",
rootStore,
})
);
break;
Expand All @@ -414,6 +432,7 @@ export default function createMetricMapping({
dataTransformer: paroleRevocationReasons,
sourceFileName:
"supervision_revocations_by_period_by_type_by_demographics",
rootStore,
})
);
break;
Expand All @@ -437,6 +456,7 @@ export default function createMetricMapping({
return prisonAdmissionReasons(rawRecords, fieldMapping);
},
sourceFileName: "incarceration_population_by_admission_reason",
rootStore,
})
);
break;
Expand All @@ -453,6 +473,7 @@ export default function createMetricMapping({
localityLabels: undefined,
dataTransformer: prisonReleaseTypes,
sourceFileName: "incarceration_releases_by_type_by_period",
rootStore,
})
);
break;
Expand All @@ -469,6 +490,7 @@ export default function createMetricMapping({
localityLabels: undefined,
dataTransformer: recidivismRateAllFollowup,
sourceFileName: "recidivism_rates_by_cohort_by_year",
rootStore,
})
);
break;
Expand All @@ -486,6 +508,7 @@ export default function createMetricMapping({
localityLabels: undefined,
dataTransformer: recidivismRateConventionalFollowup,
sourceFileName: "recidivism_rates_by_cohort_by_year",
rootStore,
})
);
break;
Expand All @@ -503,6 +526,7 @@ export default function createMetricMapping({
dataTransformer: prisonStayLengths,
sourceFileName: "incarceration_lengths_by_demographics",
color: colors.dataVizNamed.teal,
rootStore,
})
);
break;
Expand Down
Loading

0 comments on commit 552814e

Please sign in to comment.