= {
creatorUID: auth.user!.uid,
title,
@@ -146,70 +279,40 @@ export default function ContentEditor({ isEditMode }: { isEditMode: boolean }) {
};
try {
- // Handle thumbnail upload if present
+ // TODO: Move the thumbnail logic to the CONTENT SERVICE when developing the CONTENT SERVICE.
if (thumbnail) {
- const formData = new FormData();
- formData.append("thumbnail", thumbnail);
- const thumbRes = await axios.post(
- `${apiURL}/content/uploadThumbnail`,
- formData,
- {
- headers: { "Content-Type": "multipart/form-data" },
- }
+ const thumbnailUploadResponse = await ImageService.uploadThumbnail(
+ thumbnail
);
- newContent.thumbnailUrl = thumbRes.data.url;
- }
- // Determine request method and URL
- if (isEditMode) {
- await axios.put(
- `${apiURL}/content/${id}`,
- newContent,
- {
- headers: { "Content-Type": "application/json" },
- withCredentials: true,
- }
- );
- } else {
- await axios.post(
- `${apiURL}/content`,
- {
- ...newContent,
- },
- {
- headers: { "Content-Type": "application/json" },
- withCredentials: true,
- }
- );
+ if (thumbnailUploadResponse instanceof Error) {
+ setError(
+ thumbnailUploadResponse.message ||
+ "Failed to upload thumbnail. Please try again."
+ );
+ return;
+ }
+ newContent.thumbnail = thumbnailUploadResponse.url;
}
+ await submitContent(newContent);
} catch (error) {
- console.error("Error submitting content:", error);
setError(
"An error occurred while submitting the content. Please try again."
);
+ return;
}
// If submission was successful, redirect to the content page
if (!error) {
- // Reset local storage and cookies
- localStorage.removeItem("title");
- Cookies.remove("content");
- setTitle("");
- setContent("");
-
- if (id) {
- navigate(`/content/${id}`);
- } else {
- setError("Content ID not found after submission. Please try again.");
- }
- }
-
- // User must be authenticated to create content
- if (!auth.isAuthenticated) {
- navigate("/authentication/login");
+ handlePostSubmit();
}
};
+ // User must be authenticated to create content
+ if (!auth.isAuthenticated) {
+ navigate("/authentication/login");
+ }
+
// --------------------------------------
// -------------- Render ----------------
// --------------------------------------
@@ -265,8 +368,6 @@ export default function ContentEditor({ isEditMode }: { isEditMode: boolean }) {
)}
@@ -286,23 +387,7 @@ export default function ContentEditor({ isEditMode }: { isEditMode: boolean }) {
{error && {error}
}
-