Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 0b37f0e

Browse files
committed
tria
1 parent 9694602 commit 0b37f0e

1 file changed

Lines changed: 72 additions & 20 deletions

File tree

client/src/views/pages/LegalPanel/complains.js

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,106 @@ import axiosInstance from '../../../utils/axiosInstance';
33

44
const Complains = () => {
55
const [complaints, setComplaints] = useState([]);
6+
const [loading, setLoading] = useState(false);
67

8+
// Fetch complaints on component mount only
79
useEffect(() => {
810
fetchComplains();
911
}, []);
1012

1113
const fetchComplains = async () => {
1214
try {
15+
setLoading(true);
1316
const response = await axiosInstance.get('/complains/get-complains');
1417
setComplaints(response.data);
1518
} catch (error) {
1619
console.error('Error fetching complaints:', error);
20+
} finally {
21+
setLoading(false);
1722
}
1823
};
1924

2025
const resolveComplaintWithAI = async (complaint) => {
2126
try {
22-
const aiResponse = await axiosInstance.post('/complains/ai-resolve', {
27+
await axiosInstance.post('/complains/ai-resolve', {
2328
complaintText: complaint.complaintText,
2429
complaintId: complaint._id
2530
});
26-
console.log('AI resolved:', aiResponse.data);
27-
fetchComplains(); // Refresh
31+
32+
// Instead of fetching all complaints, update this specific complaint locally
33+
setComplaints(prevComplaints =>
34+
prevComplaints.map(c =>
35+
c._id === complaint._id
36+
? {...c, status: 'Processing'} // Mark as processing until next fetch
37+
: c
38+
)
39+
);
40+
41+
// Optional: Fetch all complaints after a small delay to get the updated data
42+
setTimeout(fetchComplains, 1000);
2843
} catch (error) {
2944
console.error('Error resolving with AI:', error);
3045
}
3146
};
3247

33-
useEffect(() => {
34-
complaints.forEach(c => {
35-
if (c.status === 'Pending') {
36-
resolveComplaintWithAI(c);
37-
}
38-
});
39-
}, [complaints]);
48+
// Manual resolution button handler
49+
const handleResolveClick = (complaint) => {
50+
if (complaint.status === 'Pending') {
51+
resolveComplaintWithAI(complaint);
52+
}
53+
};
4054

4155
return (
4256
<div style={{ padding: '20px' }}>
43-
<h1>Complaints</h1>
44-
{complaints.map((c) => (
45-
<div key={c._id} style={{ border: '1px solid #ccc', margin: '10px', padding: '10px' }}>
46-
<p><strong>User:</strong> {c.userId}</p>
47-
<p><strong>Complaint:</strong> {c.complaintText}</p>
48-
<p><strong>Status:</strong> {c.status}</p>
49-
{c.resolutionText && <p><strong>AI Resolution:</strong> {c.resolutionText}</p>}
50-
</div>
51-
))}
57+
<h1>Complaints Management</h1>
58+
{loading ? (
59+
<p>Loading complaints...</p>
60+
) : (
61+
<>
62+
<button
63+
onClick={fetchComplains}
64+
style={{ marginBottom: '15px', padding: '8px 12px' }}
65+
>
66+
Refresh Complaints
67+
</button>
68+
69+
{complaints.length === 0 ? (
70+
<p>No complaints found.</p>
71+
) : (
72+
complaints.map((c) => (
73+
<div
74+
key={c._id}
75+
style={{
76+
border: '1px solid #ccc',
77+
borderRadius: '4px',
78+
margin: '10px 0',
79+
padding: '15px',
80+
backgroundColor: c.status === 'Resolved' ? '#f0f7f0' : '#fff'
81+
}}
82+
>
83+
<p><strong>User:</strong> {c.userId}</p>
84+
<p><strong>Complaint:</strong> {c.complaintText}</p>
85+
<p><strong>Status:</strong> {c.status}</p>
86+
{c.resolutionText && (
87+
<div style={{ marginTop: '10px', padding: '10px', backgroundColor: '#f5f5f5' }}>
88+
<p><strong>AI Resolution:</strong> {c.resolutionText}</p>
89+
</div>
90+
)}
91+
{c.status === 'Pending' && (
92+
<button
93+
onClick={() => handleResolveClick(c)}
94+
style={{ marginTop: '10px', padding: '5px 10px' }}
95+
>
96+
Resolve with AI
97+
</button>
98+
)}
99+
</div>
100+
))
101+
)}
102+
</>
103+
)}
52104
</div>
53105
);
54106
};
55107

56-
export default Complains;
108+
export default Complains;

0 commit comments

Comments
 (0)