fixing bugs with item notification radius
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
import React, { useState } from "react";
|
||||
import React, { useState, useEffect } from "react";
|
||||
import { useNavigate, Link } from "react-router-dom";
|
||||
import { useAuth } from "../contexts/AuthContext";
|
||||
import { forumAPI } from "../services/api";
|
||||
import { forumAPI, addressAPI } from "../services/api";
|
||||
import TagInput from "../components/TagInput";
|
||||
import ForumImageUpload from "../components/ForumImageUpload";
|
||||
import { Address } from "../types";
|
||||
|
||||
const CreateForumPost: React.FC = () => {
|
||||
const { user } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [userAddresses, setUserAddresses] = useState<Address[]>([]);
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
title: "",
|
||||
@@ -21,11 +23,26 @@ const CreateForumPost: React.FC = () => {
|
||||
| "general_discussion",
|
||||
tags: [] as string[],
|
||||
zipCode: user?.zipCode || "",
|
||||
latitude: undefined as number | undefined,
|
||||
longitude: undefined as number | undefined,
|
||||
});
|
||||
|
||||
const [imageFiles, setImageFiles] = useState<File[]>([]);
|
||||
const [imagePreviews, setImagePreviews] = useState<string[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchUserAddresses();
|
||||
}, []);
|
||||
|
||||
const fetchUserAddresses = async () => {
|
||||
try {
|
||||
const response = await addressAPI.getAddresses();
|
||||
setUserAddresses(response.data);
|
||||
} catch (error) {
|
||||
console.error("Error fetching addresses:", error);
|
||||
}
|
||||
};
|
||||
|
||||
const categories = [
|
||||
{
|
||||
value: "item_request",
|
||||
@@ -55,7 +72,21 @@ const CreateForumPost: React.FC = () => {
|
||||
>
|
||||
) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData((prev) => ({ ...prev, [name]: value }));
|
||||
|
||||
// If category is being changed to item_request and user has addresses, autopopulate location data
|
||||
if (name === "category" && value === "item_request" && userAddresses.length > 0) {
|
||||
// Try to find primary address first, otherwise use first address
|
||||
const primaryAddress = userAddresses.find(addr => addr.isPrimary) || userAddresses[0];
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
[name]: value,
|
||||
zipCode: primaryAddress.zipCode,
|
||||
latitude: primaryAddress.latitude,
|
||||
longitude: primaryAddress.longitude
|
||||
}));
|
||||
} else {
|
||||
setFormData((prev) => ({ ...prev, [name]: value }));
|
||||
}
|
||||
};
|
||||
|
||||
const handleTagsChange = (tags: string[]) => {
|
||||
@@ -134,6 +165,11 @@ const CreateForumPost: React.FC = () => {
|
||||
// Add location data for item requests
|
||||
if (formData.category === 'item_request' && formData.zipCode) {
|
||||
submitData.append('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());
|
||||
}
|
||||
}
|
||||
|
||||
// Add images
|
||||
|
||||
Reference in New Issue
Block a user