1 | import 'bootstrap/dist/css/bootstrap.min.css';
|
---|
2 | import React, {useContext, useEffect, useState} from "react";
|
---|
3 | import axios from "axios";
|
---|
4 | import { Link } from 'react-router-dom';
|
---|
5 | import { useNavigate } from 'react-router-dom';
|
---|
6 | import {CustomerContext} from "./CustomerContext";
|
---|
7 |
|
---|
8 |
|
---|
9 | const Customers = () => {
|
---|
10 | const [customers, setCustomers] = useState([]);
|
---|
11 | const navigate = useNavigate();
|
---|
12 | const customersContext = useContext(CustomerContext);
|
---|
13 |
|
---|
14 | useEffect(() => {
|
---|
15 | setCustomers(customersContext.customers)
|
---|
16 | }, [customersContext]);
|
---|
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:8081/api/customers/delete/${customerId}`);
|
---|
29 | setCustomers(customers.filter(customer => customer.customerID !== customerId));
|
---|
30 | window.location.reload();
|
---|
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.firstName + " " + customer.lastName}</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 Level:</strong> {customer.membershipLevel}</p>
|
---|
51 | {/*<p className="card-text"><strong>Registration*/}
|
---|
52 | {/* Date:</strong> {new Date(customer.registrationDate).toLocaleString()}</p>*/}
|
---|
53 | </div>
|
---|
54 | <button onClick={() => handleDetailClick(customer.customerId)}>View Details</button>
|
---|
55 | <button onClick={() => handleEditClick(customer.customerId)}>Edit</button>
|
---|
56 | <button onClick={() => handleDeleteClick(customer.customerId)}>DELETE</button>
|
---|
57 | </div>
|
---|
58 | </div>
|
---|
59 | ))}
|
---|
60 | </div>
|
---|
61 | </div>
|
---|
62 | );
|
---|
63 |
|
---|
64 | }
|
---|
65 |
|
---|
66 | export default Customers; |
---|