Skip to content

Commit 568f2af

Browse files
committed
refactor: structure for place
1 parent b38eefc commit 568f2af

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

js/features/createSearch/search.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ListItemClickListener, SearchApiInterface, SearchEventCallback, SearchInterface, SearchLocationListItem, SearchUiInterface } from './searchInterface';
1+
import { ListItemClickListener, PlaceObject, SearchApiInterface, SearchCallback, SearchInterface, SearchUiInterface } from './searchInterface';
22
import { MapInterface } from '../createMap/mapInterface';
33

44
export class Search implements SearchInterface {
@@ -9,8 +9,8 @@ export class Search implements SearchInterface {
99
private searchUi: SearchUiInterface
1010
) {}
1111

12-
public addSearchListener(searchEventCallback: SearchEventCallback): this {
13-
this.apiInstance.addSearchListener(searchEventCallback);
12+
public addSearchResponseCallback(searchEventCallback: SearchCallback): this {
13+
this.apiInstance.addSearchResponseCallback(searchEventCallback);
1414
return this;
1515
}
1616

@@ -24,12 +24,12 @@ export class Search implements SearchInterface {
2424
return this;
2525
}
2626

27-
public setSearchListItems(items: SearchLocationListItem[]): this {
27+
public setSearchListItems(items: any): this {
2828
this.searchUi.setSearchListItems(items);
2929
return this;
3030
}
3131

32-
public search(question: string): Promise<any> {
32+
public search(question: string): Promise<PlaceObject[]> {
3333
return this.apiInstance.search(question);
3434
}
3535

js/features/createSearch/search/api.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { SearchCallback } from "../../../types";
2-
import { SearchApiInterface } from "../searchInterface";
1+
import { PlaceObject, SearchApiInterface, SearchCallback } from "../searchInterface";
32

43
export class SearchApi implements SearchApiInterface {
54
private apiUrl: URL|null = null;
65
private searchParam: string|null = null;
76
private searchListeners: SearchCallback[] = [];
87
private searchResults: any = {};
98

10-
public search(question: string): Promise<any> {
9+
public search(question: string): Promise<PlaceObject[]> {
1110
if (this.apiUrl === null || this.searchParam === null) {
1211
throw new Error("API URL and search parameter must be set before searching.");
1312
}
@@ -42,12 +41,12 @@ export class SearchApi implements SearchApiInterface {
4241
})
4342
.catch(error => {
4443
console.error('Error:', error);
45-
return null;
44+
return Promise.resolve([]);
4645
});
4746
}
4847

4948
// Adding listeners gives you a chance to change the response value
50-
public addSearchListener(callback: SearchCallback): this {
49+
public addSearchResponseCallback(callback: SearchCallback): this {
5150
this.searchListeners.push(callback);
5251
return this;
5352
}

js/features/createSearch/search/searchUi.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import L, { Map as LeafletMap} from 'leaflet';
22
import { MapInterface } from '../../createMap/mapInterface';
3-
import { ListItemClickListener, SearchApiInterface, SearchLocationListItem, SearchUiInterface } from '../searchInterface';
3+
import { ListItemClickListener, PlaceObject, SearchApiInterface, SearchUiInterface } from '../searchInterface';
44
import { SearchOptions } from '../createSearchInterface';
55

66
export class SearchUi implements SearchUiInterface {
@@ -15,8 +15,8 @@ export class SearchUi implements SearchUiInterface {
1515
private listenForInput(): void {
1616
const debouncedSearch = this.debounce((value: string) => {
1717
this.searchApi.search(value)
18-
.then((data: any) => {
19-
console.log(data);
18+
.then((data: PlaceObject[]) => {
19+
this.setSearchListItems(data);
2020
});
2121
}, 500);
2222

@@ -32,7 +32,7 @@ export class SearchUi implements SearchUiInterface {
3232
return this;
3333
}
3434

35-
public setSearchListItems(items: SearchLocationListItem[]): this {
35+
public setSearchListItems(items: PlaceObject[]): this {
3636
const listContainer = this.getList();
3737
if (!listContainer) {
3838
throw new Error('List container not found');
@@ -44,13 +44,13 @@ export class SearchUi implements SearchUiInterface {
4444
listContainer.innerHTML = '';
4545
}
4646

47-
items.forEach(item => {
47+
items.forEach((item: PlaceObject) => {
4848
const listItem = this.createListItem(item);
4949

5050
listItem.addEventListener('click', () => {
5151
this.itemClickListeners.forEach(listener => listener(item));
5252
if (this.input) {
53-
this.input.value = item.title;
53+
this.input.value = item.address as string;
5454
this.input.focus();
5555
}
5656

@@ -63,9 +63,10 @@ export class SearchUi implements SearchUiInterface {
6363
return this;
6464
}
6565

66-
private createListItem(item: SearchLocationListItem): HTMLLIElement {
66+
private createListItem(item: PlaceObject): HTMLLIElement {
67+
console.log(item);
6768
const li = document.createElement('li');
68-
li.innerHTML = `<span>${item.title}</span>`;
69+
li.innerHTML = `<span>${item.address}</span>`;
6970

7071
return li;
7172
}

0 commit comments

Comments
 (0)