Correção perfil
This commit is contained in:
@@ -33,7 +33,7 @@ import { refreshSession } from "../services/refreshSession";
|
|||||||
|
|
||||||
const Perfil: React.FC = () => {
|
const Perfil: React.FC = () => {
|
||||||
const [showToast, setShowToast] = useState(false);
|
const [showToast, setShowToast] = useState(false);
|
||||||
const [messageToast, setMessageToast ] = useState('');
|
const [messageToast, setMessageToast] = useState('');
|
||||||
|
|
||||||
const [inputValues, setInputValues] = useReducer(
|
const [inputValues, setInputValues] = useReducer(
|
||||||
(state: any, newState: any) => ({ ...state, ...newState }),
|
(state: any, newState: any) => ({ ...state, ...newState }),
|
||||||
@@ -60,33 +60,36 @@ const Perfil: React.FC = () => {
|
|||||||
let userId = ''
|
let userId = ''
|
||||||
|
|
||||||
// verify if user is authenticated
|
// verify if user is authenticated
|
||||||
const refreshedSession = await refreshSession()
|
const refreshSessionRes = await refreshSession()
|
||||||
|
|
||||||
if (refreshedSession.error) {
|
if (refreshSessionRes.error) {
|
||||||
redirectUserToLogin()
|
redirectUserToLogin()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
await usersRoutes.getById(userId).then(response => {
|
if (refreshSessionRes.userId) {
|
||||||
if (response.status === 'error') {
|
userId = refreshSessionRes.userId
|
||||||
setMessageToast(response.message.data)
|
}
|
||||||
setShowToast(true);
|
|
||||||
|
// get user info by ID
|
||||||
return
|
const getByIdRes = await usersRoutes.getById(userId)
|
||||||
}
|
|
||||||
|
if (getByIdRes.error) {
|
||||||
const userData = response.data
|
setMessageToast(getByIdRes.message.data)
|
||||||
|
setShowToast(true)
|
||||||
setInputValues({
|
|
||||||
'name': userData.name,
|
return
|
||||||
'lastname': userData.lastname,
|
}
|
||||||
'email': userData.email,
|
|
||||||
'birth_date': userData.birth_date,
|
const userData = getByIdRes.data
|
||||||
'bio': userData.bio
|
|
||||||
});
|
setInputValues({
|
||||||
}).catch(() => {
|
'name': userData.name,
|
||||||
redirectUserToLogin()
|
'lastname': userData.lastname,
|
||||||
})
|
'email': userData.email,
|
||||||
|
'birth_date': userData.birth_date,
|
||||||
|
'bio': userData.bio
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const userToken = LocalStorage.getToken()
|
const userToken = LocalStorage.getToken()
|
||||||
@@ -94,8 +97,7 @@ const Perfil: React.FC = () => {
|
|||||||
if (!userToken) {
|
if (!userToken) {
|
||||||
redirectUserToLogin()
|
redirectUserToLogin()
|
||||||
}
|
}
|
||||||
|
|
||||||
// const refreshResponse = refreshSession()
|
|
||||||
loadUserData()
|
loadUserData()
|
||||||
}, [history]);
|
}, [history]);
|
||||||
|
|
||||||
|
|||||||
35
src/services/users/getById.ts
Normal file
35
src/services/users/getById.ts
Normal 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.",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user