Skip to content

Commit 8cdbb88

Browse files
committed
fix(location): correctly load locations for pre-matching map
1 parent 5b6ef1e commit 8cdbb88

File tree

7 files changed

+71
-35
lines changed

7 files changed

+71
-35
lines changed

src/app/features/location/map/map.component.spec.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { MatDialog } from "@angular/material/dialog";
1515
import { MapPopupConfig } from "../map-popup/map-popup.component";
1616
import { FontAwesomeTestingModule } from "@fortawesome/angular-fontawesome/testing";
1717
import { EMPTY, of, Subject } from "rxjs";
18+
import { GeoLocation } from "../location.datatype";
1819

1920
describe("MapComponent", () => {
2021
let component: MapComponent;
@@ -23,6 +24,11 @@ describe("MapComponent", () => {
2324
const config: MapConfig = { start: [52, 13] };
2425
let map: L.Map;
2526

27+
const TEST_LOCATION: GeoLocation = {
28+
locationString: "test address",
29+
geoLookup: { lat: 1, lon: 1, display_name: "test address" },
30+
};
31+
2632
beforeEach(async () => {
2733
mockDialog = jasmine.createSpyObj(["open"]);
2834
mockDialog.open.and.returnValue({ afterClosed: () => EMPTY } as any);
@@ -78,7 +84,7 @@ describe("MapComponent", () => {
7884
it("should create markers for entities and emit entity when marker is clicked", (done) => {
7985
Child.schema.set("address", { dataType: "location" });
8086
const child = new Child();
81-
child["address"] = { lat: 1, lon: 1 };
87+
child["address"] = TEST_LOCATION;
8288
component.entities = [child];
8389

8490
const marker = getEntityMarkers()[0];
@@ -111,8 +117,10 @@ describe("MapComponent", () => {
111117
Child.schema.set("address", { dataType: "location" });
112118
Child.schema.set("otherAddress", { dataType: "location" });
113119
const child = new Child();
114-
child["address"] = { lat: 1, lon: 1 };
115-
child["otherAddress"] = { lat: 1, lon: 2 };
120+
child["address"] = TEST_LOCATION;
121+
child["otherAddress"] = {
122+
geoLookup: { lon: 99, lat: 99, display_name: "other address" },
123+
} as GeoLocation;
116124

117125
component.entities = [child];
118126

src/app/features/location/map/map.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
LocationProperties,
2525
MapPropertiesPopupComponent,
2626
} from "./map-properties-popup/map-properties-popup.component";
27+
import { GeoResult } from "../geo.service";
2728

2829
@Component({
2930
selector: "app-map",
@@ -158,9 +159,10 @@ export class MapComponent implements AfterViewInit {
158159
.filter((entity) => !!entity)
159160
.forEach((entity) => {
160161
this.getMapProperties(entity)
161-
.filter((prop) => !!entity[prop])
162-
.forEach((prop) => {
163-
const marker = L.marker([entity[prop].lat, entity[prop].lon]);
162+
.map((prop) => entity[prop]?.geoLookup)
163+
.filter((loc: GeoResult) => !!loc)
164+
.forEach((loc: GeoResult) => {
165+
const marker = L.marker([loc.lat, loc.lon]);
164166
marker.bindTooltip(entity.toString());
165167
marker.on("click", () => this.entityClick.emit(entity));
166168
marker["entity"] = entity;

src/app/features/location/view-distance/view-distance.component.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ViewDistanceComponent } from "./view-distance.component";
33
import { Child } from "../../../child-dev-project/children/model/child";
44
import { Subject } from "rxjs";
55
import { Coordinates } from "../coordinates";
6+
import { GeoLocation } from "../location.datatype";
67

78
describe("ViewDistanceComponent", () => {
89
let component: ViewDistanceComponent;
@@ -17,7 +18,7 @@ describe("ViewDistanceComponent", () => {
1718
fixture = TestBed.createComponent(ViewDistanceComponent);
1819

1920
entity = new Child();
20-
entity["address"] = { lat: 52, lon: 13 };
21+
entity["address"] = { geoLookup: { lat: 52, lon: 13 } } as GeoLocation;
2122
compareCoordinates = new Subject();
2223
component = fixture.componentInstance;
2324
component.id = "distance";
@@ -58,7 +59,7 @@ describe("ViewDistanceComponent", () => {
5859
});
5960

6061
it("should display the shortest distance", () => {
61-
entity["otherAddress"] = { lat: 52, lon: 14 };
62+
entity["otherAddress"] = { geoLookup: { lat: 52, lon: 14 } } as GeoLocation;
6263
const c1 = { lat: 53, lon: 14 };
6364
const c2 = { lat: 52.0001, lon: 14 };
6465
compareCoordinates.next([c1, c2]);

src/app/features/location/view-distance/view-distance.component.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Observable } from "rxjs";
77
import { DynamicComponent } from "../../../core/config/dynamic-components/dynamic-component.decorator";
88
import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy";
99
import { ReadonlyFunctionComponent } from "../../../core/common-components/display-readonly-function/readonly-function.component";
10+
import { GeoLocation } from "../location.datatype";
1011

1112
/**
1213
* Config for displaying the distance between two entities
@@ -73,8 +74,10 @@ export class ViewDistanceComponent
7374
const results: number[] = [];
7475
for (const prop of this.config.coordinatesProperties) {
7576
for (const coord of compareCoordinates) {
76-
if (e[prop] && coord) {
77-
results.push(getKmDistance(e[prop], coord));
77+
if (e[prop]?.geoLookup && coord) {
78+
results.push(
79+
getKmDistance((e[prop] as GeoLocation).geoLookup, coord),
80+
);
7881
}
7982
}
8083
}

src/app/features/matching-entities/matching-entities/matching-entities.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
[sideDetails?.[0].selected, sideDetails?.[1].selected] | flattenArray
7777
"
7878
(entityClick)="entityInMapClicked($event)"
79-
[(displayedProperties)]="displayedProperties"
79+
[(displayedProperties)]="displayedLocationProperties"
8080
(displayedPropertiesChange)="updateMarkersAndDistances()"
8181
></app-map>
8282
</div>

src/app/features/matching-entities/matching-entities/matching-entities.component.spec.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { MockedTestingModule } from "../../../utils/mocked-testing.module";
2323
import { School } from "../../../child-dev-project/schools/model/school";
2424
import { DynamicComponentConfig } from "../../../core/config/dynamic-components/dynamic-component-config.interface";
2525
import { Note } from "../../../child-dev-project/notes/model/note";
26+
import { GeoLocation } from "../../location/location.datatype";
2627

2728
describe("MatchingEntitiesComponent", () => {
2829
let component: MatchingEntitiesComponent;
@@ -43,6 +44,13 @@ describe("MatchingEntitiesComponent", () => {
4344
leftSide: { entityType: "School" },
4445
};
4546

47+
const LOCATION_1: GeoLocation = {
48+
geoLookup: { lat: 52, lon: 13, display_name: "loc 1" },
49+
};
50+
const LOCATION_2: GeoLocation = {
51+
geoLookup: { lat: 52, lon: 14, display_name: "loc 1" },
52+
};
53+
4654
beforeEach(waitForAsync(() => {
4755
routeData = new Subject();
4856
mockConfigService = jasmine.createSpyObj(["getConfig"], {
@@ -288,11 +296,13 @@ describe("MatchingEntitiesComponent", () => {
288296
);
289297

290298
const compare = new Child();
291-
compare["address"] = { lat: 52, lon: 13 };
299+
compare["address"] = LOCATION_1;
292300

293301
component.sideDetails[0].selectMatch(compare);
294302

295-
expect(newCoordinates).toEqual([compare["address"]]);
303+
expect(newCoordinates).toEqual([
304+
(compare["address"] as GeoLocation)?.geoLookup,
305+
]);
296306

297307
Child.schema.delete("address");
298308
}));
@@ -334,12 +344,12 @@ describe("MatchingEntitiesComponent", () => {
334344
Child.schema.set("otherAddress", { dataType: "location" });
335345
School.schema.set("address", { dataType: "location" });
336346
const leftEntity = new Child();
337-
leftEntity["address"] = { lat: 52, lon: 14 };
338-
leftEntity["otherAddress"] = { lat: 53, lon: 14 };
347+
leftEntity["address"] = LOCATION_1;
348+
leftEntity["otherAddress"] = LOCATION_2;
339349
const rightEntity1 = new School();
340-
rightEntity1["address"] = { lat: 52, lon: 13 };
350+
rightEntity1["address"] = LOCATION_1;
341351
const rightEntity2 = new School();
342-
rightEntity2["address"] = { lat: 53, lon: 13 };
352+
rightEntity2["address"] = LOCATION_2;
343353
spyOn(TestBed.inject(EntityMapperService), "loadType").and.resolveTo([
344354
rightEntity1,
345355
rightEntity2,
@@ -368,36 +378,47 @@ describe("MatchingEntitiesComponent", () => {
368378

369379
expect(lastLeftValue).toEqual([]);
370380
expect(lastRightValue).toEqual([
371-
leftEntity["address"],
372-
leftEntity["otherAddress"],
381+
(leftEntity["address"] as GeoLocation)?.geoLookup,
382+
(leftEntity["otherAddress"] as GeoLocation)?.geoLookup,
373383
]);
374384

375385
// values should be emitted again
376386
lastLeftValue = undefined;
377387
lastRightValue = undefined;
378388
// select only one property
379-
component.displayedProperties["Child"] = ["address"];
389+
component.displayedLocationProperties["Child"] = ["address"];
380390
component.updateMarkersAndDistances();
381391

382392
expect(lastLeftValue).toEqual([]);
383-
expect(lastRightValue).toEqual([leftEntity["address"]]);
393+
expect(lastRightValue).toEqual([
394+
(leftEntity["address"] as GeoLocation)?.geoLookup,
395+
]);
384396

385397
// select an entity for right
386398
rightSide.selectMatch(rightEntity1);
387399

388-
expect(lastLeftValue).toEqual([rightEntity1["address"]]);
389-
expect(lastRightValue).toEqual([leftEntity["address"]]);
400+
expect(lastLeftValue).toEqual([
401+
(rightEntity1["address"] as GeoLocation)?.geoLookup,
402+
]);
403+
expect(lastRightValue).toEqual([
404+
(leftEntity["address"] as GeoLocation)?.geoLookup,
405+
]);
390406

391407
lastLeftValue = undefined;
392408
lastRightValue = undefined;
393409
//select both properties
394-
component.displayedProperties["Child"] = ["address", "otherAddress"];
410+
component.displayedLocationProperties["Child"] = [
411+
"address",
412+
"otherAddress",
413+
];
395414
component.updateMarkersAndDistances();
396415

397-
expect(lastLeftValue).toEqual([rightEntity1["address"]]);
416+
expect(lastLeftValue).toEqual([
417+
(rightEntity1["address"] as GeoLocation)?.geoLookup,
418+
]);
398419
expect(lastRightValue).toEqual([
399-
leftEntity["address"],
400-
leftEntity["otherAddress"],
420+
(leftEntity["address"] as GeoLocation)?.geoLookup,
421+
(leftEntity["otherAddress"] as GeoLocation)?.geoLookup,
401422
]);
402423

403424
Child.schema.delete("otherAddress");

src/app/features/matching-entities/matching-entities/matching-entities.component.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
import { RouteTarget } from "../../../route-target";
4242
import { EntitiesTableComponent } from "../../../core/common-components/entities-table/entities-table.component";
4343
import { DataFilter } from "../../../core/filter/filters/filters";
44+
import { GeoLocation } from "../../location/location.datatype";
4445

4546
export interface MatchingSide extends MatchingSideConfig {
4647
/** pass along filters from app-filter to subrecord component */
@@ -109,7 +110,7 @@ export class MatchingEntitiesComponent implements OnInit {
109110

110111
mapVisible = false;
111112
filteredMapEntities: Entity[] = [];
112-
displayedProperties: LocationProperties = {};
113+
displayedLocationProperties: LocationProperties = {};
113114

114115
constructor(
115116
private route: ActivatedRoute,
@@ -367,7 +368,7 @@ export class MatchingEntitiesComponent implements OnInit {
367368
viewComponent: "DisplayDistance",
368369
additional: {
369370
coordinatesProperties:
370-
this.displayedProperties[side.entityType.ENTITY_TYPE],
371+
this.displayedLocationProperties[side.entityType.ENTITY_TYPE],
371372
compareCoordinates: new BehaviorSubject<Coordinates[]>([]),
372373
},
373374
};
@@ -376,7 +377,7 @@ export class MatchingEntitiesComponent implements OnInit {
376377
updateMarkersAndDistances() {
377378
this.sideDetails.forEach((side) => {
378379
const sideProperties =
379-
this.displayedProperties[side.entityType.ENTITY_TYPE];
380+
this.displayedLocationProperties[side.entityType.ENTITY_TYPE];
380381
if (side.distanceColumn) {
381382
side.distanceColumn.coordinatesProperties = sideProperties;
382383
const lastValue = side.distanceColumn.compareCoordinates.value;
@@ -389,13 +390,13 @@ export class MatchingEntitiesComponent implements OnInit {
389390
}
390391

391392
private updateDistanceColumn(side: MatchingSide) {
392-
const properties =
393-
this.displayedProperties[side.highlightedSelected?.getType()];
393+
const locationProperties =
394+
this.displayedLocationProperties[side.highlightedSelected?.getType()];
394395
const otherIndex = this.sideDetails[0] === side ? 1 : 0;
395396
const distanceColumn = this.sideDetails[otherIndex].distanceColumn;
396-
if (properties && distanceColumn) {
397-
const coordinates = properties.map(
398-
(prop) => side.highlightedSelected[prop],
397+
if (locationProperties && distanceColumn) {
398+
const coordinates: Coordinates[] = locationProperties.map(
399+
(prop) => (side.highlightedSelected[prop] as GeoLocation)?.geoLookup,
399400
);
400401
distanceColumn.compareCoordinates.next(coordinates);
401402
}

0 commit comments

Comments
 (0)