@ -1,15 +1,13 @@
import { COMMA , ENTER } from "@angular/cdk/keycodes" ;
import { COMMA , ENTER } from "@angular/cdk/keycodes" ;
import { Component , OnInit , ElementRef , ViewChild } from "@angular/core" ;
import { Component , OnInit , ElementRef , ViewChild } from "@angular/core" ;
import { MatChipInputEvent } from "@angular/material/chips" ;
import { MatAutocomplete } from "@angular/material/autocomplete" ;
import { MatAutocompleteSelectedEvent , MatAutocomplete } from "@angular/material/autocomplete" ;
import { empty , of , Observable } from "rxjs" ;
import { ITheMovieDbSettings , IMovieDbKeyword } from "../../interfaces" ;
import { ITheMovieDbSettings , IMovieDbKeyword } from "../../interfaces" ;
import { NotificationService } from "../../services" ;
import { NotificationService } from "../../services" ;
import { SettingsService } from "../../services" ;
import { SettingsService } from "../../services" ;
import { TheMovieDbService } from "../../services" ;
import { TheMovieDbService } from "../../services" ;
import { Form Control, Form Builder, FormGroup } from "@angular/forms" ;
import { Form Builder, FormGroup } from "@angular/forms" ;
import { startWith, map , debounceTime, tap, switchMap, finalize } from "rxjs/operators" ;
import { debounceTime, switchMap } from "rxjs/operators" ;
interface IKeywordTag {
interface IKeywordTag {
id : number ;
id : number ;
@ -28,9 +26,6 @@ export class TheMovieDbComponent implements OnInit {
public tagForm : FormGroup ;
public tagForm : FormGroup ;
public filteredTags : IMovieDbKeyword [ ] ;
public filteredTags : IMovieDbKeyword [ ] ;
@ViewChild ( 'fruitInput' ) public fruitInput : ElementRef < HTMLInputElement > ;
@ViewChild ( 'fruitInput' ) public fruitInput : ElementRef < HTMLInputElement > ;
@ViewChild ( 'auto' ) public matAutocomplete : MatAutocomplete ;
private readonly separatorKeysCodes : number [ ] = [ ENTER , COMMA ] ;
constructor ( private settingsService : SettingsService ,
constructor ( private settingsService : SettingsService ,
private notificationService : NotificationService ,
private notificationService : NotificationService ,
@ -50,6 +45,13 @@ export class TheMovieDbComponent implements OnInit {
initial : true ,
initial : true ,
} ) )
} ) )
: [ ] ;
: [ ] ;
this . excludedKeywords . forEach ( key = > {
this . tmdbService . getKeyword ( key . id ) . subscribe ( keyResult = > {
this . excludedKeywords . filter ( ( val , idx ) = > {
val . name = keyResult . name ;
} )
} ) ;
} ) ;
} ) ;
} ) ;
this . tagForm
this . tagForm
@ -64,61 +66,8 @@ export class TheMovieDbComponent implements OnInit {
)
)
. subscribe ( ( r ) = > ( this . filteredTags = r ) ) ;
. subscribe ( ( r ) = > ( this . filteredTags = r ) ) ;
// this.tagForm.controls.input.valueChanges
// .pipe(
// debounceTime(500),
// switchMap(value => this.tmdbService.getKeywords(value))
// )
// .subscribe((data: IMovieDbKeyword[]) => {
// this.filteredTags = data;
// });
}
public async selected ( event : MatAutocompleteSelectedEvent ) {
const keywordId = await this . tmdbService . getKeyword ( + event . option . value ) . toPromise ( ) ;
this . excludedKeywords . push ( { id : keywordId.id , name : keywordId.name , initial : false } ) ;
this . fruitInput . nativeElement . value = '' ;
this . tagForm . controls . input . setValue ( null ) ;
}
public autocompleteKeyword = ( text : string ) = > this . tmdbService . getKeywords ( text ) ;
public onAddingKeyword = ( tag : string | IKeywordTag ) = > {
if ( typeof tag === "string" ) {
const id = Number ( tag ) ;
return isNaN ( id ) ? empty ( ) : this . tmdbService . getKeyword ( id ) ;
} else {
return of ( tag ) ;
}
}
public onKeywordSelect = ( keyword : IKeywordTag ) = > {
if ( keyword . initial ) {
this . tmdbService . getKeyword ( keyword . id )
. subscribe ( k = > {
keyword . name = k . name ;
keyword . initial = false ;
} ) ;
}
}
}
public async add ( event : MatChipInputEvent ) {
const input = event . input ;
const value = event . value ;
// Add our fruit
if ( ( value || '' ) . trim ( ) ) {
const keyword = await this . tmdbService . getKeywords ( value ) . toPromise ( ) ;
this . excludedKeywords . push ( { id : keyword [ 0 ] . id , name : keyword [ 0 ] . name , initial : false } ) ;
}
// Reset the input value
if ( input ) {
input . value = '' ;
}
this . tagForm . controls . input . setValue ( null ) ;
}
public remove ( tag : IKeywordTag ) : void {
public remove ( tag : IKeywordTag ) : void {
const index = this . excludedKeywords . indexOf ( tag ) ;
const index = this . excludedKeywords . indexOf ( tag ) ;