48 lines
1.0 KiB
JavaScript
48 lines
1.0 KiB
JavaScript
import {useState,useEffect} from "react";
|
|
import AuthRepository from "../repositories/AuthRepository";
|
|
import {cacheProfileData, getCachedProfileData} from "../slices/apiDataManager";
|
|
import {useSelector} from "react-redux";
|
|
|
|
export const useProfile = () =>
|
|
{
|
|
const loggedUser = useSelector((store)=>store.globalVariables.loginUser)
|
|
|
|
const [profile, setProfile] = useState(null);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
|
|
const fetchData = async () =>
|
|
{
|
|
try
|
|
{
|
|
setLoading( true )
|
|
let response = await AuthRepository.profile();
|
|
setProfile( response.data )
|
|
cacheProfileData( response.data )
|
|
setLoading( false );
|
|
|
|
} catch ( error )
|
|
{
|
|
setLoading( false )
|
|
console.error( error );
|
|
setError( "Failed to fetch data." );
|
|
};
|
|
}
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if (!loggedUser )
|
|
{
|
|
fetchData()
|
|
} else
|
|
{
|
|
setProfile(loggedUser)
|
|
}
|
|
|
|
},[])
|
|
|
|
return { profile,loading,error}
|
|
|
|
} |