Skip to content

Commit

Permalink
Merge branch '30-Reservas-Seleccionar-fechas'
Browse files Browse the repository at this point in the history
  • Loading branch information
fantonifranco committed Dec 6, 2023
2 parents baefdfb + e922e95 commit 9e0e75e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
26 changes: 22 additions & 4 deletions Front/src/Components/utils/ProtectedRoute.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import { Navigate } from 'react-router-dom';
import React from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import PropTypes from 'prop-types';
import NotFound from '../../Routes/NotFound';

const ProtectedRoute = ({ children, onlyAdmin }) => {
const isAuthenticated = Boolean(localStorage.getItem('userData'));

const location = useLocation();

const currentPath = location.pathname;

const isReservationDetailRoute = currentPath === '/detailReservation';
const isFavsRoute = currentPath === '/profile/favs';

const getRedirectState = () => {
if (isReservationDetailRoute && !isAuthenticated) {
return { fromReserve: true };
} else if (isFavsRoute && !isAuthenticated) {
return { fromFavButton: true };
} else {
return { fromProtectedRoute: true };
}
};

if (!isAuthenticated) {
return <Navigate to="/login" replace state={{ fromProtectedRoute: true }} />;
return <Navigate to="/login" replace state={getRedirectState()} />;
}

const userData = JSON.parse(localStorage.getItem('userData'));
const isAdmin = userData.tipo == "ROLE_ADMIN" ? true : false;

if (onlyAdmin && !isAdmin) {
return <NotFound />;
}

return children;
};

Expand All @@ -25,3 +42,4 @@ ProtectedRoute.propTypes = {
}

export default ProtectedRoute;

12 changes: 5 additions & 7 deletions Front/src/Routes/Detail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const Detail = () => {
]);
};

// const onClickReserve = () => {
const onClickReserve = () => {
// dataReserve.id = id
// dataReserve.titulo = result.titulo
// dataReserve.subtitulo = result.subtitulo
Expand All @@ -121,8 +121,8 @@ const Detail = () => {
// dataReserve.menores = numberOfChildren

// // localStorage.setItem('datosReserva', JSON.stringify(dataReserve));
// // window.location.href = '/reserve';
// };
window.location.href = '/detailReservation';
};

return (
<main>
Expand Down Expand Up @@ -341,10 +341,8 @@ const Detail = () => {
</p>
</div>
<div className="row">
{/* <button className="btn" type="button" onClick={onClickReserve}>Completar reserva</button> */}
<button className="btn" type="button">
Completar reserva
</button>
{<button className="btn" type="button" onClick={onClickReserve}>Completar reserva</button>}
{/* <button className="btn" type="button">Completar reserva</button> */}
</div>
</div>
</div>
Expand Down
15 changes: 14 additions & 1 deletion Front/src/Routes/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const Login = () => {
});
}

if (location.state?.fromReserve) {
Swal.fire({
title: 'Autenticación requerida',
text: 'El login es obligatorio para completar una reserva. Por favor, registrese.',
icon: 'info',
confirmButtonText: 'Ok'
});
}

if (location.state?.fromFavButton) {
Swal.fire({
title: 'Autenticación requerida',
Expand Down Expand Up @@ -113,7 +122,11 @@ const Login = () => {
tipo: "success",
texto: "Inicio de sesión exitoso.",
});
navigate("/");
if (location.state?.fromReserve) {
navigate("/detailReservation");
} else {
navigate("/");
}
} else {
// Error en el inicio de sesión
setMensaje({
Expand Down
20 changes: 10 additions & 10 deletions Front/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import ProtectedRoute from "./Components/utils/ProtectedRoute";
// Toast notification
import { ToastContainer } from "react-toastify";
import Profile from "./Routes/Profile";
import Reservation from "./Routes/Reservation";

createRoot(document.getElementById("root")).render(
<Context>
<BrowserRouter>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<Home />} />
<Route path="tour/:id" element={<Detail />} />
<Route path="tour" element={<AddTour />} />
{<Route path="tour/:id" element={<Detail />} />}
<Route path="sign-up" element={<SignUp />} />
<Route path="login" element={<Login />} />

Expand All @@ -33,13 +33,14 @@ createRoot(document.getElementById("root")).render(
<Profile />
</ProtectedRoute>
} />

<Route path="reservation" element={
<ProtectedRoute onlyAdmin={false}>
<Reservation />
</ProtectedRoute>
} />

<Route
path="detailReservation"
element={
<ProtectedRoute>
{/* { <Detail /> } */}
</ProtectedRoute>
}
/>
<Route path="profile/favs" element={
<ProtectedRoute onlyAdmin={false}>
<Favs />
Expand Down Expand Up @@ -69,7 +70,6 @@ createRoot(document.getElementById("root")).render(
{/* <List /> */}
</ProtectedRoute>
} />

<Route path="*" element={<NotFound />} />
</Route>
</Routes>
Expand Down
7 changes: 7 additions & 0 deletions reporteErrores.log
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@
[2023-11-20 19:00:31] [ ERROR] [ExceptionController:21] IllegalArgumentException: No enum constant com.viajecito.api.model.TourDificultad.Baja
[2023-11-20 19:07:42] [ ERROR] [ExceptionController:21] NoSuchElementException: No value present
[2023-11-20 20:03:38] [ ERROR] [ExceptionController:21] MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "NaN"
[2023-12-02 18:34:52] [ ERROR] [ExceptionController:21] MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.List'; nested exception is java.lang.NumberFormatException: For input string: "Septiembre-Febrero"
[2023-12-02 18:35:00] [ ERROR] [ExceptionController:21] MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.List'; nested exception is java.lang.NumberFormatException: For input string: "Septiembre-Febrero"
[2023-12-02 18:39:05] [ ERROR] [ExceptionController:21] NullPointerException: Cannot invoke "com.viajecito.api.model.Salida.getActivo()" because "salida" is null
[2023-12-02 18:41:24] [ ERROR] [ExceptionController:21] NullPointerException: Cannot invoke "com.viajecito.api.model.Salida.getActivo()" because "salida" is null
[2023-12-02 19:47:03] [ ERROR] [ExceptionController:21] NullPointerException: Cannot invoke "com.viajecito.api.model.Salida.getActivo()" because "salida" is null
[2023-12-05 09:19:49] [ ERROR] [ExceptionController:21] MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "undefined"
[2023-12-05 09:20:14] [ ERROR] [ExceptionController:21] MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "undefined"

0 comments on commit 9e0e75e

Please sign in to comment.