1- import { NgClass } from '@angular/common' ;
21import { Component } from '@angular/core' ;
32import { Router } from '@angular/router' ;
4- import { DropdownButtonComponent } from '../../../../shared/components/fxdonad-shared/dropdown/dropdown.component' ;
53import { InputComponent } from '../../../../shared/components/fxdonad-shared/input/input' ;
64import { SkeletonLoadingComponent } from '../../../../shared/components/fxdonad-shared/skeleton-loading/skeleton-loading.component' ;
75import { TrendingItem } from '../../../../shared/components/fxdonad-shared/trending/trending.component' ;
86import { ButtonComponent } from '../../../../shared/components/my-shared/button/button.component' ;
97import { MediaResource } from '../../../../core/models/resource.model' ;
108import { ResourceCardComponent } from '../../../../shared/components/my-shared/resource-card/resource-card' ;
119import { ResourceService } from '../../../../core/services/api-service/resource.service' ;
12- import { mapToResourceCardList } from '../../../../shared/utils/mapData' ;
1310import { Store } from '@ngrx/store' ;
1411import {
1512 clearLoading ,
@@ -18,6 +15,8 @@ import {
1815import { sendNotification } from '../../../../shared/utils/notification' ;
1916import { ResourceEditPopupComponent } from '../../modal/popup-update/resource-edit-popup.component' ;
2017import { ScrollEndDirective } from '../../../../shared/directives/scroll-end.directive' ;
18+ import { forkJoin } from 'rxjs' ;
19+ import { map } from 'rxjs/operators' ;
2120
2221@Component ( {
2322 selector : 'app-resource-list' ,
@@ -78,25 +77,69 @@ export class ResourceListComponent {
7877 }
7978
8079 ngOnInit ( ) : void {
80+ this . pageIndex = 1 ;
8181 this . fetchDataResource ( ) ;
8282 }
8383
84+ // ================== Fetch ==================
85+ // fetchDataResource(append: boolean = false) {
86+ // this.isLoadingMore = append;
87+ // this.isLoading = !append;
88+
89+ // this.resourceService
90+ // .getAllResourceLearning(this.itemsPerPage, this.pageIndex)
91+ // .subscribe({
92+ // next: (res) => {
93+ // const newData = res.result.data;
94+ // const { currentPage, totalPages } = res.result;
95+
96+ // this.resources = append ? [...this.resources, ...newData] : newData;
97+ // this.filteredResources = [...this.resources]; // gán mặc định cho search
98+
99+ // this.hasMore = currentPage < totalPages;
100+
101+ // this.isLoading = false;
102+ // this.isLoadingMore = false;
103+ // },
104+ // error: (err) => {
105+ // console.error(err);
106+ // this.isLoading = false;
107+ // this.isLoadingMore = false;
108+ // },
109+ // });
110+ // }
111+
84112 // ================== Fetch ==================
85113 fetchDataResource ( append : boolean = false ) {
86114 this . isLoadingMore = append ;
87115 this . isLoading = ! append ;
88116
89- this . resourceService
90- . getAllResourceLearning ( this . itemsPerPage , this . pageIndex )
117+ forkJoin ( [
118+ this . resourceService . getVideoResources ( ) ,
119+ this . resourceService . getDocumentResources ( ) ,
120+ ] )
121+ . pipe (
122+ map ( ( [ videosRes , docsRes ] ) => {
123+ const videos = videosRes . result ?? [ ] ;
124+ const docs = docsRes . result ?? [ ] ;
125+ return [ ...videos , ...docs ] ; // gộp
126+ } )
127+ )
91128 . subscribe ( {
92- next : ( res ) => {
93- const newData = res . result . data ;
94- const { currentPage, totalPages } = res . result ;
129+ next : ( allResources ) => {
130+ // Nếu append thì cộng dồn, ngược lại reset
131+ this . resources = append
132+ ? [ ...this . resources , ...allResources ]
133+ : allResources ;
95134
96- this . resources = append ? [ ...this . resources , ...newData ] : newData ;
97- this . filteredResources = [ ...this . resources ] ; // gán mặc định cho search
135+ // tự phân trang tại FE
136+ const start = ( this . pageIndex - 1 ) * this . itemsPerPage ;
137+ const end = this . pageIndex * this . itemsPerPage ;
98138
99- this . hasMore = currentPage < totalPages ;
139+ this . filteredResources = this . resources . slice ( 0 , end ) ; // hiển thị từ đầu tới trang hiện tại
140+
141+ // check còn data nữa không
142+ this . hasMore = end < this . resources . length ;
100143
101144 this . isLoading = false ;
102145 this . isLoadingMore = false ;
@@ -109,12 +152,23 @@ export class ResourceListComponent {
109152 } ) ;
110153 }
111154
155+ // ================== Load Next Page ==================
156+ // loadNextPage() {
157+ // if (this.isLoadingMore || !this.hasMore) return;
158+
159+ // this.pageIndex++;
160+ // this.fetchDataResource(true);
161+ // }
162+
112163 // ================== Load Next Page ==================
113164 loadNextPage ( ) {
114165 if ( this . isLoadingMore || ! this . hasMore ) return ;
115166
116167 this . pageIndex ++ ;
117- this . fetchDataResource ( true ) ;
168+ const end = this . pageIndex * this . itemsPerPage ;
169+
170+ this . filteredResources = this . resources . slice ( 0 , end ) ;
171+ this . hasMore = end < this . resources . length ;
118172 }
119173
120174 // ================== Delete ==================
0 commit comments