Skip to content

Commit

Permalink
make selections async
Browse files Browse the repository at this point in the history
  • Loading branch information
lastminutediorama committed Dec 19, 2024
1 parent a3b39f3 commit f9490b7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
30 changes: 11 additions & 19 deletions src/interface/src/app/treatments/direct-impacts.state.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BehaviorSubject, combineLatest, map, of } from 'rxjs';
import { BehaviorSubject, combineLatest, map } from 'rxjs';
import {
DEFAULT_SLOT,
ImpactsMetric,
Expand Down Expand Up @@ -37,8 +37,8 @@ export class DirectImpactsStateService {

public activeMetric$ = this._activeMetric$.asObservable();

//TODO: make this async? a behavior subject?
private selectedMetrics: string[] = [];
private _selectedMetrics$ = new BehaviorSubject<string[]>([]);
public selectedMetrics$ = this._selectedMetrics$.asObservable();

private _filteredTreatmentTypes$ = new BehaviorSubject<PrescriptionAction[]>(
[]
Expand All @@ -52,32 +52,28 @@ export class DirectImpactsStateService {
);
public activeTreatmentPlan$ = this._activeTreatmentPlan$.asObservable();

private _selectedProjectAreaForChanges$ =
private _selectedProjectArea$ =
new BehaviorSubject<ImpactsProjectArea | null>(null);
public selectedProjectAreaForChanges$ =
this._selectedProjectAreaForChanges$.asObservable();
public selectedProjectArea$ = this._selectedProjectArea$.asObservable();

private _changeOverTimeData$ = new BehaviorSubject<
ChangeOverTimeChartItem[][]
>([[]]);
public changeOverTimeData$ = this._changeOverTimeData$.asObservable();

// todo: placeholder to fill once we have project area filter
projectArea$ = of('All Project Areas');

private _showTreatmentPrescription$ = new BehaviorSubject(false);
public showTreatmentPrescription$ =
this._showTreatmentPrescription$.asObservable();

mapPanelTitle$ = combineLatest([
this.activeMetric$,
this.projectArea$,
this._selectedProjectArea$,
this.showTreatmentPrescription$,
]).pipe(
map(([activeMetric, pa, showTreatment]) =>
showTreatment
? 'Applied Treatment Prescription'
: `${activeMetric.metric.label} for ${pa}`
: `${activeMetric.metric.label} for ${pa?.project_area_name ?? 'All Project Areas'}`
)
);

Expand Down Expand Up @@ -111,11 +107,11 @@ export class DirectImpactsStateService {
if (!treatmentPlanId) {
return;
}
const projId = this._selectedProjectAreaForChanges$.value?.project_area_id;
const projId = this._selectedProjectArea$.value?.project_area_id;
this.treatmentsService
.getTreatmentImpactCharts(
treatmentPlanId,
this.selectedMetrics,
this._selectedMetrics$.value,
projId ?? null
)
.subscribe({
Expand All @@ -125,19 +121,15 @@ export class DirectImpactsStateService {
);
this._changeOverTimeData$.next(chartData);
},
error: (error) => {
//TODO: replace with snackbar or similar
},
});
}

setSelectedMetrics(selections: string[]) {
//TODO: make this a behavior subject??
this.selectedMetrics = selections;
this._selectedMetrics$.next(selections);
}

setProjectAreaForChanges(projectArea: ImpactsProjectArea | null) {
this._selectedProjectAreaForChanges$.next(projectArea);
this._selectedProjectArea$.next(projectArea);

//then refresh the changes chart data
this.getChangesOverTimeData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class DirectImpactsComponent implements OnInit, OnDestroy {
activeStand$ = this.directImpactsStateService.activeStand$;

selectedChartProjectArea$ =
this.directImpactsStateService.selectedProjectAreaForChanges$;
this.directImpactsStateService.selectedProjectArea$;
showTreatmentPrescription = false;
changeChartButtons: PanelIconButton[] = [
{ icon: 'open_in_full', actionName: 'expand' },
Expand Down Expand Up @@ -187,11 +187,6 @@ export class DirectImpactsComponent implements OnInit, OnDestroy {
Chart.unregister(ChartDataLabels);
}

handleChangedMetrics(data: string[]) {
this.directImpactsStateService.setSelectedMetrics(data);
this.directImpactsStateService.getChangesOverTimeData();
}

setChartProjectArea(e: ImpactsProjectArea) {
this.directImpactsStateService.setProjectAreaForChanges(e);
}
Expand All @@ -217,4 +212,9 @@ export class DirectImpactsComponent implements OnInit, OnDestroy {
saveShowTreatmentPrescription(value: MatSlideToggleChange) {
this.directImpactsStateService.setShowTreatmentPrescription(value.checked);
}

handleChangedMetrics(data: string[]) {
this.directImpactsStateService.setSelectedMetrics(data);
this.directImpactsStateService.getChangesOverTimeData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class ExpandedChangeOverTimeChartComponent {
public dialogRef: MatDialogRef<ExpandedChangeOverTimeChartComponent>
) {}
selectedChartProjectArea$ =
this.directImpactsStateService.selectedProjectAreaForChanges$;
this.directImpactsStateService.selectedProjectArea$;

availableProjectAreas$ = this.treatmentsState.summary$.pipe(
map((summary) => {
Expand Down

0 comments on commit f9490b7

Please sign in to comment.