@@ -4,7 +4,12 @@ import {FormControl, FormGroupDirective, NgForm, Validators} from '@angular/form
44import { ErrorStateMatcher } from '@angular/material/core' ;
55
66import { FetchService } from '../services/fetch.service' ;
7- import { ICloudBook } from '../../vendor' ;
7+ import { MessageService } from '../../services/message.service' ;
8+
9+ import {
10+ ICloudBook ,
11+ IMessage
12+ } from '../../vendor' ;
813
914/** Error when invalid control is dirty, touched, or submitted. */
1015export class MyErrorStateMatcher implements ErrorStateMatcher {
@@ -37,24 +42,35 @@ export class SearchComponent implements OnInit {
3742 icon : 'assets/images/gitlab-seeklogo.com.svg' ,
3843 } ] ;
3944
45+ matcher = new MyErrorStateMatcher ( ) ;
4046 keywords : string = '' ;
4147 platformSelected : string = 'github.com' ;
4248 bookListCount : number ;
4349
4450 constructor (
45- private fetchService : FetchService
51+ private fetchService : FetchService ,
52+ private message : MessageService
4653 ) { }
4754
48- ngOnInit ( ) { }
55+ ngOnInit ( ) {
56+ this . message . getMessage ( ) . subscribe ( async ( msg : IMessage ) => {
57+ if ( msg . event === 'scrolled-to-end' ) {
58+ if ( this . bookList . length % 20 === 0 && ! ( / g i t l a b / . test ( this . platformSelected ) ) ) {
59+ this . search ( this . bookList . length / 20 + 1 ) ;
60+ }
61+ }
62+ } ) ;
63+ }
4964
5065 clearKeywords = ( ) => {
5166 this . keywords = '' ;
52- this . bookList = [ ] . slice ( ) ;
5367 }
54-
55- search = async ( ) => {
56- const res = await this . fetchService . searchBooks ( this . platformSelected , this . keywords , 1 ) as object [ ] | object ;
57- console . log ( res )
68+ /*
69+ *
70+ * @page , 传入 1 表示首次搜索,需要清空原有 bookList
71+ */
72+ search = async ( page : number ) => {
73+ const res = await this . fetchService . searchBooks ( this . platformSelected , this . keywords , page ) as object [ ] | object ;
5874
5975 let _bookList : object [ ]
6076 if ( / g i t h u b / . test ( this . platformSelected ) ) {
@@ -64,9 +80,10 @@ export class SearchComponent implements OnInit {
6480 _bookList = ( res as object [ ] ) . slice ( ) ;
6581 }
6682
67- this . bookList = [ ] . slice ( ) ;
83+ if ( page === 1 ) this . bookList = [ ] . slice ( ) ;
6884 _bookList . map ( ( bookRaw : object ) => {
6985 const book : ICloudBook = {
86+ fullName : '' ,
7087 url : '' ,
7188 desc : '' ,
7289 writerName : '' ,
@@ -76,26 +93,25 @@ export class SearchComponent implements OnInit {
7693 } ;
7794
7895 book . desc = bookRaw [ 'description' ] ? bookRaw [ 'description' ] : '' ;
96+
7997 if ( / g i t l a b / . test ( this . platformSelected ) ) {
8098 book . dateUpdated = bookRaw [ 'last_activity_at' ] ;
8199 book . writerName = bookRaw [ 'namespace' ] [ 'name' ] ;
82100 book . writerAvatarUrl = bookRaw [ 'namespace' ] [ 'avatar_url' ] ;
83101 book . url = bookRaw [ 'http_url_to_repo' ] ;
84102 book . stars = bookRaw [ 'star_count' ] ;
103+ book . fullName = bookRaw [ 'path_with_namespace' ] ;
85104 }
86105 if ( ! ( / g i t l a b / . test ( this . platformSelected ) ) ) {
87106 book . dateUpdated = bookRaw [ 'updated_at' ] ;
88107 book . writerName = / g i t h u b / . test ( this . platformSelected ) ? bookRaw [ 'owner' ] [ 'login' ] : bookRaw [ 'owner' ] [ 'name' ] ;
89108 book . writerAvatarUrl = bookRaw [ 'owner' ] [ 'avatar_url' ] ;
90109 book . url = / g i t h u b / . test ( this . platformSelected ) ? bookRaw [ 'clone_url' ] : bookRaw [ 'html_url' ] ;
91110 book . stars = bookRaw [ 'stargazers_count' ] ;
111+ book . fullName = bookRaw [ 'full_name' ] ;
92112 }
93113
94114 this . bookList . push ( book ) ;
95115 } ) ;
96-
97- console . log ( this . bookList ) ;
98116 }
99-
100- matcher = new MyErrorStateMatcher ( ) ;
101117}
0 commit comments