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

Commit 6e1a684

Browse files
committed
ai error
1 parent 0b37f0e commit 6e1a684

2 files changed

Lines changed: 72 additions & 18 deletions

File tree

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

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import axiosInstance from '../../../utils/axiosInstance';
44
const Complains = () => {
55
const [complaints, setComplaints] = useState([]);
66
const [loading, setLoading] = useState(false);
7+
const [error, setError] = useState(null);
78

89
// Fetch complaints on component mount only
910
useEffect(() => {
@@ -13,35 +14,54 @@ const Complains = () => {
1314
const fetchComplains = async () => {
1415
try {
1516
setLoading(true);
17+
setError(null);
1618
const response = await axiosInstance.get('/complains/get-complains');
1719
setComplaints(response.data);
1820
} catch (error) {
1921
console.error('Error fetching complaints:', error);
22+
setError('Failed to load complaints. Please try again.');
2023
} finally {
2124
setLoading(false);
2225
}
2326
};
2427

2528
const resolveComplaintWithAI = async (complaint) => {
2629
try {
27-
await axiosInstance.post('/complains/ai-resolve', {
30+
setError(null);
31+
// Update UI immediately to show processing
32+
setComplaints(prevComplaints =>
33+
prevComplaints.map(c =>
34+
c._id === complaint._id
35+
? {...c, status: 'Processing'}
36+
: c
37+
)
38+
);
39+
40+
// Make API call with the exact route that matches the backend
41+
const response = await axiosInstance.post('/complains/ai-resolve', {
2842
complaintText: complaint.complaintText,
2943
complaintId: complaint._id
3044
});
3145

32-
// Instead of fetching all complaints, update this specific complaint locally
46+
// Update the complaint with the response data
3347
setComplaints(prevComplaints =>
3448
prevComplaints.map(c =>
3549
c._id === complaint._id
36-
? {...c, status: 'Processing'} // Mark as processing until next fetch
50+
? response.data
3751
: c
3852
)
3953
);
40-
41-
// Optional: Fetch all complaints after a small delay to get the updated data
42-
setTimeout(fetchComplains, 1000);
4354
} catch (error) {
4455
console.error('Error resolving with AI:', error);
56+
setError('Failed to resolve complaint. Please try again.');
57+
// Revert the complaint status on error
58+
setComplaints(prevComplaints =>
59+
prevComplaints.map(c =>
60+
c._id === complaint._id
61+
? {...c, status: 'Pending'}
62+
: c
63+
)
64+
);
4565
}
4666
};
4767

@@ -55,17 +75,33 @@ const Complains = () => {
5575
return (
5676
<div style={{ padding: '20px' }}>
5777
<h1>Complaints Management</h1>
78+
79+
{error && (
80+
<div style={{ color: 'red', margin: '10px 0', padding: '10px', backgroundColor: '#ffecec', borderRadius: '4px' }}>
81+
{error}
82+
</div>
83+
)}
84+
85+
<button
86+
onClick={fetchComplains}
87+
disabled={loading}
88+
style={{
89+
marginBottom: '15px',
90+
padding: '8px 12px',
91+
backgroundColor: loading ? '#cccccc' : '#4285f4',
92+
color: 'white',
93+
border: 'none',
94+
borderRadius: '4px',
95+
cursor: loading ? 'not-allowed' : 'pointer'
96+
}}
97+
>
98+
{loading ? 'Loading...' : 'Refresh Complaints'}
99+
</button>
100+
58101
{loading ? (
59102
<p>Loading complaints...</p>
60103
) : (
61104
<>
62-
<button
63-
onClick={fetchComplains}
64-
style={{ marginBottom: '15px', padding: '8px 12px' }}
65-
>
66-
Refresh Complaints
67-
</button>
68-
69105
{complaints.length === 0 ? (
70106
<p>No complaints found.</p>
71107
) : (
@@ -77,21 +113,39 @@ const Complains = () => {
77113
borderRadius: '4px',
78114
margin: '10px 0',
79115
padding: '15px',
80-
backgroundColor: c.status === 'Resolved' ? '#f0f7f0' : '#fff'
116+
backgroundColor: c.status === 'Resolved' ? '#f0f7f0' :
117+
c.status === 'Processing' ? '#fff8e1' : '#fff'
81118
}}
82119
>
83120
<p><strong>User:</strong> {c.userId}</p>
84121
<p><strong>Complaint:</strong> {c.complaintText}</p>
85-
<p><strong>Status:</strong> {c.status}</p>
122+
<p>
123+
<strong>Status:</strong>
124+
<span style={{
125+
color: c.status === 'Resolved' ? 'green' :
126+
c.status === 'Processing' ? 'orange' : 'gray',
127+
fontWeight: 'bold'
128+
}}>
129+
{c.status}
130+
</span>
131+
</p>
86132
{c.resolutionText && (
87-
<div style={{ marginTop: '10px', padding: '10px', backgroundColor: '#f5f5f5' }}>
133+
<div style={{ marginTop: '10px', padding: '10px', backgroundColor: '#f5f5f5', borderRadius: '4px' }}>
88134
<p><strong>AI Resolution:</strong> {c.resolutionText}</p>
89135
</div>
90136
)}
91137
{c.status === 'Pending' && (
92138
<button
93139
onClick={() => handleResolveClick(c)}
94-
style={{ marginTop: '10px', padding: '5px 10px' }}
140+
style={{
141+
marginTop: '10px',
142+
padding: '5px 10px',
143+
backgroundColor: '#34a853',
144+
color: 'white',
145+
border: 'none',
146+
borderRadius: '4px',
147+
cursor: 'pointer'
148+
}}
95149
>
96150
Resolve with AI
97151
</button>

server/routes/legal/complains.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ const router = express.Router();
88

99
router.post('/postComplains', createComplaint);
1010
router.get('/get-complains', getComplaints);
11-
router.patch('/ai-resolve', resolveComplaintWithAI);
11+
router.post('/ai-resolve', resolveComplaintWithAI);
1212

1313
export default router;

0 commit comments

Comments
 (0)