s3
This commit is contained in:
@@ -2,6 +2,7 @@ import React, { useState, useEffect } from "react";
|
||||
import { useNavigate, Link } from "react-router-dom";
|
||||
import { useAuth } from "../contexts/AuthContext";
|
||||
import { forumAPI, addressAPI } from "../services/api";
|
||||
import { uploadFiles } from "../services/uploadService";
|
||||
import TagInput from "../components/TagInput";
|
||||
import ForumImageUpload from "../components/ForumImageUpload";
|
||||
import { Address } from "../types";
|
||||
@@ -151,36 +152,53 @@ const CreateForumPost: React.FC = () => {
|
||||
try {
|
||||
setIsSubmitting(true);
|
||||
|
||||
// Create FormData
|
||||
const submitData = new FormData();
|
||||
submitData.append('title', formData.title);
|
||||
submitData.append('content', formData.content);
|
||||
submitData.append('category', formData.category);
|
||||
// Upload images to S3 first (if any)
|
||||
let imageFilenames: string[] = [];
|
||||
if (imageFiles.length > 0) {
|
||||
const uploadResults = await uploadFiles("forum", imageFiles);
|
||||
imageFilenames = uploadResults.map((result) => result.key);
|
||||
}
|
||||
|
||||
// Add tags as JSON string
|
||||
// Build the post data
|
||||
const postData: {
|
||||
title: string;
|
||||
content: string;
|
||||
category: string;
|
||||
tags?: string[];
|
||||
zipCode?: string;
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
imageFilenames?: string[];
|
||||
} = {
|
||||
title: formData.title,
|
||||
content: formData.content,
|
||||
category: formData.category,
|
||||
};
|
||||
|
||||
// Add tags if present
|
||||
if (formData.tags.length > 0) {
|
||||
submitData.append('tags', JSON.stringify(formData.tags));
|
||||
postData.tags = formData.tags;
|
||||
}
|
||||
|
||||
// Add location data for item requests
|
||||
if (formData.category === 'item_request' && formData.zipCode) {
|
||||
submitData.append('zipCode', formData.zipCode);
|
||||
postData.zipCode = formData.zipCode;
|
||||
// If we have coordinates from a saved address, send them to avoid re-geocoding
|
||||
if (formData.latitude !== undefined && formData.longitude !== undefined) {
|
||||
submitData.append('latitude', formData.latitude.toString());
|
||||
submitData.append('longitude', formData.longitude.toString());
|
||||
postData.latitude = formData.latitude;
|
||||
postData.longitude = formData.longitude;
|
||||
}
|
||||
}
|
||||
|
||||
// Add images
|
||||
imageFiles.forEach((file) => {
|
||||
submitData.append('images', file);
|
||||
});
|
||||
// Add S3 image keys
|
||||
if (imageFilenames.length > 0) {
|
||||
postData.imageFilenames = imageFilenames;
|
||||
}
|
||||
|
||||
const response = await forumAPI.createPost(submitData);
|
||||
const response = await forumAPI.createPost(postData);
|
||||
navigate(`/forum/${response.data.id}`);
|
||||
} catch (err: any) {
|
||||
setError(err.response?.data?.error || "Failed to create post");
|
||||
setError(err.response?.data?.error || err.message || "Failed to create post");
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user