Skip to content

Commit 1a67bd7

Browse files
author
Andrea Barbasso
committed
[UXP-206] try to use given coordinates when geocoding API call fails
1 parent 71ce50b commit 1a67bd7

3 files changed

Lines changed: 40 additions & 11 deletions

File tree

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/metadataGroup/googlemaps-group/googlemaps-group.component.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
77
import { TranslateLoaderMock } from '../../../../../../../../shared/mocks/translate-loader.mock';
88
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
99
import { LoadMoreService } from '../../../../../../../services/load-more.service';
10-
import { GooglemapsComponent } from '../../../../../../../../shared/googlemaps/googlemaps.component';
1110
import { NO_ERRORS_SCHEMA } from '@angular/core';
1211
import { ConfigurationDataService } from '../../../../../../../../core/data/configuration-data.service';
1312
import { createSuccessfulRemoteDataObject$ } from '../../../../../../../../shared/remote-data.utils';
13+
import { HttpClientTestingModule } from '@angular/common/http/testing';
1414

1515
describe('GooglemapsGroupComponent', () => {
1616
let component: GooglemapsGroupComponent;
@@ -80,7 +80,9 @@ describe('GooglemapsGroupComponent', () => {
8080
provide: TranslateLoader,
8181
useClass: TranslateLoaderMock
8282
}
83-
}), BrowserAnimationsModule],
83+
}),
84+
BrowserAnimationsModule,
85+
HttpClientTestingModule],
8486
providers: [
8587
{ provide: 'fieldProvider', useValue: mockField },
8688
{ provide: 'itemProvider', useValue: testItem },
@@ -91,8 +93,8 @@ describe('GooglemapsGroupComponent', () => {
9193
],
9294
schemas: [NO_ERRORS_SCHEMA],
9395
declarations: [
94-
GooglemapsGroupComponent,
95-
GooglemapsComponent]
96+
GooglemapsGroupComponent
97+
]
9698
})
9799
.compileComponents();
98100

src/app/shared/googlemaps/googlemaps.component.spec.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { By } from '@angular/platform-browser';
44
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
55
import { createSuccessfulRemoteDataObject$ } from '../remote-data.utils';
66
import { GooglemapsComponent } from './googlemaps.component';
7+
import { LocationService } from '../../core/services/location.service';
8+
import { HttpClientTestingModule } from '@angular/common/http/testing';
79

810
describe('GooglemapsComponent', () => {
911

1012
let component: GooglemapsComponent;
11-
1213
let fixture: ComponentFixture<GooglemapsComponent>;
1314

1415
const coordinates = '@41.3455,456.67';
@@ -19,17 +20,29 @@ describe('GooglemapsComponent', () => {
1920

2021
const confResponse$ = createSuccessfulRemoteDataObject$({ values: ['valid-googlemap-key'] });
2122

23+
const locationService = jasmine.createSpyObj('locationService', {
24+
findPlaceCoordinates: jasmine.createSpy('findPlaceCoordinates'),
25+
findPlaceAndDecimalCoordinates: jasmine.createSpy('findPlaceAndDecimalCoordinates'),
26+
searchByCoordinates: jasmine.createSpy('searchByCoordinates'),
27+
isValidDecimalCoordinatePair: jasmine.createSpy('isValidDecimalCoordinatePair'),
28+
isDecimalCoordinateString: jasmine.createSpy('isDecimalCoordinateString'),
29+
isSexagesimalCoordinateString: jasmine.createSpy('isSexagesimalCoordinateString'),
30+
isValidCoordinateString: jasmine.createSpy('isValidCoordinateString'),
31+
parseCoordinates: jasmine.createSpy('parseCoordinates'),
32+
});
33+
2234
beforeEach(async () => {
2335
await TestBed.configureTestingModule({
36+
imports: [ HttpClientTestingModule ],
2437
declarations: [ GooglemapsComponent ],
2538
providers: [
26-
{ provide: ConfigurationDataService, useValue: configurationDataService },
39+
{ provide: ConfigurationDataService, useValue: configurationDataService },
40+
{ provide: LocationService, useValue: locationService }
2741
]
2842
})
29-
.compileComponents();
43+
.compileComponents();
3044
});
3145

32-
3346
beforeEach(() => {
3447
fixture = TestBed.createComponent(GooglemapsComponent);
3548
component = fixture.componentInstance;

src/app/shared/googlemaps/googlemaps.component.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getFirstCompletedRemoteData } from '../../core/shared/operators';
66
import { isNotEmpty } from '../empty.util';
77
import { RemoteData } from '../../core/data/remote-data';
88
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
9+
import { LocationService } from '../../core/services/location.service';
910

1011
@Component({
1112
selector: 'ds-googlemaps',
@@ -48,6 +49,7 @@ export class GooglemapsComponent implements OnInit {
4849
private renderer: Renderer2,
4950
private configService: ConfigurationDataService,
5051
private cdr: ChangeDetectorRef,
52+
private locationService: LocationService,
5153
) {
5254
}
5355

@@ -85,14 +87,26 @@ export class GooglemapsComponent implements OnInit {
8587
* Set latitude and longitude when metadata has an address
8688
*/
8789
setLatAndLongFromAddress() {
88-
return new Promise((reslove, reject) => {
90+
return new Promise((resolve, reject) => {
8991
new google.maps.Geocoder().geocode({ 'address': this.coordinates }, (results, status) => {
9092
if (status === google.maps.GeocoderStatus.OK) {
9193
this.latitude = results[0].geometry.location.lat().toString();
9294
this.longitude = results[0].geometry.location.lng().toString();
93-
reslove(1);
95+
resolve(1);
9496
} else {
95-
reject(1);
97+
console.error('Error from Geocoder API: ', results, status);
98+
try {
99+
const coordinates = this.locationService.parseCoordinates(this.coordinates);
100+
if (coordinates) {
101+
this.latitude = coordinates.latitude.toString();
102+
this.longitude = coordinates.longitude.toString();
103+
resolve(1);
104+
} else {
105+
reject(1);
106+
}
107+
} catch (e) {
108+
reject(1);
109+
}
96110
}
97111
});
98112
});

0 commit comments

Comments
 (0)