Skip to content

Commit c1fcb4f

Browse files
setting up uploads api
1 parent 011dcc1 commit c1fcb4f

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

apps/client/src/components/Chat.tsx

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import { format } from "date-fns";
1010
import { getChatMessages, createRoom } from "../services/MindsMeshAPI";
1111
import { v4 as uuidv4 } from "uuid";
1212
import { PaperClipIcon } from "@heroicons/react/20/solid";
13-
import { SocketContext } from "../contexts/SocketContext";
13+
import { SocketContext } from "../contexts/SocketContext";
14+
import { uploadFile as apiUploadFile } from '../services/MindsMeshAPI';
15+
1416

1517

1618
interface Message {
@@ -236,28 +238,17 @@ const Chat: React.FC<{ chatPartner?: User | null; onClose?: () => void }> = ({
236238
if (!chatPartner || !senderId) {
237239
return null;
238240
}
239-
241+
240242
const formData = new FormData();
241243
formData.append('file', file);
242244
formData.append('text', newMessage);
243245
formData.append('messageId', uuidv4());
244-
246+
245247
try {
246248
setIsUploading(true);
247-
const response = await fetch(`/api/chat/${chatPartner.id}/upload`, {
248-
method: 'POST',
249-
headers: {
250-
'Authorization': `Bearer ${localStorage.getItem('token')}`,
251-
},
252-
body: formData,
253-
});
254-
255-
if (!response.ok) {
256-
throw new Error(`Upload failed: ${response.statusText}`);
257-
}
258-
259-
const data = await response.json();
260-
console.log("File upload response:", data); // Debug log
249+
const data = await apiUploadFile(chatPartner.id, formData);
250+
251+
console.log("File upload response:", data);
261252
return {
262253
url: data.fileUrl,
263254
fileName: data.fileName,

apps/client/src/services/MindsMeshAPI.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,20 @@ export const fetchRoomsForFreelancer = async (
421421
throw error;
422422
}
423423
};
424+
425+
export const uploadFile = async (
426+
receiverId: string,
427+
formData: FormData
428+
): Promise<{ fileUrl: string, fileName: string, fileType: string }> => {
429+
try {
430+
const response = await api.post(`/chat/${receiverId}/upload`, formData, {
431+
headers: {
432+
'Content-Type': 'multipart/form-data',
433+
},
434+
});
435+
return response.data;
436+
} catch (error) {
437+
console.error("Error uploading file:", error);
438+
throw error;
439+
}
440+
};

0 commit comments

Comments
 (0)