-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create MapView service AB#23655
- Loading branch information
Showing
5 changed files
with
132 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
interfaces/IBF-dashboard/src/app/services/map-view.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MapViewService } from './map-view.service'; | ||
|
||
describe('MapViewService', () => { | ||
let service: MapViewService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(MapViewService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
98 changes: 98 additions & 0 deletions
98
interfaces/IBF-dashboard/src/app/services/map-view.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BehaviorSubject, Observable } from 'rxjs'; | ||
import { PlaceCode } from '../models/place-code.model'; | ||
import { EventState } from '../types/event-state'; | ||
import { MapView } from '../types/map-view'; | ||
import { EventService } from './event.service'; | ||
import { PlaceCodeService } from './place-code.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class MapViewService { | ||
private mapViewSubject = new BehaviorSubject<MapView>(null); | ||
private mapView: MapView; | ||
|
||
private eventState: EventState; | ||
private placeCode: PlaceCode; | ||
private placeCodeHover: PlaceCode; | ||
|
||
constructor( | ||
private eventService: EventService, | ||
private placeCodeService: PlaceCodeService, | ||
) { | ||
this.eventService | ||
.getInitialEventStateSubscription() | ||
.subscribe(this.onEventStateChange); | ||
this.eventService | ||
.getManualEventStateSubscription() | ||
.subscribe(this.onEventStateChange); | ||
this.placeCodeService | ||
.getPlaceCodeSubscription() | ||
.subscribe(this.onPlacecodeChange); | ||
this.placeCodeService | ||
.getPlaceCodeHoverSubscription() | ||
.subscribe(this.onPlacecodeHoverChange); | ||
} | ||
|
||
private setMapView(view: MapView) { | ||
this.mapView = view; | ||
this.mapViewSubject.next(this.mapView); | ||
} | ||
|
||
private updateMapView() { | ||
if (!this.eventState?.event && this.placeCodeHover) { | ||
this.setMapView(MapView.adminArea); | ||
return; | ||
} | ||
|
||
if (!this.eventState || !this.eventState.event) { | ||
this.setMapView(MapView.national); | ||
return; | ||
} | ||
|
||
if (this.eventState.event && !this.placeCode && !this.placeCodeHover) { | ||
this.eventHasName() | ||
? this.setMapView(MapView.event) | ||
: this.setMapView(MapView.national); | ||
return; | ||
} | ||
|
||
if (this.placeCode || this.placeCodeHover) { | ||
this.setMapView(MapView.adminArea); | ||
return; | ||
} | ||
|
||
this.setMapView(MapView.national); | ||
} | ||
|
||
public getMapViewSubscription(): Observable<MapView> { | ||
return this.mapViewSubject.asObservable(); | ||
} | ||
|
||
private onEventStateChange = (eventState: EventState) => { | ||
this.eventState = eventState; | ||
this.updateMapView(); | ||
}; | ||
|
||
private onPlacecodeChange = (pc: PlaceCode) => { | ||
this.placeCode = pc; | ||
this.updateMapView(); | ||
}; | ||
private onPlacecodeHoverChange = (pc: PlaceCode) => { | ||
this.placeCodeHover = pc; | ||
this.updateMapView(); | ||
}; | ||
|
||
private eventHasName(): boolean { | ||
if ( | ||
!this.eventState || | ||
!this.eventState.event || | ||
!this.eventState.event.eventName | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum MapView { | ||
national = 'national', | ||
event = 'event', | ||
adminArea = 'admin-area', | ||
} |