Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/app/core/models/post.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,12 @@ export type PostResponse = {
upvoteCount: number;
downvoteCount: number;
};

//post-create
export interface PostDataCreateRequest {
file: File;
description: string;
tags: string[];
isLectureVideo: boolean;
isTextbook: boolean;
}
55 changes: 15 additions & 40 deletions src/app/core/services/api-service/resource.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../../models/api-response';
import { MediaResource, ResourceData, Tag } from '../../models/resource.model';
import { API_CONFIG } from '../config-service/api.enpoints';
import { PostDataCreateRequest } from '../../models/post.models';

@Injectable({
providedIn: 'root',
Expand Down Expand Up @@ -52,51 +53,25 @@ export class ResourceService {
API_CONFIG.ENDPOINTS.GET.GET_FILE_BY_ID(id)
);
}
addResource(postData: {
file: File;
category: number;
description: string;
tags: string[];
isLectureVideo: boolean;
isTextbook: boolean;
orgId?: string;
associatedResourceIds?: string[];
thumbnailUrl?: string;
}) {
const formData = new FormData();

if (postData.file) {
formData.append('file', postData.file);
}

if (postData.category !== null) {
formData.append('category', postData.category.toString());
}

formData.append('description', postData.description || '');
formData.append('isLectureVideo', String(postData.isLectureVideo));
formData.append('isTextbook', String(postData.isTextbook));

if (postData.orgId) {
formData.append('orgId', postData.orgId);
}

if (postData.thumbnailUrl) {
formData.append('thumbnailUrl', postData.thumbnailUrl);
}
addResource(postData: PostDataCreateRequest) {
const { file, description, tags, isLectureVideo, isTextbook } = postData;

// Tags array
postData.tags?.forEach((tag, i) => formData.append(`tags[${i}]`, tag));
// data: phần dữ liệu thông thường (không phải file)
const data: Record<string, any> = {
description,
tags: JSON.stringify(tags), // stringify array để backend parse
isLectureVideo: String(isLectureVideo),
isTextbook: String(isTextbook),
};

// Associated resources array
postData.associatedResourceIds?.forEach((id, i) =>
formData.append(`associatedResourceIds[${i}]`, id)
);
// files: phần file
const files: File = file;

return this.api.post<ApiResponse<XuanPresignedUrlResponse>>(
return this.api.postWithFormData<ApiResponse<XuanPresignedUrlResponse>>(
API_CONFIG.ENDPOINTS.POST.ADD_FILE,
formData,
true
data,
files
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
<div class="post-create-container">
<div class="general-infor">
<div class="input-info">
<app-dropdown-button
[label]="'Loại tệp'"
[options]="category"
[variant]="'secondary'"
[size]="'small'"
[customDropField]="'main-type'"
[disabled]="false"
[multiSelect]="false"
[isDisplayCheckbox]="false"
(onSelect)="handleSelect('category', $event)"
[isOpen]="activeDropdown === 'category'"
(toggle)="toggleDropdown('category')"
[isDisplaySelectedOpptionLabels]="true"
[isButtonControl]="true"
[isSearchable]="true"
[minHeight]="true"
[needIndexColor]="true"
></app-dropdown-button>
</div>
<div class="input-title">
<app-input
label="Link thumbnail"
placeholder="Nhập link thumbnail..."
[value]="thumbnail"
(valueChange)="handleInputChange($event)"
[minLength]="3"
[variant]="'primary'"
[errorMessage]="thumbnailError"
[isSvg]="false"
></app-input>
</div>
<div class="input-title">
<!-- Thay bằng input -->
<app-input
Expand All @@ -54,8 +22,6 @@
[(value)]="editorContent"
[config]="editorConfig"
(onChange)="onContentChange($event)"
(onFocus)="onEditorFocus()"
(onBlur)="onEditorBlur()"
></app-text-editor>
</div>
<div class="post-create-button">
Expand Down Expand Up @@ -83,54 +49,6 @@
</div>
</div>
<div class="metric">
<div class="link-section">
<div class="link-header" (click)="startAddLink()">
<span>Thêm link</span>
<button type="button" class="add-link-btn">
<span>+</span>
</button>
</div>

<!-- Ô nhập link khi bấm + -->
<div class="link-input" *ngIf="isAddingLink">
<input
#linkInput
type="text"
[(ngModel)]="newLink"
placeholder="Nhập link..."
(keyup.enter)="addLink()"
/>
<button type="button" (click)="addLink()">OK</button>
</div>
<!-- Danh sách nhiều link -->
<div class="link-list" *ngIf="associatedResourceIds.length > 0">
<div
class="single-link"
*ngFor="let link of associatedResourceIds; let i = index"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="lucide lucide-link-2"
>
<path d="M9 17H7A5 5 0 0 1 7 7h2" />
<path d="M15 7h2a5 5 0 1 1 0 10h-2" />
<line x1="8" x2="16" y1="12" y2="12" />
</svg>
<a [href]="link" target="_blank">{{ link }}</a>
<button type="button" class="remove-link" (click)="removeLink(i)">
×
</button>
</div>
</div>
</div>
<div class="file-upload">
<label class="upload-label">
<input
Expand Down
Loading