Skip to content

Instantly share code, notes, and snippets.

@dueka
Created August 3, 2021 14:07
Show Gist options
  • Save dueka/ad66b7ed0ad418c9047a332b23659a89 to your computer and use it in GitHub Desktop.
Save dueka/ad66b7ed0ad418c9047a332b23659a89 to your computer and use it in GitHub Desktop.
custom hook for retrieving investment details
export const useInvestmentDetails = (id) => {
const [currentInvestment, setCurrentInvestment] = useState([]);
const token = selectToken;
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const fetchInvestmentDetails = useCallback(() => {
const fetcInvestmentData = async () => {
const req = async () => {
try {
setLoading(true);
const { data } = await axiosWithAuth(token).get(
`api/v1/treasury/investments/${id}?include=source, beneficiary`
);
setCurrentInvestment(data.data.investment);
} catch ({ response }) {
setError(response);
if (error.response.status === 401) {
logout();
}
} finally {
setLoading(false);
}
};
req();
};
fetcInvestmentData();
}, [id, token]);
useEffect(() => {
fetchInvestmentDetails();
}, [fetchInvestmentDetails]);
return [currentInvestment, fetchInvestmentDetails, loading, error];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment