import { useState } from "react"; import { motion } from "motion/react"; import { SubmitFormProps, getTopicConfig } from "../../../config/topicConfig"; import { MediaUploadBox } from "./shared/MediaUploadBox"; import { TeammatesSection, Teammate } from "./shared/TeammatesSection"; import { FormInput } from "./shared/FormInput"; import { uploadImage, submitMission } from "../../../services/feedService"; import { useProfile } from "../../context/ProfileContext"; // Helper function to generate unique IDs const generateId = () => `teammate-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; export function ImageForm({ topicId, topicTitle, onSubmit }: SubmitFormProps) { const topicConfig = getTopicConfig(topicId); const { refreshProfile } = useProfile(); // ===== Form Fields State ===== const [title, setTitle] = useState(""); const [learnings, setLearnings] = useState(""); // ===== Teammates State (with unique IDs and verification) ===== const [teammates, setTeammates] = useState([{ id: generateId(), phone: "" }]); // ===== Media State ===== const [uploadedImage, setUploadedImage] = useState(null); const [uploadedImageFile, setUploadedImageFile] = useState(null); // ===== Loading State ===== const [isSubmitting, setIsSubmitting] = useState(false); const [uploadProgress, setUploadProgress] = useState(""); // ===== Media Handler ===== const handleImageUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { setUploadedImageFile(file); const reader = new FileReader(); reader.onloadend = () => setUploadedImage(reader.result as string); reader.readAsDataURL(file); } }; // ===== Teammates Handlers ===== const handleAddTeammate = () => { setTeammates([...teammates, { id: generateId(), phone: "" }]); }; const handleRemoveTeammate = (id: string) => { const filtered = teammates.filter((t) => t.id !== id); setTeammates(filtered.length > 0 ? filtered : [{ id: generateId(), phone: "" }]); }; const handleTeammateChange = (id: string, value: string) => { setTeammates(teammates.map((t) => (t.id === id ? { ...t, phone: value, fullName: undefined, userId: undefined, error: undefined } : t))); }; const handleTeammateVerify = (id: string, fullName: string, userId: string, error?: string) => { setTeammates(teammates.map((t) => (t.id === id ? { ...t, fullName, userId, error } : t))); }; const handleSubmit = async () => { if (isSubmitting) return; setIsSubmitting(true); try { // آپلود تصویر اگر وجود دارد let imageFilename = ""; if (uploadedImageFile) { setUploadProgress("در حال آپلود تصویر..."); const filename = await uploadImage(uploadedImageFile); if (filename) { imageFilename = filename; } else { alert("خطا در آپلود تصویر"); setIsSubmitting(false); setUploadProgress(""); return; } } // Get verified user IDs and join with comma const verifiedUserIds = teammates .filter((t) => t.userId) .map((t) => t.userId) .join(","); // Get workflow_ID from localStorage const workflowID = localStorage.getItem("current_workflow_ID") || ""; // ثبت نهایی ماموریت setUploadProgress("در حال ثبت ماموریت..."); const result = await submitMission({ title: title || "", mission_type: topicTitle, mission_done_workflowID: workflowID, description: learnings || "", film: "", image: imageFilename, audio: "", team_member: verifiedUserIds, }); if (result.success) { // بروزرسانی پروفایل (برای سکه‌های جدید) await refreshProfile(); // فراخوانی onSubmit برای نمایش modal و هدایت به feed onSubmit({ topicId, title, learnings, teammates: verifiedUserIds ? [verifiedUserIds] : [], mediaType: "image", uploadedImage, }); } else { alert(result.message || "خطا در ثبت ماموریت"); } } catch (error) { console.error("Error submitting:", error); alert("خطا در ثبت ماموریت"); } finally { setIsSubmitting(false); setUploadProgress(""); } }; return (
{/* Image Upload */} setUploadedImage(null)} label="تصویر چالش" /> {/* Teammates - Only show if config requires it */} {topicConfig.requiresTeammates && ( )} {/* Title */} {/* Learnings */} {/* Submit Button */} {isSubmitting ? "در حال ثبت..." : "ثبت نهایی چالش"} {/* Upload Progress Message */} {uploadProgress && (
{uploadProgress}
)}
); }