Skip to content

Commit

Permalink
feat(lib): Add address search sector limitation (API)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolkov committed Nov 19, 2019
1 parent 8a9f376 commit 0b87912
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 8 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ onAddressSelected(event: DadataSuggestion) {

For `ngModel` to work, you must import `FormsModule` from `@angular/forms`, or for `formControlName`, you must import `ReactiveFormsModule` from `@angular/forms`

also you may add additional options to constraint search (for example cities only):

```typescript
import { DaDataConfig } from '@kolkov/ngx-dadata';
...
config: DaDataConfig = {
apiKey: 'your_api_key',
type: DaDataType.address,
locations: [
{
region: 'Самарская',
city: 'Тольятти',
}
]
};
```

## What's included

Within the download you'll find the following directories and files. You'll see something like this:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kolkov/ngx-dadata",
"version": "0.5.0",
"version": "0.6.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down
5 changes: 5 additions & 0 deletions projects/ngx-dadata-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export class AppComponent {
configAddress: DadataConfig = {
apiKey: '2e51c5fbc1a60bd48face95951108560bf03f7d9',
type: DadataType.address,
locations: [
{
city: 'Москва',
}
]
};
configFio: DadataConfig = {
apiKey: '2e51c5fbc1a60bd48face95951108560bf03f7d9',
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-dadata/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kolkov/ngx-dadata",
"version": "0.5.0",
"version": "0.6.0",
"author": "Andrey Kolkov <a.kolkov@gmail.com>",
"repository": "https://github.com/kolkov/ngx-dadata",
"license": "MIT",
Expand Down
22 changes: 22 additions & 0 deletions projects/ngx-dadata/src/lib/dadata-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import {DadataType} from './ngx-dadata.service';

export interface Locations {
kladr_id?: string;
region_fias_id?: string;
area_fias_id?: string;
city_fias_id?: string;
settlement_fias_id?: string;
street_fias_id?: string;
region?: string;
city?: string;
street_type_full?: string;
settlement_type_full?: string;
city_district_type_full?: string;
city_type_full?: string;
area_type_full?: string;
region_type_full?: string;
country?: string;
country_iso_code?: string;
}

export interface DadataConfig {
apiKey: string;
type?: DadataType;
Expand All @@ -8,6 +27,8 @@ export interface DadataConfig {
width?: 'auto' | string;
minWidth?: '0' | string;
partyAddress?: 'city' | 'full';
locations?: Locations[];
restrict_value?: boolean;
}

export const DadataConfigDefault: DadataConfig = {
Expand All @@ -18,4 +39,5 @@ export const DadataConfigDefault: DadataConfig = {
width: 'auto',
minWidth: '0',
partyAddress: 'city',
locations: null,
};
4 changes: 3 additions & 1 deletion projects/ngx-dadata/src/lib/ngx-dadata.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class NgxDadataComponent implements OnInit, ControlValueAccessor, OnChang
@Input() type = DadataType.address;
@Input() limit = DadataConfigDefault.limit;
@Input() placeholder = '';
@Input() locations = null;

@Output() selectedSuggestion: DadataSuggestion;
@Output() selected: EventEmitter<DadataSuggestion> = new EventEmitter<DadataSuggestion>();
Expand Down Expand Up @@ -95,11 +96,12 @@ export class NgxDadataComponent implements OnInit, ControlValueAccessor, OnChang
/*this.validateFn = createDaDataValidator(this._value);
this.propagateChange(this._value);*/
this.type = this.config.type;
this.locations = this.config.locations;
this.dataService.setApiKey(this.apiKey ? this.apiKey : this.config.apiKey);
this.inputString$.pipe(
debounce(() => timer(this.config.delay ? this.config.delay : 500)),
).subscribe(x => {
this.dataService.getData(x, this.type, this.limit).subscribe((y: DadataResponse) => {
this.dataService.getData(x, this.type, this.limit, this.locations).subscribe((y: DadataResponse) => {
this.data = y.suggestions;
});
});
Expand Down
13 changes: 8 additions & 5 deletions projects/ngx-dadata/src/lib/ngx-dadata.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Injectable } from '@angular/core';
import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs';
import {DadataResponse} from './models/dadata-response';
import {Locations} from './dadata-config';

export enum DadataType {
fio = 'fio',
Expand All @@ -17,21 +18,23 @@ export enum DadataType {
export class NgxDadataService {
apiKey = '';

constructor(private http: HttpClient) { }
constructor(private http: HttpClient) {
}

setApiKey(key: string) {
this.apiKey = key;
}

getData(value: string, type: DadataType = DadataType.address, count: number = 10): Observable<DadataResponse> {
// tslint:disable-next-line:max-line-length
getData(value: string, type: DadataType = DadataType.address, count: number = 10, locations: Locations[] = null): Observable<DadataResponse> {
const httpOptions = {
headers: new HttpHeaders({
Accept: 'application/json',
'Content-Type': 'application/json',
'Content-Type': 'application/json',
Authorization: 'Token ' + this.apiKey,
})
};
const body = { query: value, count };
const body = Object.assign({query: value, count, locations});
return this.http.post<DadataResponse>('https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/' + type, body, httpOptions);
}
}

0 comments on commit 0b87912

Please sign in to comment.