fix: (cadastro-profissiona) ajuste de redirecionamento
This commit is contained in:
parent
4ce07c1fc5
commit
44b73a080d
5 changed files with 43 additions and 27 deletions
|
|
@ -6,6 +6,7 @@ import { getFunctions } from "../services/apiService";
|
||||||
interface ProfessionalFormProps {
|
interface ProfessionalFormProps {
|
||||||
onSubmit: (data: ProfessionalData) => void;
|
onSubmit: (data: ProfessionalData) => void;
|
||||||
onCancel: () => void;
|
onCancel: () => void;
|
||||||
|
isLoading?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ProfessionalData {
|
export interface ProfessionalData {
|
||||||
|
|
@ -42,6 +43,7 @@ export interface ProfessionalData {
|
||||||
export const ProfessionalForm: React.FC<ProfessionalFormProps> = ({
|
export const ProfessionalForm: React.FC<ProfessionalFormProps> = ({
|
||||||
onSubmit,
|
onSubmit,
|
||||||
onCancel,
|
onCancel,
|
||||||
|
isLoading = false,
|
||||||
}) => {
|
}) => {
|
||||||
const [functions, setFunctions] = useState<
|
const [functions, setFunctions] = useState<
|
||||||
Array<{ id: string; nome: string }>
|
Array<{ id: string; nome: string }>
|
||||||
|
|
@ -173,6 +175,7 @@ export const ProfessionalForm: React.FC<ProfessionalFormProps> = ({
|
||||||
|
|
||||||
const handleSubmit = (e: React.FormEvent) => {
|
const handleSubmit = (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
if (isLoading) return;
|
||||||
|
|
||||||
// Validação de foto de perfil
|
// Validação de foto de perfil
|
||||||
if (!formData.avatar) {
|
if (!formData.avatar) {
|
||||||
|
|
@ -720,17 +723,12 @@ export const ProfessionalForm: React.FC<ProfessionalFormProps> = ({
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Botões */}
|
{/* Botões */}
|
||||||
<div className="flex gap-4 pt-4">
|
<div className="flex justify-end gap-3 pt-6 border-t font-medium">
|
||||||
<Button
|
<Button type="button" variant="outline" onClick={onCancel} disabled={isLoading}>
|
||||||
type="button"
|
|
||||||
variant="outline"
|
|
||||||
onClick={onCancel}
|
|
||||||
className="flex-1"
|
|
||||||
>
|
|
||||||
Cancelar
|
Cancelar
|
||||||
</Button>
|
</Button>
|
||||||
<Button type="submit" className="flex-1">
|
<Button type="submit" disabled={isLoading}>
|
||||||
Cadastrar Profissional
|
{isLoading ? "Cadastrando..." : "Cadastrar Profissional"}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
|
|
@ -262,8 +262,8 @@ const login = async (email: string, password?: string) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const register = async (data: any) => {
|
const register = async (data: any, autoLogin: boolean = true, skipLoading: boolean = false) => {
|
||||||
setIsLoading(true);
|
if (!skipLoading) setIsLoading(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const API_URL = import.meta.env.VITE_API_URL || "http://localhost:8080";
|
const API_URL = import.meta.env.VITE_API_URL || "http://localhost:8080";
|
||||||
|
|
@ -305,10 +305,14 @@ const login = async (email: string, password?: string) => {
|
||||||
const responseData = await response.json();
|
const responseData = await response.json();
|
||||||
|
|
||||||
if (responseData.user && responseData.user.ativo) {
|
if (responseData.user && responseData.user.ativo) {
|
||||||
|
|
||||||
|
if (autoLogin) {
|
||||||
if (responseData.access_token) {
|
if (responseData.access_token) {
|
||||||
localStorage.setItem('token', responseData.access_token);
|
localStorage.setItem('token', responseData.access_token);
|
||||||
setToken(responseData.access_token);
|
setToken(responseData.access_token);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const backendUser = responseData.user;
|
const backendUser = responseData.user;
|
||||||
const mappedUser: User = {
|
const mappedUser: User = {
|
||||||
|
|
@ -333,8 +337,10 @@ const login = async (email: string, password?: string) => {
|
||||||
cidade: backendUser.cidade,
|
cidade: backendUser.cidade,
|
||||||
estado: backendUser.estado,
|
estado: backendUser.estado,
|
||||||
};
|
};
|
||||||
|
if (autoLogin) {
|
||||||
setUser(mappedUser);
|
setUser(mappedUser);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
|
|
@ -348,7 +354,7 @@ const login = async (email: string, password?: string) => {
|
||||||
}
|
}
|
||||||
return { success: false, error: "Erro desconhecido" };
|
return { success: false, error: "Erro desconhecido" };
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
if (!skipLoading) setIsLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
import {
|
import {
|
||||||
ProfessionalForm,
|
ProfessionalForm,
|
||||||
ProfessionalData,
|
ProfessionalData,
|
||||||
|
|
@ -13,12 +14,15 @@ interface ProfessionalRegisterProps {
|
||||||
export const ProfessionalRegister: React.FC<ProfessionalRegisterProps> = ({
|
export const ProfessionalRegister: React.FC<ProfessionalRegisterProps> = ({
|
||||||
onNavigate,
|
onNavigate,
|
||||||
}) => {
|
}) => {
|
||||||
|
const navigate = useNavigate();
|
||||||
const { register } = useAuth();
|
const { register } = useAuth();
|
||||||
const [isSuccess, setIsSuccess] = useState(false);
|
const [isSuccess, setIsSuccess] = useState(false);
|
||||||
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
|
|
||||||
// Removed access code logic as per requirement
|
// Removed access code logic as per requirement
|
||||||
|
|
||||||
const handleSubmit = async (professionalData: ProfessionalData) => {
|
const handleSubmit = async (professionalData: ProfessionalData) => {
|
||||||
|
setIsSubmitting(true);
|
||||||
try {
|
try {
|
||||||
// 1. Cadastrar Usuário (Auth) e Logar Automaticamente
|
// 1. Cadastrar Usuário (Auth) e Logar Automaticamente
|
||||||
const authResult = await register({
|
const authResult = await register({
|
||||||
|
|
@ -29,7 +33,7 @@ export const ProfessionalRegister: React.FC<ProfessionalRegisterProps> = ({
|
||||||
role: professionalData.funcaoLabel?.toUpperCase().includes("PESQUISA") ? "RESEARCHER" : "PHOTOGRAPHER",
|
role: professionalData.funcaoLabel?.toUpperCase().includes("PESQUISA") ? "RESEARCHER" : "PHOTOGRAPHER",
|
||||||
tipo_profissional: professionalData.funcaoLabel || "", // Envia o nome da função (ex: Cinegrafista)
|
tipo_profissional: professionalData.funcaoLabel || "", // Envia o nome da função (ex: Cinegrafista)
|
||||||
regiao: professionalData.regiao, // Pass region
|
regiao: professionalData.regiao, // Pass region
|
||||||
});
|
}, false, true); // autoLogin = false, skipLoading = true to prevent global loading & form flash
|
||||||
|
|
||||||
if (!authResult.success) {
|
if (!authResult.success) {
|
||||||
throw new Error("Falha no cadastro de usuário.");
|
throw new Error("Falha no cadastro de usuário.");
|
||||||
|
|
@ -111,12 +115,12 @@ export const ProfessionalRegister: React.FC<ProfessionalRegisterProps> = ({
|
||||||
|
|
||||||
console.log("Profissional cadastrado com sucesso!");
|
console.log("Profissional cadastrado com sucesso!");
|
||||||
// Limpar dados de sessão após cadastro bem-sucedido
|
// Limpar dados de sessão após cadastro bem-sucedido
|
||||||
sessionStorage.removeItem('professionalAccessValidated');
|
// Agora feito na página de sucesso
|
||||||
sessionStorage.removeItem('professionalAccessData');
|
navigate("/cadastro-sucesso", { replace: true });
|
||||||
setIsSuccess(true);
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error("Erro ao cadastrar profissional:", error);
|
console.error("Erro ao cadastrar profissional:", error);
|
||||||
alert(error.message || "Erro ao cadastrar profissional. Tente novamente.");
|
alert(error.message || "Erro ao cadastrar profissional. Tente novamente.");
|
||||||
|
setIsSubmitting(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -173,6 +177,8 @@ export const ProfessionalRegister: React.FC<ProfessionalRegisterProps> = ({
|
||||||
<ProfessionalForm
|
<ProfessionalForm
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
onCancel={() => onNavigate("cadastro")}
|
onCancel={() => onNavigate("cadastro")}
|
||||||
|
onNavigate={onNavigate}
|
||||||
|
isLoading={isSubmitting}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
import { Button } from "../components/Button";
|
import { Button } from "../components/Button";
|
||||||
import { Input } from "../components/Input";
|
import { Input } from "../components/Input";
|
||||||
import { useAuth } from "../contexts/AuthContext";
|
import { useAuth } from "../contexts/AuthContext";
|
||||||
|
|
@ -10,6 +11,7 @@ interface RegisterProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Register: React.FC<RegisterProps> = ({ onNavigate }) => {
|
export const Register: React.FC<RegisterProps> = ({ onNavigate }) => {
|
||||||
|
const navigate = useNavigate();
|
||||||
const { register } = useAuth();
|
const { register } = useAuth();
|
||||||
const [companies, setCompanies] = useState<
|
const [companies, setCompanies] = useState<
|
||||||
Array<{ id: string; nome: string }>
|
Array<{ id: string; nome: string }>
|
||||||
|
|
@ -107,10 +109,12 @@ export const Register: React.FC<RegisterProps> = ({ onNavigate }) => {
|
||||||
complemento: formData.complemento,
|
complemento: formData.complemento,
|
||||||
bairro: formData.bairro,
|
bairro: formData.bairro,
|
||||||
cidade: formData.cidade,
|
cidade: formData.cidade,
|
||||||
|
|
||||||
estado: formData.estado,
|
estado: formData.estado,
|
||||||
});
|
}, false, true); // autoLogin=false, skipLoading=true
|
||||||
setIsLoading(false);
|
|
||||||
window.location.href = "/cadastro-sucesso";
|
// Keep loading true while redirecting
|
||||||
|
navigate("/cadastro-sucesso", { replace: true });
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
setError(err.message || "Erro ao realizar cadastro");
|
setError(err.message || "Erro ao realizar cadastro");
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ export const RegistrationSuccess: React.FC = () => {
|
||||||
// Clear session data to lock /cadastro again
|
// Clear session data to lock /cadastro again
|
||||||
sessionStorage.removeItem('accessCodeValidated');
|
sessionStorage.removeItem('accessCodeValidated');
|
||||||
sessionStorage.removeItem('accessCodeData');
|
sessionStorage.removeItem('accessCodeData');
|
||||||
|
sessionStorage.removeItem('professionalAccessValidated');
|
||||||
|
sessionStorage.removeItem('professionalAccessData');
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue