import React, { useEffect, useState } from 'react'; import { Modal } from '../Modal'; import { Input } from '../Input'; import { Button } from '../Button'; import { Select } from '../Select'; import api from '../../services/api'; import * as geometryService from '../../services/geometryTypeService'; import { useAuth } from '../../context/useAuth'; import { useToast } from '../../hooks/useToast'; import type { Part, GeometryType } from '../../types'; interface CreatePartModalProps { isOpen: boolean; onClose: () => void; onSuccess: () => void; projectId?: string; initialData?: Part; } export const CreatePartModal: React.FC = ({ isOpen, onClose, onSuccess, projectId, initialData }) => { const { isGuest } = useAuth(); const { showGuestWarning } = useToast(); const [loading, setLoading] = useState(false); const [projects, setProjects] = useState<{ id: string, name: string }[]>([]); const [geometryTypes, setGeometryTypes] = useState([]); const [selectedProjectId, setSelectedProjectId] = useState(projectId || ''); const [formData, setFormData] = useState({ description: '', dimensions: '', weight: '', type: '', area: '', complexity: '', quantity: '1', notes: '' }); useEffect(() => { if (initialData) { setFormData({ description: initialData.description || '', dimensions: initialData.dimensions || '', weight: initialData.weight?.toString() || '', type: initialData.type || '', area: initialData.area?.toString() || '', complexity: initialData.complexity?.toString() || '', quantity: initialData.quantity?.toString() || '1', notes: initialData.notes || '' }); if (initialData.projectId) setSelectedProjectId(initialData.projectId); } else { setFormData({ description: '', dimensions: '', weight: '', type: '', area: '', complexity: '', quantity: '1', notes: '' }); if (projectId) setSelectedProjectId(projectId); } }, [initialData, isOpen, projectId]); useEffect(() => { if (isOpen) { if (!projectId) { api.get('/projects') .then(res => setProjects(res.data)) .catch(err => console.error("Error fetching projects", err)); } geometryService.getAllTypes() .then(res => setGeometryTypes(res.data)) .catch(err => console.error("Error fetching geometry types", err)); } }, [isOpen, projectId]); const handleChange = (e: React.ChangeEvent) => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (isGuest()) { showGuestWarning(); return; } const projectToUse = projectId || selectedProjectId; if (!projectToUse) { alert("Por favor, selecione um projeto."); return; } setLoading(true); try { const payload = { description: formData.type, // Usar o tipo como descrição projectId: projectToUse, dimensions: formData.dimensions || undefined, weight: formData.weight ? parseFloat(formData.weight) : undefined, type: formData.type || undefined, area: formData.area ? parseFloat(formData.area) : undefined, quantity: formData.quantity ? parseInt(formData.quantity) : 1, notes: formData.notes || undefined }; if (initialData) { await api.put(`/parts/${initialData.id}`, payload); } else { await api.post('/parts', payload); } onSuccess(); onClose(); } catch (error) { console.error('Error saving part', error); alert('Erro ao salvar peça'); } finally { setLoading(false); } }; return (
{!projectId && ( ({ label: t.name, value: t.name })) ]} value={formData.type} onChange={handleChange} required />