diff --git a/frontend/components/EventForm.tsx b/frontend/components/EventForm.tsx index d8c9a5c..38556b0 100644 --- a/frontend/components/EventForm.tsx +++ b/frontend/components/EventForm.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from "react"; +import React, { useState, useEffect, useMemo } from "react"; import { EventType, EventStatus, Address } from "../types"; import { Input, Select } from "./Input"; import { Button } from "./Button"; @@ -26,7 +26,7 @@ import { useData } from "../contexts/DataContext"; import { UserRole } from "../types"; import { InstitutionForm } from "./InstitutionForm"; import { MapboxMap } from "./MapboxMap"; -import { getEventTypes, EventTypeResponse, getCadastroFot, createAgenda } from "../services/apiService"; +import { getEventTypes, EventTypeResponse, getCadastroFot, createAgenda, getCompanies } from "../services/apiService"; interface EventFormProps { onCancel: () => void; @@ -121,48 +121,30 @@ export const EventForm: React.FC = ({ fetchEventTypes(); }, []); - // Fetch FOTs filtered by user company - const [availableFots, setAvailableFots] = useState([]); - const [loadingFots, setLoadingFots] = useState(false); - - useEffect(() => { - const loadFots = async () => { - // Allow FOT loading for Business Owners, Event Owners (Clients), and Superadmins - if (user?.role === UserRole.BUSINESS_OWNER || user?.role === UserRole.EVENT_OWNER || user?.role === UserRole.SUPERADMIN) { - // If user is not superadmin (admin generally has no empresaId but sees all or selects one, here we assume superadmin logic is separate or allowed) - // Check if regular user has empresaId - if (user?.role !== UserRole.SUPERADMIN && !user?.empresaId) { - // If no company linked, do not load FOTs - return; - } - - setLoadingFots(true); - const token = localStorage.getItem("token") || ""; - // Use empresaId from user context if available - const empresaId = user.empresaId; - const response = await getCadastroFot(token, empresaId); - - if (response.data) { - // If we didn't filter by API (e.g. no empresaId), filter client side as fallback - const myFots = (empresaId || user.companyName) - ? response.data.filter(f => - (empresaId && f.empresa_id === empresaId) || - (user.companyName && f.empresa_nome === user.companyName) - ) - : response.data; - - setAvailableFots(myFots); - } - setLoadingFots(false); - } - }; - loadFots(); - }, [user]); - // Derived state for dropdowns + const [companies, setCompanies] = useState([]); // New state for companies list + const [selectedCompanyId, setSelectedCompanyId] = useState(""); // Changed from Name to ID + const [selectedCourseName, setSelectedCourseName] = useState(""); const [selectedInstitutionName, setSelectedInstitutionName] = useState(""); + // Load Companies for Business Owner / Superadmin + useEffect(() => { + if (user?.role === UserRole.BUSINESS_OWNER || user?.role === UserRole.SUPERADMIN) { + const fetchCompanies = async () => { + try { + const response = await getCompanies(); + if (response.data) { + setCompanies(response.data); + } + } catch (error) { + console.error("Failed to load companies", error); + } + }; + fetchCompanies(); + } + }, [user]); + // Populate form with initialData useEffect(() => { if (initialData) { @@ -171,11 +153,17 @@ export const EventForm: React.FC = ({ ...prev, ...initialData, startTime: initialData.time || "00:00", - locationName: (initialData as any).local_evento || (initialData as any).address?.mapLink?.includes('http') ? "" : (initialData as any).address?.mapLink || "", // Try to recover location name or clear if it's a link + locationName: (initialData as any).local_evento || (initialData as any).address?.mapLink?.includes('http') ? "" : (initialData as any).address?.mapLink || "", fotId: initialData.fotId || "", })); // 2. Populate derived dropdowns if data exists + // Check for empresa_id or empresaId in initialData + const initEmpresaId = (initialData as any).empresa_id || (initialData as any).empresaId; + if (initEmpresaId && (user?.role === UserRole.BUSINESS_OWNER || user?.role === UserRole.SUPERADMIN)) { + setSelectedCompanyId(initEmpresaId); + } + if (initialData.curso) { setSelectedCourseName(initialData.curso); } @@ -195,9 +183,47 @@ export const EventForm: React.FC = ({ })); } } - }, [initialData]); + }, [initialData, user?.role]); - // Unique Courses + // Fetch FOTs filtered by user company OR selected company + const [availableFots, setAvailableFots] = useState([]); + const [loadingFots, setLoadingFots] = useState(false); + + useEffect(() => { + const loadFots = async () => { + // Determine which company ID to use + let targetEmpresaId = user?.empresaId; + + if (user?.role === UserRole.BUSINESS_OWNER || user?.role === UserRole.SUPERADMIN) { + // Must select a company first + if (!selectedCompanyId) { + setAvailableFots([]); + return; + } + targetEmpresaId = selectedCompanyId; + } + + // If we have a target company (or user is linked), fetch FOTs + if (targetEmpresaId || user?.role === UserRole.EVENT_OWNER) { // EventOwner might be linked differently or via getCadastroFot logic + // Verify logic: EventOwner (client) usually has strict link. + // If targetEmpresaId is still empty and user is NOT BusinessOwner/Superadmin, we might skip or let backend decide (if user has implicit link). + // But let's assume valid flow. + + setLoadingFots(true); + const token = localStorage.getItem("token") || ""; + const response = await getCadastroFot(token, targetEmpresaId); + + if (response.data) { + setAvailableFots(response.data); + } + setLoadingFots(false); + } + }; + loadFots(); + }, [user, selectedCompanyId]); + + + // Unique Courses (from availableFots - which are already specific to the company) const uniqueCourses = Array.from(new Set(availableFots.map(f => f.curso_nome))).sort(); // Filtered Institutions based on Course @@ -611,7 +637,7 @@ export const EventForm: React.FC = ({

Seleção da Turma

- {!user?.empresaId && user?.role !== UserRole.SUPERADMIN ? ( + {!user?.empresaId && user?.role !== UserRole.SUPERADMIN && user?.role !== UserRole.BUSINESS_OWNER ? (
@@ -626,6 +652,42 @@ export const EventForm: React.FC = ({
) : ( <> + {/* 0. Empresa (Only for Business Owner / Superadmin) */} + {(user?.role === UserRole.BUSINESS_OWNER || user?.role === UserRole.SUPERADMIN) && ( +
+ + +
+ )} + + {/* Warning if Company Selected but No FOTs */} + {selectedCompanyId && !loadingFots && availableFots.length === 0 && (user?.role === UserRole.BUSINESS_OWNER || user?.role === UserRole.SUPERADMIN) && ( +
+
+
+
+
+

+ Esta empresa selecionada ainda não possui turmas, cursos ou FOTs cadastrados. +

+
+
+
+ )} + {/* 1. Curso */}
@@ -637,7 +699,7 @@ export const EventForm: React.FC = ({ setSelectedInstitutionName(""); setFormData({ ...formData, fotId: "" }); }} - disabled={loadingFots} + disabled={loadingFots || ((user?.role === UserRole.BUSINESS_OWNER || user?.role === UserRole.SUPERADMIN) && !selectedCompanyId)} > {uniqueCourses.map(c => )} @@ -685,7 +747,7 @@ export const EventForm: React.FC = ({