Skip to content

Commit

Permalink
Create useApi.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent b10c78a commit 532faa1
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions projects/PiWalletBot/hooks/useApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { useState, useEffect } from 'react';
import axios from 'axios';

const useApi = () => {
const [api, setApi] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);

useEffect(() => {
const createApi = async () => {
try {
const response = await axios.get('/api/config');
const apiConfig = response.data;
const apiInstance = axios.create({
baseURL: apiConfig.baseURL,
headers: {
'Content-Type': 'application/json',
},
});
setApi(apiInstance);
} catch (error) {
setError(error);
} finally {
setLoading(false);
}
};
createApi();
}, []);

const get = async (endpoint, params) => {
try {
setLoading(true);
const response = await api.get(endpoint, { params });
return response.data;
} catch (error) {
setError(error);
return null;
} finally {
setLoading(false);
}
};

const post = async (endpoint, data) => {
try {
setLoading(true);
const response = await api.post(endpoint, data);
return response.data;
} catch (error) {
setError(error);
return null;
} finally {
setLoading(false);
}
};

return { api, loading, error, get, post };
};

export default useApi;

0 comments on commit 532faa1

Please sign in to comment.