From 727b043507403da803b2d610e23d1360c4208b0e Mon Sep 17 00:00:00 2001 From: Felipe Date: Fri, 15 Dec 2023 13:57:23 -0300 Subject: [PATCH] Fixed user null issues for delte myreservations and reserve --- src/components/pages/DeleteBoat.jsx | 77 +++++++---- src/components/pages/MyReservations.jsx | 97 ++++++++------ src/components/pages/Reserve.jsx | 169 +++++++++++++----------- 3 files changed, 196 insertions(+), 147 deletions(-) diff --git a/src/components/pages/DeleteBoat.jsx b/src/components/pages/DeleteBoat.jsx index 4a3da72..e78efbd 100644 --- a/src/components/pages/DeleteBoat.jsx +++ b/src/components/pages/DeleteBoat.jsx @@ -1,11 +1,13 @@ import React, { useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { deleteBoat, fetchBoats, selectAllBoats } from '../../redux/boats/boatsSlice'; +import { selectUser } from '../../redux/usersession/usersessionsSlice'; function DeleteBoat() { const dispatch = useDispatch(); const boats = useSelector(selectAllBoats); const currentItems = Array.isArray(boats); + const user = useSelector(selectUser); useEffect(() => { dispatch(fetchBoats()); @@ -17,32 +19,57 @@ function DeleteBoat() { }; return ( -
-
-

- Delete Boats -

-

- Please click on Delete button to delete a boat -

-

********************

-
- {currentItems - && boats.map((boat) => ( -
-

{boat.name}

- -
- ))} -
-
-
+

{boat.name}

+ {boat.user_id === user.id ? ( + + ) : ( + + )} + + ))} + + + + )} + ); } diff --git a/src/components/pages/MyReservations.jsx b/src/components/pages/MyReservations.jsx index bfd1d36..175eba6 100644 --- a/src/components/pages/MyReservations.jsx +++ b/src/components/pages/MyReservations.jsx @@ -8,7 +8,7 @@ function MyReservations() { const { reservations } = useSelector((state) => state.reservations); const user = useSelector(selectUser); const dispatch = useDispatch(); - const userReservations = reservations.filter((e) => e.username === user.name); + const userReservations = reservations.filter((e) => e.username === user?.name); const boats = useSelector(selectAllBoats); console.log(boats); @@ -18,49 +18,60 @@ function MyReservations() { }, [dispatch]); return ( -
-
-

- My reservations -

-

Here you can see all your reservations.

-
- {userReservations.map((re) => ( -
- - Reservation # - {re.id} - -

- Hello - {' '} - {re.username} - ! - You reserved - {' '} - {boats.find(({ id }) => id === re.boat_id).name} - . -

-

- Reservation details -

-

- Date: - {' '} - {re.date} - . -

-

- City: - {' '} - {re.city} - . -

+ <> + {user === null ? ( +
+
+

Acces Denied

+

Please Log In first so you can see all your Reservations.

+
+
+ ) : ( +
+
+

+ My reservations +

+

Here you can see all your reservations.

+
+ {userReservations.map((re) => ( +
+ + Reservation # + {re.id} + +

+ Hello + {' '} + {re.username} + ! + You reserved + {' '} + {boats.find(({ id }) => id === re.boat_id).name} + . +

+

+ Reservation details +

+

+ Date: + {' '} + {re.date} + . +

+

+ City: + {' '} + {re.city} + . +

+
+ ))}
- ))} -
-
-
+ + + )} + ); } diff --git a/src/components/pages/Reserve.jsx b/src/components/pages/Reserve.jsx index 0a00584..15985a2 100644 --- a/src/components/pages/Reserve.jsx +++ b/src/components/pages/Reserve.jsx @@ -7,7 +7,7 @@ import { createReservation } from '../../redux/reservations/reservationsSlice'; function Reserve() { const [boats, setBoats] = useState([]); const user = useSelector(selectUser); - const [username, setUsername] = useState(user.name); + const [username, setUsername] = useState(user); const [city, setCity] = useState(''); const [date, setDate] = useState(''); const [boatId, setBoatId] = useState(undefined); @@ -46,84 +46,95 @@ function Reserve() { }, []); return ( -
-
-

- Reserve a Boat Trip -

-

- Complete All fields to reserve a boat trip. -

-
-
-
- { - setUsername(e.target.value); - }} - /> - { - setCity(e.target.value); - }} - /> - { - setDate(e.target.value); - }} - /> - -
-
- -
-
-
-
-
+ <> + {user === null ? ( +
+
+

Acces Denied

+

Please Log In first so you can reserve a boat.

+
+
+ ) : ( +
+
+

+ Reserve a Boat Trip +

+

+ Complete All fields to reserve a boat trip. +

+
+
+
+ { + setUsername(e.target.value); + }} + /> + { + setCity(e.target.value); + }} + /> + { + setDate(e.target.value); + }} + /> + +
+
+ +
+
+
+
+
+ )} + ); }