Ignore:
Timestamp:
01/19/25 23:18:37 (4 months ago)
Author:
Aleksandar Panovski <apano77@…>
Branches:
main
Children:
f5b256e
Parents:
db39d9e
Message:

Done with stupid timeslots

File:
1 edited

Legend:

Unmodified
Added
Removed
  • my-react-app/src/components/ReservationConfirmation.js

    rdb39d9e r8ca35dc  
    33import axios from 'axios';
    44import { useNavigate } from 'react-router-dom';
     5import {jwtDecode} from "jwt-decode";
    56
    67const ReservationConfirmation = () => {
     
    1718        const fetchTableDetails = async () => {
    1819            try {
    19                 const tableResponse = await axios.get(`http://localhost:8080/api/tables/${tableNumber}`);
     20                const tableResponse = await axios.get(`http://localhost:8081/api/tables/${tableNumber}`);
    2021                setTable(tableResponse.data);
    2122
    22                 const restaurantResponse = await axios.get(`http://localhost:8080/api/restaurants/${restaurantId}`);
     23                const restaurantResponse = await axios.get(`http://localhost:8081/api/restaurants/${restaurantId}`);
    2324                setRestaurant(restaurantResponse.data);
    2425            } catch (error) {
     
    3132    const handleSubmit = async (e) => {
    3233        e.preventDefault();
    33         // Handle form submission here
     34
     35        if (!restaurant || !table) {
     36            console.error("Restaurant or table is missing.");
     37            return;
     38        }
     39
    3440        try {
    35             // Check if restaurant and table are valid
    36             if (!restaurant || !table) {
    37                 console.error("Restaurant or table is missing.");
     41            const token = localStorage.getItem("token");
     42            if (!token) {
     43                console.error("No token found");
    3844                return;
    3945            }
    4046
    41             const response = await axios.post(`http://localhost:8080/api/reservations`, {
    42                 restaurant: restaurant, // Assuming restaurant has an 'id' property
    43                 table: table, // Assuming table has an 'id' property
    44                 checkInTime: timeSlot, // Fill in as needed
    45                 partySize: partySize,
    46                 specialRequests: specialRequests
    47             });
    48             // Handle successful reservation creation
    49             // console.log("Reservation created:", response.data);
    50             navigate("/reservations")
     47            // Decode the token to get the user email
     48            const decodedToken = jwtDecode(token);
     49            console.log(decodedToken)
     50            const userId = decodedToken.iss;// Assuming the email is part of the decoded JWT token
     51
     52            const response = await axios.post(
     53                `http://localhost:8081/api/reservations/${userId}`,
     54                {
     55                    restaurant: restaurant,
     56                    table: table,
     57                    checkInTime: timeSlot,
     58                    partySize: partySize,
     59                    specialRequests: specialRequests
     60                },
     61                {
     62                    headers: {
     63                        Authorization: `Bearer ${token}` // Include the token here
     64                    }
     65                }
     66            );
     67
     68            navigate("/reservations");
    5169        } catch (error) {
    5270            console.error("Error creating reservation:", error);
Note: See TracChangeset for help on using the changeset viewer.