From a97db9a6617add89ffcccd01dc1f92f35f30c341 Mon Sep 17 00:00:00 2001 From: victorAnumudu Date: Wed, 1 May 2024 17:33:41 +0100 Subject: [PATCH] added get user by ID API --- src/core/apiRequest.ts | 11 ++++++++- src/core/axiosCall.ts | 23 +++++++++++++++++++ src/core/models.ts | 2 +- src/layouts/DashboardLayout/DashboardAuth.tsx | 19 ++++++++++----- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/core/apiRequest.ts b/src/core/apiRequest.ts index 0cb41ba..a9d7071 100644 --- a/src/core/apiRequest.ts +++ b/src/core/apiRequest.ts @@ -1,4 +1,4 @@ -import { postAuxEnd } from "./axiosCall"; +import { postAuxEnd, getAuxEnd } from "./axiosCall"; // FUNCTION TO START BVN VALIDATION export const validateBVN = (postData:any) => { @@ -24,4 +24,13 @@ export const applyForLoan = (postData:any) => { ...postData } return postAuxEnd('/loan/apply', reqData) +} + + +// FUNCTION TO GET USER BY CUSTOMER UID +export const getUserByID = () => { + let reqData = { + customer_uid: localStorage.getItem('uid'), + } + return getAuxEnd(`/profile?uid=${reqData.customer_uid}`, reqData) } \ No newline at end of file diff --git a/src/core/axiosCall.ts b/src/core/axiosCall.ts index 4031b01..67e0043 100644 --- a/src/core/axiosCall.ts +++ b/src/core/axiosCall.ts @@ -56,3 +56,26 @@ export function postAuxEnd(uri: string, reqData: any): Promise { } }); } + + +export function getAuxEnd(uri: string, reqData: any): Promise { + const endPoint = import.meta.env.VITE_USERS_ENDPOINT + uri; + const formData = new FormData(); + for (let value in reqData) { + formData.append(value, reqData[value]); + } + return axios + .get(endPoint, reqData) + .then((response: {}) => { + // if (response.data.internal_return == "-9999") { + // localStorage.clear(); + // window.location.href = `/login?sessionExpired=true`; + // } + return response; + }) + .catch((error: any) => { + console.log( + "ERROR3-------------------------------------------------------", error + ); + }); +} diff --git a/src/core/models.ts b/src/core/models.ts index 397ea5c..a0da8dd 100644 --- a/src/core/models.ts +++ b/src/core/models.ts @@ -13,6 +13,6 @@ export interface User { last_login?:string message?:string token?:string - uid?:string + customer_uid?:string call_return?:string } \ No newline at end of file diff --git a/src/layouts/DashboardLayout/DashboardAuth.tsx b/src/layouts/DashboardLayout/DashboardAuth.tsx index 2255169..1923bd0 100644 --- a/src/layouts/DashboardLayout/DashboardAuth.tsx +++ b/src/layouts/DashboardLayout/DashboardAuth.tsx @@ -5,6 +5,7 @@ import { Outlet, useNavigate } from "react-router-dom"; import { useSelector, useDispatch } from "react-redux"; import { RouteHandler } from '../../router/routes'; import { updateUserDetails } from '../../store/UserDetails'; +import { getUserByID } from '../../core/apiRequest'; import Logo from '../../assets/images/logo.png' @@ -23,15 +24,21 @@ export default function DashboardAuth() { navigate(RouteHandler.letsGetStarted, {replace:true}) return } - const getUserByToken = () => { - let data = {firstname:'firstname', lastname:'lastname', uid:'28273737646466464'} - setTimeout(()=>{ + const getUser = () => { // FUNCTION TO GET USER BY ID + // let data = {firstname:'firstname', lastname:'lastname', uid:'28273737646466464'} + getUserByID().then(res=>{ + if(!res.data.call_return || !Object.keys(res.data.customer).length){ + navigate(RouteHandler.letsGetStarted, {replace:true}) + return + } setLoading(false) - dispatch(updateUserDetails({...data})); - },4000) + dispatch(updateUserDetails(res.data.customer)); + }).catch(err=>{ + console.log('USER ERROR', err) + }) } if(!Object.keys(userDetails).length){ - getUserByToken() + getUser() } },[]) -- 2.34.1