Skip to content

Commit 2d1a4c1

Browse files
authored
Merge pull request #196 from CapstoneProjectCMC/feature/ulr-user-learning-resources
update post
2 parents 427bc44 + b623e17 commit 2d1a4c1

File tree

5 files changed

+56
-26
lines changed

5 files changed

+56
-26
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export interface PostADD {
8888
allowComment?: boolean;
8989
postType?: PostType;
9090
fileUrls: string; // CHUẨN THEO BE (lưu ý đánh vần!)
91-
hashtag: string; // nếu muốn gửi dạng mảng
91+
hashtag: string[]; // nếu muốn gửi dạng mảng
9292
fileDocument?: FileDocument | null;
9393
}
9494

@@ -99,7 +99,7 @@ export interface CreatePostRequest {
9999
isPublic: boolean;
100100
allowComment: boolean;
101101
postType: PostType;
102-
hashtag?: string;
102+
hashtag?: string[]; // nếu muốn gửi dạng mảng
103103

104104
fileDocument?: {
105105
files?: File[];

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ export class PostService {
112112
const fd: Record<string, any> = {
113113
'fileDocument.category': fileDocument.category,
114114
'fileDocument.description': fileDocument.description,
115-
'fileDocument.tags': fileDocument.tags,
115+
// 'fileDocument.tags': fileDocument.tags, // Xử lý riêng bên dưới
116+
116117
'fileDocument.isLectureVideo': fileDocument.isLectureVideo,
117118
'fileDocument.isTextBook': fileDocument.isTextBook,
118119
'fileDocument.orgId': fileDocument.orgId,
@@ -123,6 +124,12 @@ export class PostService {
123124
formDataData[key] = value;
124125
}
125126
}
127+
// Nếu cần gửi tags theo dạng array [0], [1]...
128+
if (fileDocument.tags?.length) {
129+
fileDocument.tags.forEach((tag, i) => {
130+
formDataData[`fileDocument.tags[${i}]`] = tag;
131+
});
132+
}
126133
}
127134

128135
// Gọi API

src/app/features/post/pages/post-create/post-create.html

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
1414
></app-input>
1515
</div>
1616
<div class="input-info">
17-
<app-dropdown-button
17+
<app-input
18+
label="Nhập tag (cách nhau bởi dấu ,)"
19+
placeholder="Ví dụ: toán, lý, hoá"
20+
[value]="tagInput"
21+
(valueChange)="handleTagInputChange($event)"
22+
[variant]="'primary'"
23+
[isSvg]="false"
24+
></app-input>
25+
<!-- <app-dropdown-button
1826
[label]="'Chủ đề'"
1927
[options]="topics"
2028
[variant]="'secondary'"
@@ -51,8 +59,8 @@
5159
[isSearchable]="true"
5260
[minHeight]="true"
5361
[needIndexColor]="true"
54-
></app-dropdown-button>
55-
<app-dropdown-button
62+
></app-dropdown-button> -->
63+
<!-- <app-dropdown-button
5664
[label]="'tag'"
5765
[options]="tag"
5866
[variant]="'secondary'"
@@ -69,7 +77,7 @@
6977
[isSearchable]="true"
7078
[minHeight]="true"
7179
[needIndexColor]="true"
72-
></app-dropdown-button>
80+
></app-dropdown-button> -->
7381
</div>
7482
<div class="post-options-container">
7583
<div class="option-group">

src/app/features/post/pages/post-create/post-create.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
clearLoading,
3131
setLoading,
3232
} from '../../../../shared/store/loading-state/loading.action';
33+
import { th } from 'date-fns/locale';
3334

3435
export interface Draft {
3536
id: string; // ID duy nhất cho mỗi bản nháp
@@ -45,7 +46,7 @@ export interface Draft {
4546
imports: [
4647
InputComponent,
4748
TextEditor,
48-
DropdownButtonComponent,
49+
// DropdownButtonComponent,
4950
ButtonComponent,
5051
FormsModule,
5152
CommonModule,
@@ -64,7 +65,7 @@ export class PostCreatePageComponent {
6465
allowComment: true,
6566
postType: 'Global',
6667
fileUrls: '', // CHUẨN THEO BE (lưu ý đánh vần!)
67-
hashtag: '', // nếu muốn gửi dạng mảng
68+
hashtag: [], // nếu muốn gửi dạng mảng
6869
fileDocument: null,
6970
};
7071

@@ -114,6 +115,23 @@ export class PostCreatePageComponent {
114115
ngOnInit(): void {
115116
this.loadAllDrafts();
116117
}
118+
tagInput: string = ''; // người dùng nhập tag thô
119+
tags: string[] = []; // danh sách tag đã cắt ra
120+
handleTagInputChange(value: string | number): void {
121+
this.tagInput = value.toString();
122+
this.tags = this.tagInput
123+
.split(',')
124+
.map((t) => t.trim())
125+
.filter((t) => t.length > 0); // loại bỏ tag rỗng
126+
if (this.tags.length > 0) {
127+
if (!this.post.fileDocument) {
128+
this.post.fileDocument = {};
129+
}
130+
this.post.hashtag = this.tags;
131+
this.post.fileDocument.tags = this.tags;
132+
}
133+
console.log('Danh sách tag:', this.tags);
134+
}
117135

118136
onFilesSelected(event: Event) {
119137
const input = event.target as HTMLInputElement;
@@ -166,15 +184,6 @@ export class PostCreatePageComponent {
166184
this.post.fileDocument.orgId = selected?.value || '';
167185
}
168186

169-
if (dropdownKey === 'tag') {
170-
if (!this.post.fileDocument) this.post.fileDocument = {};
171-
if (Array.isArray(selected)) {
172-
this.post.fileDocument.tags = selected.map((s) => s.label);
173-
} else {
174-
this.post.fileDocument.tags = selected?.label ? [selected.label] : [];
175-
}
176-
}
177-
178187
if (dropdownKey === 'hashtag') {
179188
this.post.hashtag = selected?.value || '';
180189
}
@@ -262,12 +271,12 @@ export class PostCreatePageComponent {
262271

263272
// Hàm helper để cập nhật UI của dropdowns
264273
updateSelectedOptionsFromDraft(): void {
265-
if (this.post.hashtag) {
266-
const selectedTopic = this.topics.find(
267-
(t) => t.value === this.post.hashtag
268-
);
269-
if (selectedTopic) this.selectedOptions['hashtag'] = selectedTopic;
270-
}
274+
// if (this.post.hashtag) {
275+
// const selectedTopic = this.topics.find(
276+
// (t) => t.value === this.post.hashtag
277+
// );
278+
// if (selectedTopic) this.selectedOptions['hashtag'] = selectedTopic;
279+
// }
271280
if (this.post.orgId) {
272281
const selectedWhere = this.wherepost.find(
273282
(w) => w.value === this.post.orgId
@@ -378,7 +387,7 @@ export class PostCreatePageComponent {
378387
allowComment: true,
379388
postType: 'Global',
380389
fileUrls: '',
381-
hashtag: '',
390+
hashtag: [],
382391
fileDocument: null,
383392
};
384393
this.editorContent = '';

src/app/shared/utils/mapData.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ export function mapPostInfortoPost(post: PostADD): Post {
7878
};
7979
}
8080
export function mapPostdatatoPostCardInfo(post: PostResponse): PostCardInfo {
81+
let tagInput = post.hashtag || '';
82+
let taglist = tagInput
83+
.split(',')
84+
.map((t) => t.trim())
85+
.filter((t) => t.length > 0); // loại bỏ tag rỗng
86+
8187
return {
8288
id: post.postId,
8389
avatar: post.user?.avatarUrl || avatarUrlDefault,
@@ -87,7 +93,7 @@ export function mapPostdatatoPostCardInfo(post: PostResponse): PostCardInfo {
8793
title: post.title,
8894
time: post.createdAt,
8995
description: post.content,
90-
tags: [post.hashtag],
96+
tags: taglist,
9197
comment: post.commentCount,
9298
upvote: post.upvoteCount,
9399
downvote: post.downvoteCount,

0 commit comments

Comments
 (0)