Skip to content

Commit 34f70be

Browse files
committed
fix some fields request API
1 parent 61ca103 commit 34f70be

File tree

8 files changed

+247
-253
lines changed

8 files changed

+247
-253
lines changed
-72 Bytes
Binary file not shown.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export interface postData {
7171
}
7272
export type PostType = 'Global' | 'Private' | 'Org';
7373
export interface FileDocument {
74-
file?: File;
74+
file?: File[];
7575
category?: string; // BE mong muốn STRING
7676
description?: string;
7777
tags?: string[]; // mảng -> sẽ append tags[0], tags[1]...
@@ -102,7 +102,7 @@ export interface CreatePostRequest {
102102
hashtag?: string;
103103

104104
fileDocument?: {
105-
file?: File;
105+
files?: File[];
106106
category?: string;
107107
description?: string;
108108
tags?: string[];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class OrganizationService {
5454
return this.api.patchWithFormData<ApiResponse<null>>(
5555
API_CONFIG.ENDPOINTS.PATCH.EDIT_ORG(orgId),
5656
otherData,
57-
logo
57+
{ logo }
5858
);
5959
}
6060

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,19 @@ export class PostService {
9494
createPost(data: CreatePostRequest) {
9595
const { fileDocument, ...otherData } = data;
9696

97-
// Chuẩn bị files và form data
98-
const files: { [fieldName: string]: File } = {};
99-
if (fileDocument?.file) {
100-
files['fileDocument.file'] = fileDocument.file; //tên key cần khớp backend
97+
// Gom các field còn lại vào data (lọc undefined/null)
98+
const formDataData: Record<string, any> = {};
99+
for (const [key, value] of Object.entries(otherData)) {
100+
if (value !== undefined && value !== null) {
101+
formDataData[key] = value;
102+
}
101103
}
102104

103-
// Gom các field còn lại vào data
104-
const formDataData: Record<string, any> = {
105-
...otherData,
106-
};
105+
// Chuẩn bị files
106+
const files: { [fieldName: string]: File[] } = {};
107+
if (fileDocument?.files) {
108+
files['fileDocument.files'] = fileDocument.files; // key cần khớp backend
109+
}
107110

108111
if (fileDocument) {
109112
const fd: Record<string, any> = {
@@ -115,19 +118,18 @@ export class PostService {
115118
'fileDocument.orgId': fileDocument.orgId,
116119
};
117120

118-
// Chỉ append field nào có giá trị khác undefined
119121
for (const [key, value] of Object.entries(fd)) {
120-
if (value !== undefined) {
122+
if (value !== undefined && value !== null) {
121123
formDataData[key] = value;
122124
}
123125
}
124126
}
125127

126-
// Gọi method postWithFormData
128+
// Gọi API
127129
return this.api.postWithFormData<ApiResponse<null>>(
128130
API_CONFIG.ENDPOINTS.POST.ADD_POST,
129131
formDataData,
130-
files
132+
Object.keys(files).length ? files : undefined // chỉ gửi nếu có file
131133
);
132134
}
133135

src/app/core/services/config-service/api.methods.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export class ApiMethod {
200200
patchWithFormData<T>(
201201
endpoint: string,
202202
data?: Record<string, any>,
203-
files?: File | { [fieldName: string]: File | File[] },
203+
files?: File | { [fieldName: string]: File | File[] | undefined },
204204
apiType: 'MAIN_API' | 'SECONDARY_API' = 'MAIN_API'
205205
): Observable<T> {
206206
const url = `${API_CONFIG.BASE_URLS[apiType]}${endpoint}`;
@@ -221,8 +221,12 @@ export class ApiMethod {
221221
Object.keys(files).forEach((fieldName) => {
222222
const fileItem = files[fieldName];
223223
if (Array.isArray(fileItem)) {
224-
fileItem.forEach((file) => formData.append(fieldName, file));
225-
} else {
224+
fileItem.forEach((file) => {
225+
if (file !== undefined && file !== null) {
226+
formData.append(fieldName, file);
227+
}
228+
});
229+
} else if (fileItem !== undefined && fileItem !== null) {
226230
formData.append(fieldName, fileItem);
227231
}
228232
});

src/app/features/organization/organization-component/details-organization/details-organization.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ export class DetailsOrganizationComponent implements OnInit {
9797
}
9898

9999
onFileChange(event: any) {
100-
const file = event.target.files[0];
101-
if (file) {
102-
this.editForm.logo = file;
100+
const logo = event.target.files[0];
101+
if (logo) {
102+
this.editForm.logo = logo;
103103
}
104104
}
105105

0 commit comments

Comments
 (0)