|
@@ -4,6 +4,7 @@ import { useHistory } from "react-router";
|
|
|
import { httpCodes } from "../constants";
|
|
|
// import { auth } from "../services";
|
|
|
import { useApp } from "./useApp";
|
|
|
+import { useHttpProps, innerFetch} from '../types'
|
|
|
|
|
|
const { REACT_APP_API_URL: baseUrl } = process.env;
|
|
|
|
|
@@ -12,7 +13,7 @@ const defaultHeaders = {
|
|
|
Accept: "application/json",
|
|
|
};
|
|
|
|
|
|
-const makeHeaders = (token) =>
|
|
|
+const makeHeaders = (token: string | null) =>
|
|
|
token
|
|
|
? {
|
|
|
...defaultHeaders,
|
|
@@ -20,22 +21,22 @@ const makeHeaders = (token) =>
|
|
|
}
|
|
|
: defaultHeaders;
|
|
|
|
|
|
-const paramsToQuery = (params) =>
|
|
|
+const paramsToQuery = (params: any) =>
|
|
|
Object.keys(params)
|
|
|
.map(
|
|
|
(key) => encodeURIComponent(key) + "=" + encodeURIComponent(params[key])
|
|
|
)
|
|
|
.join("&");
|
|
|
|
|
|
-const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
|
+const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
|
|
|
|
export function useHttp({
|
|
|
req = "GET",
|
|
|
- url = null,
|
|
|
+ url = "",
|
|
|
params = null,
|
|
|
body = null,
|
|
|
- alert = false,
|
|
|
-}) {
|
|
|
+ triggerAlert = false,
|
|
|
+}: useHttpProps) {
|
|
|
const { showAlert } = useAlert();
|
|
|
const { token } = useApp();
|
|
|
const [response, setResponse] = React.useState(null);
|
|
@@ -61,12 +62,14 @@ export function useHttp({
|
|
|
// if (auth.currentUser) {
|
|
|
// token = await auth.currentUser.getIdToken();
|
|
|
// }
|
|
|
- let fetchReq = {
|
|
|
+ const fetchReq: innerFetch = {
|
|
|
method: req,
|
|
|
headers: makeHeaders(token),
|
|
|
+ body: null
|
|
|
};
|
|
|
if (body) {
|
|
|
- fetchReq = { ...fetchReq, body: JSON.stringify(body) };
|
|
|
+ const serializedBody = JSON.stringify(body);
|
|
|
+ fetchReq.body = serializedBody;
|
|
|
}
|
|
|
const paramsFinal = { ...params, ...inlineParams };
|
|
|
const str = `${baseUrl}${url}${
|
|
@@ -80,7 +83,7 @@ export function useHttp({
|
|
|
case httpCodes.OK:
|
|
|
setResponse(resBody);
|
|
|
setError(null);
|
|
|
- alert &&
|
|
|
+ triggerAlert &&
|
|
|
showAlert({
|
|
|
severity: "success",
|
|
|
message: resBody.mensaje
|
|
@@ -91,7 +94,7 @@ export function useHttp({
|
|
|
case httpCodes.BAD_REQUEST:
|
|
|
window["scrollTo"]({ top: 0, behavior: "smooth" });
|
|
|
setError(resBody.errores);
|
|
|
- alert &&
|
|
|
+ triggerAlert &&
|
|
|
showAlert({
|
|
|
severity: "warning",
|
|
|
message: resBody.mensaje
|
|
@@ -104,7 +107,7 @@ export function useHttp({
|
|
|
break;
|
|
|
case httpCodes.SERVER_ERROR:
|
|
|
default:
|
|
|
- alert &&
|
|
|
+ triggerAlert &&
|
|
|
showAlert({
|
|
|
severity: "error",
|
|
|
message: resBody.mensaje
|
|
@@ -113,7 +116,7 @@ export function useHttp({
|
|
|
});
|
|
|
}
|
|
|
} catch (error) {
|
|
|
- alert &&
|
|
|
+ triggerAlert &&
|
|
|
showAlert({
|
|
|
severity: "error",
|
|
|
message: "No se pudo establecer conexión con el servidor.",
|
|
@@ -123,7 +126,7 @@ export function useHttp({
|
|
|
setLoading(false);
|
|
|
}
|
|
|
},
|
|
|
- [body, params, req, url, alert, history, token]
|
|
|
+ [body, params, req, url, triggerAlert, history, token]
|
|
|
);
|
|
|
|
|
|
React.useEffect(() => {
|