[d24f17c] | 1 | import 'bootstrap/dist/css/bootstrap.min.css';
|
---|
| 2 | import React, {useEffect, useState} from "react";
|
---|
| 3 | import axios from "axios";
|
---|
| 4 | import { Link } from 'react-router-dom';
|
---|
| 5 | import { useNavigate } from 'react-router-dom';
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | const Customers = () => {
|
---|
| 9 | const [customers, setCustomers] = useState([]);
|
---|
| 10 | const navigate = useNavigate();
|
---|
| 11 |
|
---|
| 12 | useEffect(() => {
|
---|
| 13 | const fetchCustomers = async () => {
|
---|
| 14 | try {
|
---|
| 15 | const response = await axios.get("http://localhost:8080/api/customers");
|
---|
| 16 | setCustomers(response.data);
|
---|
| 17 | } catch (error) {
|
---|
| 18 | console.error("Error fetching customers: ", error);
|
---|
| 19 | }
|
---|
| 20 | };
|
---|
| 21 |
|
---|
| 22 | fetchCustomers()
|
---|
| 23 | }, []);
|
---|
| 24 |
|
---|
| 25 | const handleDetailClick = (customerId) => {
|
---|
| 26 | navigate(`/customers/${customerId}`);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | const handleEditClick = (customerId) => {
|
---|
| 30 | navigate(`/customers/edit/${customerId}`);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | const handleDeleteClick = async (customerId) => {
|
---|
| 34 | try {
|
---|
| 35 | await axios.delete(`http://localhost:8080/api/customers/delete/${customerId}`);
|
---|
| 36 | setCustomers(customers.filter(customer => customer.customerID !== customerId));
|
---|
| 37 | alert('Reservation canceled successfully');
|
---|
| 38 | } catch (error) {
|
---|
| 39 | console.error("Error + " + error);
|
---|
| 40 | alert("An error occurred while deleting");
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | return (
|
---|
| 45 | <div className="container mt-4">
|
---|
| 46 | <h1 className="mb-4">Customer List</h1>
|
---|
| 47 | <div className="row row-cols-1 row-cols-md-3 g-4">
|
---|
| 48 | {customers
|
---|
| 49 | .filter(customer => customer.role !== 'admin')
|
---|
| 50 | .map((customer) => (
|
---|
| 51 | <div className="col" key={customer.customerId}>
|
---|
| 52 | <div className="card h-100">
|
---|
| 53 | <div className="card-body">
|
---|
| 54 | <h5 className="card-title">{customer.fullName}</h5>
|
---|
| 55 | <p className="card-text"><strong>Email:</strong> {customer.email}</p>
|
---|
| 56 | <p className="card-text"><strong>Phone:</strong> {customer.phone}</p>
|
---|
| 57 | <p className="card-text"><strong>Address:</strong> {customer.address}</p>
|
---|
| 58 | <p className="card-text"><strong>Membership
|
---|
| 59 | Level:</strong> {customer.membershipLevel}</p>
|
---|
| 60 | <p className="card-text"><strong>Registration
|
---|
| 61 | Date:</strong> {new Date(customer.registrationDate).toLocaleString()}</p>
|
---|
| 62 | </div>
|
---|
| 63 | <button onClick={() => handleDetailClick(customer.customerId)}>View Details</button>
|
---|
| 64 | <button onClick={() => handleEditClick(customer.customerId)}>Edit</button>
|
---|
| 65 | <button onClick={() => handleDeleteClick(customer.customerId)}>DELETE</button>
|
---|
| 66 | </div>
|
---|
| 67 | </div>
|
---|
| 68 | ))}
|
---|
| 69 | </div>
|
---|
| 70 | </div>
|
---|
| 71 | );
|
---|
| 72 |
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | export default Customers; |
---|