Skip to content

Commit 67021e2

Browse files
committed
fix logic response resource
1 parent 3e332fe commit 67021e2

File tree

9 files changed

+114
-48
lines changed

9 files changed

+114
-48
lines changed

src/app/core/models/post.models.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,6 @@ export type PostResponse = {
158158
};
159159

160160
//post-create
161-
export interface PostDataCreateRequest {
162-
file: File;
163-
description: string;
164-
tags: string[];
165-
isLectureVideo: boolean;
166-
isTextbook: boolean;
167-
}
168161

169162
export type SavedPostResponse = {
170163
id: string;

src/app/core/models/resource.model.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,25 @@ export interface ResourceData {
4343
tags: Tag[];
4444
isLectureVideo: boolean;
4545
isTextbook: boolean;
46-
viewCount: number;
47-
rating: number;
48-
orgId: string; // Guid
49-
duration: string; // TimeSpan
50-
hlsUrl: string;
46+
viewCount: number | null;
47+
rating: number | null;
48+
orgId: string | null; // Guid
49+
duration: string | null; // TimeSpan
50+
hlsUrl: string | null;
5151
createdAt: string;
52+
createdBy: string;
53+
organizationId: string | null;
54+
organizationRole: string;
55+
userProfile: UserBasicInfo;
56+
}
57+
58+
export interface ResourceCreateRequest {
59+
file: File;
60+
category: FileCategory;
61+
description: string;
62+
tags: string[];
5263
}
64+
5365
//--------------Input episode local---------------------
5466
export interface IEpisodeLocal {
5567
episodeId: string;

src/app/core/models/user.models.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//ví dụ
22
export const DEFAULT_BG = 'https://wallpaper.dog/large/10932813.jpg';
3-
export const DEFAULT_AVATAR =
4-
'https://static.vecteezy.com/system/resources/previews/023/329/545/large_2x/user-profile-shadow-symbol-icon-isolated-illustration-vector.jpg';
3+
export const DEFAULT_AVATAR = 'auth-assets/avatar_placeholder.png';
54

65
export type User = {
76
userId: string; // id của user

src/app/core/services/api-service/resource.service.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import {
66
XuanIPaginationResponse,
77
XuanPresignedUrlResponse,
88
} from '../../models/api-response';
9-
import { MediaResource, ResourceData, Tag } from '../../models/resource.model';
9+
import {
10+
MediaResource,
11+
ResourceCreateRequest,
12+
ResourceData,
13+
Tag,
14+
} from '../../models/resource.model';
1015
import { API_CONFIG } from '../config-service/api.enpoints';
11-
import { PostDataCreateRequest } from '../../models/post.models';
1216

1317
@Injectable({
1418
providedIn: 'root',
@@ -39,12 +43,12 @@ export class ResourceService {
3943
}
4044

4145
getVideoResources() {
42-
return this.api.get<ApiResponse<ResourceData[]>>(
46+
return this.api.get<ApiResponse<MediaResource[]>>(
4347
API_CONFIG.ENDPOINTS.GET.GET_FILE_VIDEOS
4448
);
4549
}
4650
getDocumentResources() {
47-
return this.api.get<ApiResponse<ResourceData[]>>(
51+
return this.api.get<ApiResponse<MediaResource[]>>(
4852
API_CONFIG.ENDPOINTS.GET.GET_FILE_DOCUMENTS
4953
);
5054
}
@@ -54,15 +58,14 @@ export class ResourceService {
5458
);
5559
}
5660

57-
addResource(postData: PostDataCreateRequest) {
58-
const { file, description, tags, isLectureVideo, isTextbook } = postData;
61+
addResource(postData: ResourceCreateRequest) {
62+
const { file, category, description, tags } = postData;
5963

6064
// data: phần dữ liệu thông thường (không phải file)
6165
const data: Record<string, any> = {
66+
category,
6267
description,
6368
tags: JSON.stringify(tags), // stringify array để backend parse
64-
isLectureVideo: String(isLectureVideo),
65-
isTextbook: String(isTextbook),
6669
};
6770

6871
// files: phần file

src/app/features/resource-learning/pages/resource-create/resource-create.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { InputComponent } from '../../../../shared/components/fxdonad-shared/inp
2525
import { decodeJWT } from '../../../../shared/utils/stringProcess';
2626
import { FormsModule } from '@angular/forms';
2727
import { Location } from '@angular/common';
28+
import { FileCategory } from '../../../../core/models/resource.model';
2829

2930
@Component({
3031
selector: 'app-resource-create',
@@ -104,15 +105,14 @@ export class ResourceCreatePageComponent {
104105
sendNotification(
105106
this.store,
106107
'Tạo tài nguyên',
107-
'Vui lòng chọn file!',
108+
'Yêu cầu chọn file!',
108109
'error'
109110
);
110111
return;
111112
}
112113

113114
const file = this.selectedFile;
114-
let isTextbook = false;
115-
let isLectureVideo = false;
115+
let category: FileCategory = 3;
116116

117117
if (file) {
118118
const fileType = file.type;
@@ -123,19 +123,21 @@ export class ResourceCreatePageComponent {
123123
fileType.includes('presentation') ||
124124
fileType.includes('text')
125125
) {
126-
isTextbook = true;
126+
category = 2;
127127
}
128128
if (fileType.startsWith('video/')) {
129-
isLectureVideo = true;
129+
category = 1;
130+
}
131+
if (fileType.startsWith('image/')) {
132+
category = 0;
130133
}
131134
}
132135

133136
const postData = {
134137
file,
138+
category,
135139
description: this.htmlToMd.convert(this.editorContent),
136140
tags: this.tags,
137-
isTextbook,
138-
isLectureVideo,
139141
};
140142
this.store.dispatch(
141143
setLoading({

src/app/features/resource-learning/pages/resource-detail/resource-detail.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
alt="Avatar"
1818
/>
1919
</div>
20-
<div class="name">{{authorName}}</div>
20+
<div class="name">{{authorName ? authorName : 'Ẩn danh'}}</div>
2121
</div>
2222
<!-- <div class="action">
2323
<div class="report">
@@ -30,8 +30,8 @@
3030
</div>
3131
<div class="tag-description">
3232
<div class="resource-tags">
33-
@for (tag of resource.tags; track tag) {
34-
<span class="resource-tag"> #{{ tag.name }} </span>
33+
@for (tag of tags; track tag) {
34+
<span class="resource-tag"> #{{ tag }} </span>
3535
}
3636
</div>
3737
</div>

src/app/features/resource-learning/pages/resource-detail/resource-detail.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class ResourceDetail implements OnInit {
5050
isDocument: boolean = false;
5151
isImage: boolean = false;
5252
safeUrl!: SafeResourceUrl;
53+
tags: string[] = [];
5354

5455
// trạng thái phân trang
5556
isLoading = false;
@@ -113,6 +114,7 @@ export class ResourceDetail implements OnInit {
113114
this.resource.fileType
114115
);
115116
this.isImage = this.resource.fileType.startsWith('image/');
117+
this.tags = JSON.parse(res.result.tags[0].name);
116118
if (this.resource.url) {
117119
this.setSafeUrl(this.resource.url);
118120
}

src/app/features/resource-learning/pages/resource-list/resource-list.ts

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { NgClass } from '@angular/common';
21
import { Component } from '@angular/core';
32
import { Router } from '@angular/router';
4-
import { DropdownButtonComponent } from '../../../../shared/components/fxdonad-shared/dropdown/dropdown.component';
53
import { InputComponent } from '../../../../shared/components/fxdonad-shared/input/input';
64
import { SkeletonLoadingComponent } from '../../../../shared/components/fxdonad-shared/skeleton-loading/skeleton-loading.component';
75
import { TrendingItem } from '../../../../shared/components/fxdonad-shared/trending/trending.component';
86
import { ButtonComponent } from '../../../../shared/components/my-shared/button/button.component';
97
import { MediaResource } from '../../../../core/models/resource.model';
108
import { ResourceCardComponent } from '../../../../shared/components/my-shared/resource-card/resource-card';
119
import { ResourceService } from '../../../../core/services/api-service/resource.service';
12-
import { mapToResourceCardList } from '../../../../shared/utils/mapData';
1310
import { Store } from '@ngrx/store';
1411
import {
1512
clearLoading,
@@ -18,6 +15,8 @@ import {
1815
import { sendNotification } from '../../../../shared/utils/notification';
1916
import { ResourceEditPopupComponent } from '../../modal/popup-update/resource-edit-popup.component';
2017
import { 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 ==================

src/app/shared/utils/mapData.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Post,
1717
} from '../../core/models/post.models';
1818
import {
19+
MediaResource,
1920
ResourceData,
2021
resourceCardInfo,
2122
} from '../../core/models/resource.model';
@@ -101,14 +102,14 @@ export function mapPostdatatoPostCardInfo(post: PostResponse): PostCardInfo {
101102
allowComment: post.allowComment,
102103
};
103104
}
104-
export function maptoReourseCard(resourse: ResourceData): resourceCardInfo {
105+
export function maptoReourseCard(resourse: MediaResource): resourceCardInfo {
105106
return {
106107
id: resourse.id,
107108
avatarAuthor:
108109
'https://i.pinimg.com/736x/8b/8e/ff/8b8eff070443cf6103c8279a28673809.jpg',
109110
authorId: resourse.id,
110111
authorName: 'author',
111-
duration: resourse.duration,
112+
duration: resourse.duration?.toString() || '',
112113
progress: 5,
113114
title: resourse.fileName,
114115
thumnailurl: resourse.thumbnailUrl,
@@ -120,15 +121,15 @@ export function maptoReourseCard(resourse: ResourceData): resourceCardInfo {
120121
};
121122
}
122123
export function mapToResourceCardList(
123-
resources: ResourceData[]
124+
resources: MediaResource[]
124125
): resourceCardInfo[] {
125126
return resources.map((resource) => ({
126127
id: resource.id,
127128
avatarAuthor:
128129
'https://i.pinimg.com/1200x/b3/c2/77/b3c2779d6b6195793b72bf73e284b3e8.jpg',
129130
authorId: resource.id,
130131
authorName: 'Ẩn danh',
131-
duration: resource.duration,
132+
duration: resource.duration?.toString() || '',
132133
thumnailurl: resource.thumbnailUrl,
133134
progress: 5,
134135
title: resource.fileName,

0 commit comments

Comments
 (0)