|
@@ -1,10 +1,10 @@
|
|
|
import React from "react";
|
|
|
import { useAlert } from "./useAlert";
|
|
|
import { useHistory } from "react-router";
|
|
|
-import { httpCodes } from "../constants";
|
|
|
+import { httpStatusCodes } from "../constants";
|
|
|
// import { auth } from "../services";
|
|
|
import { useApp } from "./useApp";
|
|
|
-import { useHttpProps, innerFetch} from '../types'
|
|
|
+import { useHttpProps, innerFetch, useHttpResponse } from "../types";
|
|
|
|
|
|
const { REACT_APP_API_URL: baseUrl } = process.env;
|
|
|
|
|
@@ -36,18 +36,23 @@ export function useHttp({
|
|
|
params = null,
|
|
|
body = null,
|
|
|
triggerAlert = false,
|
|
|
-}: useHttpProps) {
|
|
|
+}: useHttpProps): [
|
|
|
+ useHttpResponse | null,
|
|
|
+ boolean,
|
|
|
+ string | null,
|
|
|
+ (showAlert: any, inlineParams?: any) => Promise<void>
|
|
|
+] {
|
|
|
const { showAlert } = useAlert();
|
|
|
const { token } = useApp();
|
|
|
- const [response, setResponse] = React.useState(null);
|
|
|
- const [error, setError] = React.useState(null);
|
|
|
+ const [response, setResponse] = React.useState<useHttpResponse | null>(null);
|
|
|
+ const [error, setError] = React.useState<string | null>(null);
|
|
|
const [loading, setLoading] = React.useState(true);
|
|
|
const history = useHistory();
|
|
|
|
|
|
const refresh = React.useCallback(
|
|
|
async (showAlert, inlineParams = {}) => {
|
|
|
try {
|
|
|
- if (!url || !params) {
|
|
|
+ if (!url || !params || !req) {
|
|
|
setResponse(null);
|
|
|
setError(null);
|
|
|
setLoading(true);
|
|
@@ -58,17 +63,13 @@ export function useHttp({
|
|
|
} else {
|
|
|
setLoading(() => true);
|
|
|
}
|
|
|
- // let token = null;
|
|
|
- // if (auth.currentUser) {
|
|
|
- // token = await auth.currentUser.getIdToken();
|
|
|
- // }
|
|
|
const fetchReq: innerFetch = {
|
|
|
method: req,
|
|
|
headers: makeHeaders(token),
|
|
|
- body: null
|
|
|
+ body: null,
|
|
|
};
|
|
|
if (body) {
|
|
|
- const serializedBody = JSON.stringify(body);
|
|
|
+ const serializedBody = JSON.stringify(body);
|
|
|
fetchReq.body = serializedBody;
|
|
|
}
|
|
|
const paramsFinal = { ...params, ...inlineParams };
|
|
@@ -80,7 +81,7 @@ export function useHttp({
|
|
|
const httpRes = await fetch(str, fetchReq);
|
|
|
const resBody = await httpRes.json();
|
|
|
switch (httpRes.status) {
|
|
|
- case httpCodes.OK:
|
|
|
+ case httpStatusCodes.default.OK:
|
|
|
setResponse(resBody);
|
|
|
setError(null);
|
|
|
triggerAlert &&
|
|
@@ -91,7 +92,7 @@ export function useHttp({
|
|
|
: "Solicitud completada correctamente!",
|
|
|
});
|
|
|
break;
|
|
|
- case httpCodes.BAD_REQUEST:
|
|
|
+ case httpStatusCodes.default.BAD_REQUEST:
|
|
|
window["scrollTo"]({ top: 0, behavior: "smooth" });
|
|
|
setError(resBody.errores);
|
|
|
triggerAlert &&
|
|
@@ -102,10 +103,10 @@ export function useHttp({
|
|
|
: "Datos erróneos o inválidos.",
|
|
|
});
|
|
|
break;
|
|
|
- case httpCodes.UNAUTHORIZED:
|
|
|
+ case httpStatusCodes.default.UNAUTHORIZED:
|
|
|
history.push("/no-autorizado");
|
|
|
break;
|
|
|
- case httpCodes.SERVER_ERROR:
|
|
|
+ case httpStatusCodes.default.INTERNAL_SERVER_ERROR:
|
|
|
default:
|
|
|
triggerAlert &&
|
|
|
showAlert({
|