diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 3a698cd..969a13f 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -1,10 +1,12 @@ import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import {HomepageComponent} from "./homepage/homepage.component"; +import {SearchComponent} from "./search/search.component"; const routes: Routes = [ - {path: '', component: HomepageComponent} + {path: '', component: HomepageComponent}, + {path: 'search', component: SearchComponent} ]; @NgModule({ diff --git a/src/app/app.module.ts b/src/app/app.module.ts index d1b7635..ad01a34 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -3,17 +3,19 @@ import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; -import { SearchHomeComponent } from './homepage/searchHome/searchHome.component'; +import { SearchBarComponent } from './homepage/searchBar/searchBar.component'; import {HttpClientModule} from "@angular/common/http"; import { HomepageComponent } from './homepage/homepage.component'; import { NavComponent } from './nav/nav.component'; +import { SearchComponent } from './search/search.component'; @NgModule({ declarations: [ AppComponent, - SearchHomeComponent, + SearchBarComponent, HomepageComponent, NavComponent, + SearchComponent, ], imports: [ HttpClientModule, diff --git a/src/app/homepage/homepage.component.html b/src/app/homepage/homepage.component.html index 46e5973..c71296c 100644 --- a/src/app/homepage/homepage.component.html +++ b/src/app/homepage/homepage.component.html @@ -3,7 +3,7 @@

Busca la musica que disfrutas!

- +
diff --git a/src/app/homepage/searchHome/searchHome.component.html b/src/app/homepage/searchBar/searchBar.component.html similarity index 69% rename from src/app/homepage/searchHome/searchHome.component.html rename to src/app/homepage/searchBar/searchBar.component.html index 200488d..210a788 100644 --- a/src/app/homepage/searchHome/searchHome.component.html +++ b/src/app/homepage/searchBar/searchBar.component.html @@ -1,9 +1,9 @@
- +
- +
@@ -22,8 +22,10 @@
- {{result.title}} cover art - {{result.title}} missing cover art + {{result.title}} cover art + {{result.title}} missing cover art

{{result.title}}

diff --git a/src/app/homepage/searchHome/searchHome.component.scss b/src/app/homepage/searchBar/searchBar.component.scss similarity index 100% rename from src/app/homepage/searchHome/searchHome.component.scss rename to src/app/homepage/searchBar/searchBar.component.scss diff --git a/src/app/homepage/searchHome/searchHome.component.spec.ts b/src/app/homepage/searchBar/searchBar.component.spec.ts similarity index 60% rename from src/app/homepage/searchHome/searchHome.component.spec.ts rename to src/app/homepage/searchBar/searchBar.component.spec.ts index feaea52..3008d26 100644 --- a/src/app/homepage/searchHome/searchHome.component.spec.ts +++ b/src/app/homepage/searchBar/searchBar.component.spec.ts @@ -1,20 +1,20 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { SearchHomeComponent } from './searchHome.component'; +import { SearchBarComponent } from './searchBar.component'; describe('SearchComponent', () => { - let component: SearchHomeComponent; - let fixture: ComponentFixture; + let component: SearchBarComponent; + let fixture: ComponentFixture; beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ SearchHomeComponent ] + declarations: [ SearchBarComponent ] }) .compileComponents(); })); beforeEach(() => { - fixture = TestBed.createComponent(SearchHomeComponent); + fixture = TestBed.createComponent(SearchBarComponent); component = fixture.componentInstance; fixture.detectChanges(); }); diff --git a/src/app/homepage/searchBar/searchBar.component.ts b/src/app/homepage/searchBar/searchBar.component.ts new file mode 100644 index 0000000..9e85a58 --- /dev/null +++ b/src/app/homepage/searchBar/searchBar.component.ts @@ -0,0 +1,66 @@ +import {Component, ElementRef, Input, OnInit, ViewChild} from '@angular/core'; +import {BehaviorSubject, forkJoin, Subject} from "rxjs"; +import {SearchService} from "../../services/search.service"; +import {Artist, Disc, Recording} from "../../models/brainz"; +import {Router} from "@angular/router"; + +@Component({ + selector: 'app-search-bar', + templateUrl: './searchBar.component.html', + styleUrls: ['./searchBar.component.scss'] +}) +export class SearchBarComponent implements OnInit { + @ViewChild("input") input: ElementRef; + showList: boolean; + + artists: Artist[] = []; + discs: Disc[] = []; + recordings: Recording[] = []; + + @Input() query$ = new BehaviorSubject(""); + @Input() button_color = "is-white"; + @Input() autocomplete = true; + + constructor(private searchService: SearchService, private router: Router) { + } + + + areResults() { + return this.artists.length > 0 || this.discs.length > 0 || this.recordings.length > 0; + } + + show() { + if (this.input.nativeElement === document.activeElement) return true; + this.showList = false; + } + + ngOnInit(): void { + this.query$.subscribe(() => { + this.artists = []; + this.discs = []; + this.recordings = []; + } + ); + + let searchArtist = this.searchService.searchArtist(this.query$).subscribe((result) => { + this.artists = result.artists; + }); + let searchDisc = this.searchService.searchDisc(this.query$).subscribe((result) => { + this.discs = result.discs; + }); + let searchRecording = this.searchService.searchRecording(this.query$).subscribe((result) => { + this.recordings = result.recordings; + }); + } + + search() { + this.router.navigate(['search'], { + state: { + artists: this.artists, + discs: this.discs, + recordings: this.recordings + }, + queryParams: {query: this.query$.getValue()} + }); + } +} diff --git a/src/app/homepage/searchHome/searchHome.component.ts b/src/app/homepage/searchHome/searchHome.component.ts deleted file mode 100644 index 29068ac..0000000 --- a/src/app/homepage/searchHome/searchHome.component.ts +++ /dev/null @@ -1,51 +0,0 @@ -import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; -import {BehaviorSubject, forkJoin, Subject} from "rxjs"; -import {SearchService} from "../../search.service"; -import {Artist, Disc, Recording} from "../../models/brainz"; - -@Component({ - selector: 'app-search', - templateUrl: './searchHome.component.html', - styleUrls: ['./searchHome.component.scss'] -}) -export class SearchHomeComponent implements OnInit { - @ViewChild("input") input: ElementRef; - showList: boolean; - - artists: Artist[] = []; - discs: Disc[] = []; - recordings: Recording[] = []; - - searchTerm$ = new Subject(); - - constructor(private searchService: SearchService) { - } - - - areResults() { - return this.artists.length > 0 || this.discs.length > 0 || this.recordings.length > 0; - } - - show() { - if (this.input.nativeElement === document.activeElement) return true; - this.showList = false; - } - - ngOnInit(): void { - this.searchTerm$.subscribe(() => { - this.artists = []; - this.discs = []; - this.recordings = []; - } - ); - let searchArtist = this.searchService.searchArtist(this.searchTerm$).subscribe((result) => { - this.artists = result.artists; - }); - let searchDisc = this.searchService.searchDisc(this.searchTerm$).subscribe((result) => { - this.discs = result.discs; - }); - let searchRecording = this.searchService.searchRecording(this.searchTerm$).subscribe((result) => { - this.recordings = result.recordings; - }); - } -} diff --git a/src/app/search/search.component.html b/src/app/search/search.component.html new file mode 100644 index 0000000..5ecba1b --- /dev/null +++ b/src/app/search/search.component.html @@ -0,0 +1,60 @@ + + +
+
+
+ +
+
+
+ + + + +
+
+
+

{{artist.name}}

+
+
+
+
+ +
+
+
+

{{disc.title}}

+
+
+
+
+ +
+
+
+

{{song.title}}

+
+
+
+
diff --git a/src/app/search/search.component.scss b/src/app/search/search.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/search/search.component.spec.ts b/src/app/search/search.component.spec.ts new file mode 100644 index 0000000..4372919 --- /dev/null +++ b/src/app/search/search.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { SearchComponent } from './search.component'; + +describe('SearchComponent', () => { + let component: SearchComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ SearchComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(SearchComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/search/search.component.ts b/src/app/search/search.component.ts new file mode 100644 index 0000000..74d1f0c --- /dev/null +++ b/src/app/search/search.component.ts @@ -0,0 +1,66 @@ +import {Component, OnInit} from '@angular/core'; +import {ActivatedRoute} from "@angular/router"; +import {SearchService} from "../services/search.service"; +import {map} from "rxjs/operators"; +import {Artist, Disc, Recording} from "../models/brainz"; +import {BehaviorSubject, Observable} from "rxjs"; + +@Component({ + selector: 'app-search', + templateUrl: './search.component.html', + styleUrls: ['./search.component.scss'] +}) +export class SearchComponent implements OnInit { + query$ = new BehaviorSubject(""); + + artists: Artist[] = []; + discs: Disc[] = []; + recordings: Recording[] = []; + + artists_active = true; + discs_active = false; + songs_active= false; + + constructor(private route: ActivatedRoute, private searchService: SearchService) { + this.route.queryParams.pipe(map(params => params['query'] as string)).subscribe((query) => { + this.query$.next(query); + }); + } + + ngOnInit(): void { + this.query$.subscribe(() => { + this.artists = []; + this.discs = []; + this.recordings = []; + }); + + this.searchService.searchArtist(this.query$).subscribe((result) => this.artists = result.artists); + this.searchService.searchDisc(this.query$).subscribe((result) => this.discs = result.discs); + this.searchService.searchRecording(this.query$).subscribe((result) => this.recordings = result.recordings); + } + + show_artist() { + if(this.artists_active) return; + + this.artists_active = true; + this.discs_active = false; + this.songs_active = false; + } + + show_discs() { + if(this.discs_active) return; + + this.artists_active = false; + this.discs_active = true; + this.songs_active = false; + } + + show_songs() { + if(this.songs_active) return; + + this.artists_active = false; + this.discs_active = false; + this.songs_active = true; + } + +} diff --git a/src/app/search.service.spec.ts b/src/app/services/search.service.spec.ts similarity index 100% rename from src/app/search.service.spec.ts rename to src/app/services/search.service.spec.ts diff --git a/src/app/search.service.ts b/src/app/services/search.service.ts similarity index 62% rename from src/app/search.service.ts rename to src/app/services/search.service.ts index 914bbfd..3fdb229 100644 --- a/src/app/search.service.ts +++ b/src/app/services/search.service.ts @@ -2,7 +2,7 @@ import {Injectable} from '@angular/core'; import {forkJoin, merge, Observable, of} from "rxjs"; import {HttpClient} from "@angular/common/http"; import {debounceTime, distinctUntilChanged, switchMap, tap} from "rxjs/operators"; -import {ArtistSearch, DiscSearch, RecordingSearch} from "./models/brainz"; +import {ArtistSearch, DiscSearch, RecordingSearch} from "../models/brainz"; @Injectable({ providedIn: 'root' @@ -13,39 +13,39 @@ export class SearchService { constructor(private http: HttpClient) { } - searchArtist(terms: Observable): Observable { + searchArtist(terms: Observable, page: number = 1): Observable { return terms.pipe( debounceTime(400), distinctUntilChanged(), - switchMap(term => this.searchArtistQuery(term)) + switchMap(term => this.searchArtistQuery(term, page)) ); } - searchDisc(terms: Observable): Observable { + searchDisc(terms: Observable, page: number = 1): Observable { return terms.pipe( debounceTime(400), distinctUntilChanged(), - switchMap(term => this.searchDiscQuery(term)) + switchMap(term => this.searchDiscQuery(term, page)) ); } - searchRecording(terms: Observable): Observable { + searchRecording(terms: Observable, page: number = 1): Observable { return terms.pipe( debounceTime(400), distinctUntilChanged(), - switchMap(term => this.searchRecordingQuery(term)) + switchMap(term => this.searchRecordingQuery(term, page)) ); } - private searchArtistQuery(term: string): Observable { + private searchArtistQuery(term: string, page: number): Observable { if (!term) { return of({paginate: {total: 0, current_page: 0, last_page: 0, per_page: 0}, artists: []} as ArtistSearch); } - return this.http.get(`${this.baseUrl}/artist?query=${term}`); + return this.http.get(`${this.baseUrl}/artist?query=${term}&page=${page}`); } - private searchDiscQuery(term: string): Observable { + private searchDiscQuery(term: string, page: number): Observable { if (!term) { return of({paginate: {total: 0, current_page: 0, last_page: 0, per_page: 0}, discs: []} as DiscSearch); } @@ -53,7 +53,7 @@ export class SearchService { return this.http.get(`${this.baseUrl}/disc?query=${term}`); } - private searchRecordingQuery(term: string): Observable { + private searchRecordingQuery(term: string, page: number): Observable { if (!term) { return of({paginate: {total: 0, current_page: 0, last_page: 0, per_page: 0}, recordings: []} as RecordingSearch); }