Correção perfil

This commit is contained in:
Matheus Albino Brunhara
2022-05-28 11:14:52 -05:00
parent a810fd066a
commit e79cb353b2
2 changed files with 63 additions and 26 deletions

View File

@@ -33,7 +33,7 @@ import { refreshSession } from "../services/refreshSession";
const Perfil: React.FC = () => {
const [showToast, setShowToast] = useState(false);
const [messageToast, setMessageToast ] = useState('');
const [messageToast, setMessageToast] = useState('');
const [inputValues, setInputValues] = useReducer(
(state: any, newState: any) => ({ ...state, ...newState }),
@@ -60,33 +60,36 @@ const Perfil: React.FC = () => {
let userId = ''
// verify if user is authenticated
const refreshedSession = await refreshSession()
const refreshSessionRes = await refreshSession()
if (refreshedSession.error) {
if (refreshSessionRes.error) {
redirectUserToLogin()
return
}
await usersRoutes.getById(userId).then(response => {
if (response.status === 'error') {
setMessageToast(response.message.data)
setShowToast(true);
return
}
const userData = response.data
setInputValues({
'name': userData.name,
'lastname': userData.lastname,
'email': userData.email,
'birth_date': userData.birth_date,
'bio': userData.bio
});
}).catch(() => {
redirectUserToLogin()
})
if (refreshSessionRes.userId) {
userId = refreshSessionRes.userId
}
// get user info by ID
const getByIdRes = await usersRoutes.getById(userId)
if (getByIdRes.error) {
setMessageToast(getByIdRes.message.data)
setShowToast(true)
return
}
const userData = getByIdRes.data
setInputValues({
'name': userData.name,
'lastname': userData.lastname,
'email': userData.email,
'birth_date': userData.birth_date,
'bio': userData.bio
});
}
const userToken = LocalStorage.getToken()
@@ -94,8 +97,7 @@ const Perfil: React.FC = () => {
if (!userToken) {
redirectUserToLogin()
}
// const refreshResponse = refreshSession()
loadUserData()
}, [history]);

View File

@@ -0,0 +1,35 @@
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.",
};
}
};