diff --git a/src/pages/customers/CustomerDetail.jsx b/src/pages/customers/CustomerDetail.jsx index e9079cb..ab37c0c 100644 --- a/src/pages/customers/CustomerDetail.jsx +++ b/src/pages/customers/CustomerDetail.jsx @@ -1,27 +1,58 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { useParams, Link, useNavigate } from 'react-router-dom'; import StatusBadge from '../../components/StatusBadge'; +import { API_BASE_URL } from '../../config'; +import { useAuth } from '../../context/AuthContext'; const CustomerDetail = () => { const { id } = useParams(); + const { token } = useAuth(); const navigate = useNavigate(); - const [customer, setCustomer] = useState({ - id: id, - name: 'Amit Kumar', - email: 'amit@gmail.com', - phone: '+91 99887 76655', - status: 'active', - registered: '2026-03-04', - address: 'Flat 301, Silver Heights, Sector 15, Gurgaon, Haryana - 122001', - orders: [ - { id: 'ORD88761', date: '2026-08-10', total: '₹2,499', paymentStatus: 'completed', trackingStatus: 'delivered', txId: 'TXN_PHPE_88761234' }, - { id: 'ORD88120', date: '2026-08-01', total: '₹1,150', paymentStatus: 'completed', trackingStatus: 'returned', txId: 'TXN_PHPE_88120456' } - ] - }); + const [customer, setCustomer] = useState(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); - const handleStatusChange = (newStatus) => { - setCustomer({ ...customer, status: newStatus }); - alert(`Customer account status changed to ${newStatus}`); + const fetchCustomerData = async () => { + try { + setLoading(true); + const res = await fetch(`${API_BASE_URL}/customers/${id}/`, { + headers: { 'Authorization': `Bearer ${token}` } + }); + if (!res.ok) throw new Error('Failed to fetch customer detail'); + const data = await res.json(); + + // Fallback: If Django database doesn't have orders relationship, initialize empty array + if (!data.orders) { + data.orders = []; + } + setCustomer(data); + } catch (err) { + setError(err.message); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + fetchCustomerData(); + }, [id, token]); + + const handleAction = async (actionPath, payload = {}) => { + try { + const res = await fetch(`${API_BASE_URL}/customers/${id}/${actionPath}/`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` + }, + body: JSON.stringify(payload) + }); + if (!res.ok) throw new Error(`Operation failed`); + alert(`Customer status updated!`); + fetchCustomerData(); // reload + } catch (err) { + alert(`Error: ${err.message}`); + } }; const handleCheckPaymentStatus = (orderId, txnId) => { @@ -36,6 +67,10 @@ const CustomerDetail = () => { navigate(`/orders/return?orderId=${orderId}&address=${encodeURIComponent(customer.address)}`); }; + if (loading) return
Customer ID: #{customer.id} • Registered: {customer.registered}
+Customer ID: #{customer.id}
| Order ID | -Date | -Total | -Payment | -Delivery | -Actions | -|||||
|---|---|---|---|---|---|---|---|---|---|---|
| {order.id} | -{order.date} | -{order.total} | -
-
-
-
- {order.trackingStatus === 'delivered' && (
-
- )}
-
- |
+ {customer.orders && customer.orders.length > 0 ? (
+
| Order ID | +Total | +Payment | +Delivery | +Actions |
|---|
No order history logs found for this user.
+ )}| Customer ID | -Name | -Phone | -Status | -Registered | -Actions | -|||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| #{cust.id} | -{cust.name} | -{cust.email} | -{cust.phone} | -{cust.registered} | -- - 👁️ View Detail - - | + {loading ? ( +
| Customer ID | +Name | +Phone | +Status | +Actions |
|---|
Seller ID: {seller.id} • Joined: {seller.joined}
+Seller ID: {seller.id} • Joined: {seller.joined_at ? new Date(seller.joined_at).toLocaleDateString() : 'N/A'}
| Seller Name | -Status | -Joined Date | -Total Sales | -Actions | -||||||
|---|---|---|---|---|---|---|---|---|---|---|
| {seller.name} | -{seller.email} | -
- |
- {seller.joined} | -{seller.revenue} | -- - 👁️ View Details - - | + {loading ? ( +
| Seller Name | +Status | +Joined Date | +Actions |
|---|