import React, { useEffect, useState } from 'react'; import { Plus, Pencil, Trash2, Box, RefreshCw } from 'lucide-react'; import { Button } from '../Button'; import { Modal } from '../Modal'; import { Input } from '../Input'; import * as geometryService from '../../services/geometryTypeService'; import type { GeometryType } from '../../types'; import { useAuth } from '../../context/useAuth'; export const GeometrySettings: React.FC = () => { const { appUser } = useAuth(); const [types, setTypes] = useState([]); const [loading, setLoading] = useState(true); const [isModalOpen, setIsModalOpen] = useState(false); const [editingItem, setEditingItem] = useState(null); const [saving, setSaving] = useState(false); const [form, setForm] = useState({ name: '', efficiencyLoss: '20' }); const fetchTypes = React.useCallback(async () => { setLoading(true); try { const response = await geometryService.getAllTypes(); setTypes(response.data); } catch (error) { console.error('Error fetching geometry types', error); } finally { setLoading(false); } }, []); useEffect(() => { if (appUser) { fetchTypes(); } }, [appUser, fetchTypes]); const handleOpenModal = (item?: GeometryType) => { if (item) { setEditingItem(item); setForm({ name: item.name, efficiencyLoss: (item.efficiencyLoss ?? 0).toString() }); } else { setEditingItem(null); setForm({ name: '', efficiencyLoss: '20' }); } setIsModalOpen(true); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); try { const payload = { name: form.name, efficiencyLoss: parseFloat(form.efficiencyLoss) || 0 }; if (editingItem) { await geometryService.updateType(editingItem.id || editingItem._id!, payload); } else { await geometryService.createType(payload); } setIsModalOpen(false); fetchTypes(); } catch (error) { console.error('Error saving type', error); alert('Erro ao salvar tipo de geometria'); } finally { setSaving(false); } }; const handleDelete = async (id: string) => { if (!confirm('Tem certeza que deseja excluir? Isso pode afetar peças criadas com este tipo.')) return; try { await geometryService.deleteType(id); fetchTypes(); } catch (error) { console.error('Error deleting type', error); } }; const handleRestoreDefaults = async () => { if (!confirm('Isso irá apagar todos os tipos atuais e restaurar a lista padrão com 20% de perda. Continuar?')) return; setLoading(true); try { await geometryService.restoreDefaults(); fetchTypes(); } catch (error) { console.error('Error restoring defaults', error); alert('Erro ao restaurar padrões'); } finally { setLoading(false); } }; return (

Tipos de Geometria/Peças

Gerencie a lista padrão de peças e suas perdas de eficiência

{loading ? (
) : (
{types.map((type) => ( ))} {types.length === 0 && ( )}
Nome da Geometria Perda de Eficiência (%) Ações
{type.name} {type.efficiencyLoss}%
Nenhum tipo cadastrado.
)}
setIsModalOpen(false)} title={editingItem ? 'Editar Tipo' : 'Novo Tipo de Geometria'}>
setForm({ ...form, name: e.target.value })} required /> setForm({ ...form, efficiencyLoss: e.target.value })} required />
); };