import { IonBadge, IonCard, IonCardContent, IonCardHeader, IonCardTitle, IonChip, IonContent, IonIcon, IonItem, IonLabel, IonList, IonListHeader, IonPage, IonToast, } from "@ionic/react"; import { callOutline, cardOutline, carOutline, createOutline, exitOutline, mapOutline, personOutline, shieldCheckmarkOutline, starOutline, } from "ionicons/icons"; import React, { useContext, useEffect, useReducer, useState } from "react"; import { useHistory, useLocation } from "react-router-dom"; import LocalStorage from "../LocalStorage"; import "./Perfil.css"; import { Color } from "@ionic/core"; import { UserContext } from "../App"; import { PageHeader } from "../components/PageHeader"; import sessionsService from "../services/functions/sessionsService"; import { checkIfUserIsDriver, getById, } from "../services/functions/usersService"; import { closeToast } from "../services/utils"; interface ScanNewProps { match: { params: { id: string; }; }; } interface LocationState { redirectData?: { showToastMessage: boolean; toastColor: Color; toastMessage: string; }; } const Perfil: React.FC = (props) => { const user = useContext(UserContext); const history = useHistory(); const location = useLocation(); const [isVisitor, setIsVisitor] = useState(true); const [isDriver, setIsDriver] = useState(false); const [incompleteProfile, setIncompleteProfile] = useState(false); const [incompleteProfileCounter, setIncompleteProfileCounter] = useState(0); const [showToast, setShowToast] = useState(false); const [toastMessage, setToastMessage] = useState(""); const [toastColor, setToastColor] = useState("primary"); const [inputValues, setInputValues] = useReducer( (state: any, newState: any) => ({ ...state, ...newState }), { id: "", name: "", lastname: "", email: "", phone_number: "", birth_date: "", bio: "", document_type: "", document: "", } ); const redirectUserToLogin = () => { history.push({ pathname: "/login" }); setToastMessage("Por favor, autentique-se!"); setShowToast(true); }; const logoff = () => { LocalStorage.clearToken(); user.setIsLoggedIn(false); history.push({ pathname: "/login" }); }; useEffect(() => { if (location.state && location.state.redirectData) { const redirectData = location.state.redirectData; if (redirectData.showToastMessage) { setToastColor(redirectData.toastColor); setToastMessage(redirectData.toastMessage); setShowToast(true); } } const loadUserData = async () => { let userId = ""; // verify if user is authenticated if (props.match.params.id) { userId = props.match.params.id; } else { const refreshSessionRes = await sessionsService.refreshSession(); if (refreshSessionRes.error) { redirectUserToLogin(); return; } if (refreshSessionRes.userId) { userId = refreshSessionRes.userId; } } // get user info by ID const getByIdRes = await getById(userId); if (getByIdRes.error) { if (isVisitor && props.match.params.id) { setToastMessage("Usuário não existe!"); setShowToast(true); history.push({ pathname: "/home" }); } else { setToastMessage(getByIdRes.error.errorMessage); setShowToast(true); } return; } // check if user is driver (if they have vans) const userIsDriverRes = await checkIfUserIsDriver(userId); // if (userIsDriverRes.error) { // setToastColor('warning') // setToastMessage(userIsDriverRes.error.errorMessage) // setShowToast(true) // return // } if (!userIsDriverRes.error && userIsDriverRes.result !== undefined) { setIsDriver(userIsDriverRes.result); } if (getByIdRes.userData) { const userData = getByIdRes.userData; if (isMounted) { setInputValues({ id: userId, name: userData.name, lastname: userData.lastname, email: userData.email, phone_number: userData.phone_number, birth_date: userData.birth_date, bio: userData.bio, document_type: userData.document_type, document: userData.document, }); if (!props.match.params.id) { setIsVisitor(false); } if (!userData.document || !userData.phone_number) { setIncompleteProfile(true); let counter = 0; if (!userData.document) counter++; if (!userData.phone_number) counter++; setIncompleteProfileCounter(counter); } } } }; let isMounted = true; const userToken = LocalStorage.getToken(); if (!userToken) { redirectUserToLogin(); } loadUserData(); return () => { isMounted = false; }; }, []); return ( avatar {/* avatar */} {inputValues.name} {inputValues.lastname}
{isDriver ? ( <> Motorista ) : ( <> )} Passageiro
Biografia {inputValues.bio ? inputValues.bio : "Sem biografia."} Informações de contato {!inputValues.phone_number ? ( <>Sem informações de contato. ) : ( <> {inputValues.phone_number ? ( <> {inputValues.phone_number} ) : ( <> )} )} {!isVisitor ? ( Configurações history.push({ pathname: "/perfil/editar", state: { userData: inputValues }, }) } > Editar perfil {incompleteProfile ? ( <> history.push({ pathname: "/perfil/completar", state: { userData: inputValues }, }) } > Completar cadastro {incompleteProfileCounter} ) : ( <> )} history.push({ pathname: "/veiculos/cadastrar" })} > Cadastrar veículo {isDriver ? ( <> history.push({ pathname: "/veiculos/meus" })} > Meus veículos history.push({ pathname: "/buscar-passageiro" }) } > Buscar passageiros history.push({ pathname: "/cadastrar-itinerario" }) } > Cadastrar itinerário history.push({ pathname: "/meus-itinerarios" }) } > Meus itinerários ) : ( <> )} Pagamentos Avaliações Sair da conta ) : ( <> )} closeToast(setShowToast)} message={toastMessage} duration={2500} />
); }; export default Perfil;