Skip to content

Commit

Permalink
make mutable model properties observable and others readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
macfarlandian committed Dec 9, 2020
1 parent c4eea75 commit d2f36be
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
6 changes: 3 additions & 3 deletions spotlight-client/src/contentModels/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ type InitOptions = {
* needed for assembling known `Collection` kinds.
*/
export default class Collection {
description: string;
readonly description: string;

name: string;
readonly name: string;

metrics: MetricMapping;
readonly metrics: MetricMapping;

constructor({ name, description, metrics }: InitOptions) {
this.name = name;
Expand Down
35 changes: 23 additions & 12 deletions spotlight-client/src/contentModels/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// =============================================================================

import assertNever from "assert-never";
import { makeAutoObservable, runInAction } from "mobx";
import { MetricTypeIdList, TenantContent, TenantId } from "../contentApi/types";
import {
DemographicsByCategoryRecord,
Expand Down Expand Up @@ -70,23 +71,23 @@ type InitOptions<RecordFormat> = {
*/
export default class Metric<RecordFormat extends AnyRecord> {
// metadata properties
description: string;
readonly description: string;

methodology: string;
readonly methodology: string;

name: string;
readonly name: string;

// relationships
collections: CollectionMap = new Map();

// we don't really need the entire Tenant object,
// only the ID for use in the API request
tenantId: TenantId;
readonly tenantId: TenantId;

// data properties
private dataTransformer: DataTransformer<RecordFormat>;
private readonly dataTransformer: DataTransformer<RecordFormat>;

private sourceFileName: string;
private readonly sourceFileName: string;

isLoading?: boolean;

Expand All @@ -102,6 +103,14 @@ export default class Metric<RecordFormat extends AnyRecord> {
dataTransformer,
sourceFileName,
}: InitOptions<RecordFormat>) {
makeAutoObservable(this, {
// readonly properties do not need to be observed
description: false,
methodology: false,
name: false,
tenantId: false,
});

// initialize metadata
this.name = name;
this.description = description;
Expand All @@ -119,13 +128,15 @@ export default class Metric<RecordFormat extends AnyRecord> {
metricNames: [this.sourceFileName],
tenantId: this.tenantId,
});
if (apiResponse) {
const metricFileData = apiResponse[this.sourceFileName];
if (metricFileData) {
this.allRecords = this.dataTransformer(metricFileData);
runInAction(() => {
if (apiResponse) {
const metricFileData = apiResponse[this.sourceFileName];
if (metricFileData) {
this.allRecords = this.dataTransformer(metricFileData);
}
this.isLoading = false;
}
this.isLoading = false;
}
});
}

get records(): RecordFormat[] | undefined {
Expand Down
8 changes: 4 additions & 4 deletions spotlight-client/src/contentModels/Tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import { createMetricMapping } from "./Metric";
import { CollectionMap, MetricMapping } from "./types";

type InitOptions = {
name: string;
description: string;
collections: CollectionMap;
metrics: MetricMapping;
readonly name: string;
readonly description: string;
readonly collections: CollectionMap;
readonly metrics: MetricMapping;
};

/**
Expand Down

0 comments on commit d2f36be

Please sign in to comment.