Tela de perfil exibe outro usuário corretamente

This commit is contained in:
Matheus Albino Brunhara
2022-05-28 17:37:55 -05:00
parent 4c6aacfd12
commit b5d6f8ef34
3 changed files with 24 additions and 14 deletions

View File

@@ -50,7 +50,7 @@ const routes = (
<Route exact path="/home" component={Home}></Route> <Route exact path="/home" component={Home}></Route>
<Route exact path="/perfil" component={Perfil}></Route> <Route exact path="/perfil" component={Perfil}></Route>
<Route exact path="/perfil/editar" component={PerfilEditar}></Route> <Route exact path="/perfil/editar" component={PerfilEditar}></Route>
<Route exact path="/perfil/:id" component={Perfil}></Route> <Route exact path="/usuario/:id" component={Perfil}></Route>
<Route exact path="/cadastro-van" component={CadastroVan}></Route> <Route exact path="/cadastro-van" component={CadastroVan}></Route>
<Route exact path="/"> <Route exact path="/">
<Redirect to="/login" /> <Redirect to="/login" />

View File

@@ -8,7 +8,7 @@ const Home: React.FC = () => {
return ( return (
<IonPage> <IonPage>
<IonContent> <IonContent>
<IonButton onClick={() => { history.push({ pathname: '/perfil/1' }); }}>Ir para o perfil de outra pessoa</IonButton> <IonButton onClick={() => { history.push({ pathname: '/usuario/56520ae7-faf8-4444-a82b-7f3990ab02d8' }); }}>Ir para o perfil de outra pessoa</IonButton>
</IonContent> </IonContent>
</IonPage> </IonPage>
); );

View File

@@ -66,31 +66,41 @@ const Perfil: React.FC<ScanNewProps> = (props) => {
const logoff = () => { const logoff = () => {
LocalStorage.clearToken() LocalStorage.clearToken()
user.setIsLoggedIn(false); user.setIsLoggedIn(false);
history.push('/login') history.push({ pathname: '/login' });
} }
useEffect(() => { useEffect(() => {
const loadUserData = async () => { const loadUserData = async () => {
let userId = '' let userId = ''
// verify if user is authenticated // verify if user is authenticated
const refreshSessionRes = await sessionsService.refreshSession() if (props.match.params.id) {
userId = props.match.params.id
} else {
const refreshSessionRes = await sessionsService.refreshSession()
if (refreshSessionRes.error) { if (refreshSessionRes.error) {
redirectUserToLogin() redirectUserToLogin()
return return
} }
if (refreshSessionRes.userId) { if (refreshSessionRes.userId) {
userId = refreshSessionRes.userId userId = refreshSessionRes.userId
}
} }
// get user info by ID // get user info by ID
const getByIdRes = await usersService.getById(userId) const getByIdRes = await usersService.getById(userId)
if (getByIdRes.error) { if (getByIdRes.error) {
setMessageToast(getByIdRes.error.errorMessage) if (isVisitor && props.match.params.id) {
setShowToast(true) setMessageToast('Usuário não existe!')
setShowToast(true)
history.push({ pathname: '/home' })
} else {
setMessageToast(getByIdRes.error.errorMessage)
setShowToast(true)
}
return return
} }