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