optimized custom hook usage
This commit is contained in:
parent
d25aa108f1
commit
932d1e7117
@ -16,7 +16,7 @@ import ConfirmModal from "../common/ConfirmModal";
|
||||
|
||||
const Teams = ({ project }) => {
|
||||
const dispatch = useDispatch();
|
||||
dispatch(changeMaster("Job Role"));
|
||||
|
||||
const { data, loading } = useMaster();
|
||||
const [isModalOpen, setIsModelOpen] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
@ -43,11 +43,9 @@ const Teams = ({ project }) => {
|
||||
.then((response) => {
|
||||
setEmployees(response.data);
|
||||
setFilteredEmployees( response.data.filter( ( emp ) => emp.isActive ) );
|
||||
console.log(response)
|
||||
setEmployeeLoading(false);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
setError("Failed to fetch data.");
|
||||
setEmployeeLoading(false);
|
||||
});
|
||||
@ -133,6 +131,11 @@ const Teams = ({ project }) => {
|
||||
document.body.style.overflow = "auto";
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(changeMaster("Job Role"));
|
||||
}, [dispatch]);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
fetchEmployees();
|
||||
}, []);
|
||||
|
@ -3,46 +3,40 @@ 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 hasFetched = false;
|
||||
|
||||
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)
|
||||
setProfile(response.data);
|
||||
cacheProfileData(response.data);
|
||||
} catch (error) {
|
||||
setError("Failed to fetch data.");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!hasFetched) {
|
||||
hasFetched = true;
|
||||
if (!loggedUser) {
|
||||
fetchData();
|
||||
} else {
|
||||
setProfile(loggedUser);
|
||||
}
|
||||
}
|
||||
|
||||
},[])
|
||||
|
||||
return { profile,loading,error}
|
||||
|
||||
}
|
||||
setProfile(loggedUser);
|
||||
|
||||
}, [loggedUser]);
|
||||
|
||||
return { profile, loading, error };
|
||||
};
|
||||
|
@ -2,17 +2,18 @@ import { useEffect, useState } from "react";
|
||||
import { cacheData, getCachedData } from "../slices/apiDataManager";
|
||||
import ProjectRepository from "../repositories/ProjectRepository";
|
||||
import { useProfile } from "./useProfile";
|
||||
import { useDispatch } from "react-redux";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { setProjectId } from "../slices/localVariablesSlice";
|
||||
|
||||
export const useProjects = () => {
|
||||
const { profile } = useProfile();
|
||||
|
||||
const loggedUser = useSelector( ( store ) => store.globalVariables.loginUser )
|
||||
const [projects, setProjects] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
const fetchData = async () => {
|
||||
const projectIds = profile?.projects || [];
|
||||
const projectIds = loggedUser?.projects || [];
|
||||
|
||||
const filterProjects = (projectsList) => {
|
||||
return projectsList
|
||||
@ -43,12 +44,11 @@ export const useProjects = () => {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (profile) {
|
||||
if (loggedUser) {
|
||||
fetchData();
|
||||
}
|
||||
}, [profile]);
|
||||
}, [loggedUser]);
|
||||
|
||||
return { projects, loading, error, refetch: fetchData };
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user