162 lines
4.0 KiB
JavaScript
162 lines
4.0 KiB
JavaScript
import { useState, useEffect } from "react";
|
|
import { PrivateRoutes } from "../models/routes";
|
|
|
|
const API_URL = process.env.REACT_APP_API_URL;
|
|
|
|
export const useAdscripcion = () => {
|
|
const [adscripciones, setAdscripciones] = useState([]);
|
|
|
|
useEffect(() => {
|
|
fetch(`${API_URL}/adscripcion`)
|
|
.then((response) => response.json())
|
|
.then((data) => setAdscripciones(data))
|
|
.catch((error) => console.error("Error fetching adscripciones:", error));
|
|
}, []);
|
|
|
|
return adscripciones;
|
|
};
|
|
|
|
export const useEdificio = () => {
|
|
const [edificios, setEdificios] = useState([]);
|
|
|
|
useEffect(() => {
|
|
fetch(`${API_URL}/edificio`)
|
|
.then((response) => response.json())
|
|
.then((data) => setEdificios(data))
|
|
.catch((error) => console.error("Error fetching edificios:", error));
|
|
}, []);
|
|
|
|
return edificios;
|
|
};
|
|
|
|
export const useCategoria = () => {
|
|
const [categorias, setCategorias] = useState([]);
|
|
|
|
useEffect(() => {
|
|
fetch(`${API_URL}/categoria`)
|
|
.then((response) => response.json())
|
|
.then((data) => setCategorias(data))
|
|
.catch((error) => console.error("Error fetching categorias:", error));
|
|
}, []);
|
|
|
|
return categorias;
|
|
};
|
|
|
|
export const useToken = () => {
|
|
const token = localStorage.getItem("Token");
|
|
if (!token) {
|
|
return;
|
|
}
|
|
|
|
return token;
|
|
};
|
|
|
|
export const useUploadImage = () => {
|
|
const token = useToken();
|
|
|
|
const uploadImage = async (base64Image, imageName) => {
|
|
try {
|
|
const response = await fetch(`${API_URL}/imagen`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
body: JSON.stringify({
|
|
fotografia: base64Image,
|
|
nombre: imageName,
|
|
}),
|
|
});
|
|
|
|
if (response.status === 413) {
|
|
throw new Error("Imagen demasiado pesada");
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error("Error al subir la imagen");
|
|
}
|
|
|
|
const result = await response.json();
|
|
return `${API_URL}${result.imageUrl}`;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
return { uploadImage };
|
|
};
|
|
|
|
export const useUpdateImage = () => {
|
|
const token = useToken();
|
|
|
|
const updateImage = async (base64Image, imageName) => {
|
|
try {
|
|
const response = await fetch(`${API_URL}/imagen`, {
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
body: JSON.stringify({
|
|
fotografia: base64Image,
|
|
nombre: imageName,
|
|
}),
|
|
});
|
|
|
|
if (response.status === 413) {
|
|
throw new Error("Imagen demasiado pesada");
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error("Error al subir la imagen");
|
|
}
|
|
|
|
const result = await response.json();
|
|
return `${API_URL}${result.imageUrl}`;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
return { updateImage };
|
|
};
|
|
|
|
export const useLogin = () => {
|
|
const [error, setError] = useState(null);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const login = async (usuario, contraseña, rememberMe) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await fetch(`${API_URL}/auth/login`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ usuario, contraseña, rememberMe }),
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
setError("Usuario o contraseña inválido.");
|
|
} else if (response.status === 500) {
|
|
setError("Error con la conexión de la API.");
|
|
} else if (!response.ok) {
|
|
setError("Error en el inicio de sesión");
|
|
} else {
|
|
const data = await response.json();
|
|
localStorage.setItem("Token", data.token);
|
|
window.location.href = PrivateRoutes.INTERFACE;
|
|
}
|
|
} catch (error) {
|
|
console.error("Error:", error);
|
|
setError("Error en el inicio de sesión");
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return { login, error, loading };
|
|
};
|