Refatorando código e correções
This commit is contained in:
17
src/App.tsx
17
src/App.tsx
@@ -39,6 +39,8 @@ import { search, home, person } from 'ionicons/icons';
|
|||||||
import { useState, useContext, useEffect } from 'react';
|
import { useState, useContext, useEffect } from 'react';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
|
import sessionsService from './services/functions/sessionsService'
|
||||||
|
|
||||||
setupIonicReact();
|
setupIonicReact();
|
||||||
|
|
||||||
const routes = (
|
const routes = (
|
||||||
@@ -69,8 +71,19 @@ const IonicApp: React.FC = () => {
|
|||||||
user.setIsLoggedIn = setIsLoggedIn;
|
user.setIsLoggedIn = setIsLoggedIn;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// TODO, verifica se usuário está logado
|
const verifyAuthenticatedUser = async () => {
|
||||||
// fazer com serviço externo (evita duplicações)
|
const refreshSessionRes = await sessionsService.refreshSession()
|
||||||
|
|
||||||
|
if (refreshSessionRes.error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (refreshSessionRes.userId) {
|
||||||
|
setIsLoggedIn(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
verifyAuthenticatedUser()
|
||||||
})
|
})
|
||||||
|
|
||||||
return(
|
return(
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import {
|
|||||||
IonContent,
|
IonContent,
|
||||||
IonFab,
|
IonFab,
|
||||||
IonFabButton,
|
IonFabButton,
|
||||||
IonGrid,
|
|
||||||
IonHeader,
|
IonHeader,
|
||||||
IonIcon,
|
IonIcon,
|
||||||
IonItem,
|
IonItem,
|
||||||
@@ -24,12 +23,11 @@ import React, { useState, useEffect, useReducer } from "react";
|
|||||||
import { IonRow, IonCol } from "@ionic/react";
|
import { IonRow, IonCol } from "@ionic/react";
|
||||||
import { createOutline } from "ionicons/icons";
|
import { createOutline } from "ionicons/icons";
|
||||||
|
|
||||||
import * as sessionRoutes from '../services/api/session';
|
|
||||||
import * as usersRoutes from '../services/api/users';
|
|
||||||
|
|
||||||
import './Perfil.css'
|
import './Perfil.css'
|
||||||
import LocalStorage from "../LocalStorage";
|
import LocalStorage from "../LocalStorage";
|
||||||
import { refreshSession } from "../services/refreshSession";
|
|
||||||
|
import sessionsService from '../services/functions/sessionsService'
|
||||||
|
import usersService from '../services/functions/usersService'
|
||||||
|
|
||||||
const Perfil: React.FC = () => {
|
const Perfil: React.FC = () => {
|
||||||
const [showToast, setShowToast] = useState(false);
|
const [showToast, setShowToast] = useState(false);
|
||||||
@@ -48,50 +46,56 @@ const Perfil: React.FC = () => {
|
|||||||
|
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
|
const redirectUserToLogin = () => {
|
||||||
|
// TODO, não impede o usuário de retornar a página de login
|
||||||
|
history.push({ pathname: '/login' });
|
||||||
|
setMessageToast("Por favor, autentique-se!");
|
||||||
|
setShowToast(true);
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const redirectUserToLogin = () => {
|
|
||||||
// TODO, não impede o usuário de retornar a página de login
|
|
||||||
history.push({ pathname: '/login' });
|
|
||||||
setMessageToast("Por favor, autentique-se!");
|
|
||||||
setShowToast(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
const loadUserData = async () => {
|
const loadUserData = async () => {
|
||||||
let userId = ''
|
let userId = ''
|
||||||
|
|
||||||
// verify if user is authenticated
|
// verify if user is authenticated
|
||||||
const refreshSessionRes = await refreshSession()
|
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 usersRoutes.getById(userId)
|
const getByIdRes = await usersService.getById(userId)
|
||||||
|
|
||||||
if (getByIdRes.error) {
|
if (getByIdRes.error) {
|
||||||
setMessageToast(getByIdRes.message.data)
|
setMessageToast(getByIdRes.error.errorMessage)
|
||||||
setShowToast(true)
|
setShowToast(true)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const userData = getByIdRes.data
|
if (getByIdRes.userData) {
|
||||||
|
const userData = getByIdRes.userData
|
||||||
setInputValues({
|
|
||||||
'name': userData.name,
|
if (isMounted) {
|
||||||
'lastname': userData.lastname,
|
setInputValues({
|
||||||
'email': userData.email,
|
'name': userData.name,
|
||||||
'birth_date': userData.birth_date,
|
'lastname': userData.lastname,
|
||||||
'bio': userData.bio
|
'email': userData.email,
|
||||||
});
|
'birth_date': userData.birth_date,
|
||||||
|
'bio': userData.bio
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let isMounted = true;
|
||||||
|
|
||||||
const userToken = LocalStorage.getToken()
|
const userToken = LocalStorage.getToken()
|
||||||
|
|
||||||
if (!userToken) {
|
if (!userToken) {
|
||||||
@@ -99,7 +103,10 @@ const Perfil: React.FC = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadUserData()
|
loadUserData()
|
||||||
}, [history]);
|
|
||||||
|
return () => { isMounted = false };
|
||||||
|
}, []);
|
||||||
|
// }, [history]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<IonPage>
|
<IonPage>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import * as sessionRoutes from "../services/api/session";
|
import * as sessionRoutes from "../api/session";
|
||||||
|
|
||||||
interface refreshSessionReturn {
|
interface refreshSessionReturn {
|
||||||
userId?: string;
|
userId?: string;
|
||||||
@@ -12,7 +12,7 @@ interface refreshSessionResponse {
|
|||||||
userId?: string;
|
userId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const refreshSession = async (): Promise<refreshSessionReturn> => {
|
const refreshSession = async (): Promise<refreshSessionReturn> => {
|
||||||
try {
|
try {
|
||||||
let res: refreshSessionResponse = await sessionRoutes.refresh()
|
let res: refreshSessionResponse = await sessionRoutes.refresh()
|
||||||
|
|
||||||
@@ -42,3 +42,5 @@ export const refreshSession = async (): Promise<refreshSessionReturn> => {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default { refreshSession }
|
||||||
53
src/services/functions/usersService.ts
Normal file
53
src/services/functions/usersService.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import * as usersRoutes from "../api/users";
|
||||||
|
|
||||||
|
interface getByIdReturn {
|
||||||
|
userData?: {
|
||||||
|
name: string;
|
||||||
|
lastname: string;
|
||||||
|
email: string;
|
||||||
|
birth_date: string;
|
||||||
|
bio: string;
|
||||||
|
},
|
||||||
|
error?: {
|
||||||
|
errorMessage: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface getByIdRes {
|
||||||
|
status: string;
|
||||||
|
message: string;
|
||||||
|
userId?: string;
|
||||||
|
data: {
|
||||||
|
name: string;
|
||||||
|
lastname: string;
|
||||||
|
email: string;
|
||||||
|
birth_date: string;
|
||||||
|
bio: string;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const getById = async (userId: string): Promise<getByIdReturn> => {
|
||||||
|
try {
|
||||||
|
let res: getByIdRes = await usersRoutes.getById(userId)
|
||||||
|
|
||||||
|
if (res.status === "error") {
|
||||||
|
return {
|
||||||
|
error: {
|
||||||
|
errorMessage: res.message,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
userData: res.data,
|
||||||
|
};
|
||||||
|
} catch(err) {
|
||||||
|
return {
|
||||||
|
error: {
|
||||||
|
errorMessage: "Por favor, autentique-se.",
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default { getById }
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
import * as usersRoutes from "../../services/api/users";
|
|
||||||
|
|
||||||
interface getByIdReturn {
|
|
||||||
userId?: string;
|
|
||||||
error?: boolean;
|
|
||||||
errorMessage?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface getByIdResponse {
|
|
||||||
status?: string;
|
|
||||||
message?: string;
|
|
||||||
userId?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getById = async (userId: string): Promise<getByIdReturn> => {
|
|
||||||
try {
|
|
||||||
let res: getByIdResponse = await usersRoutes.getById(userId)
|
|
||||||
|
|
||||||
if (res.status === "error") {
|
|
||||||
return {
|
|
||||||
error: true,
|
|
||||||
errorMessage: res.message,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
userId: res.userId,
|
|
||||||
};
|
|
||||||
} catch(err) {
|
|
||||||
return {
|
|
||||||
error: true,
|
|
||||||
errorMessage: "Por favor, autentique-se.",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user