Created
August 3, 2021 14:07
-
-
Save dueka/ad66b7ed0ad418c9047a332b23659a89 to your computer and use it in GitHub Desktop.
custom hook for retrieving investment details
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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