EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
EditAgent.jsx
Go to the documentation of this file.
1import React, { useState, useEffect } from 'react';
2import { apiFetch } from '../lib/api';
3import MainLayout from '../components/Layout/MainLayout';
4import { useParams, useNavigate } from 'react-router-dom';
5
6function EditAgent() {
7 const { agentId } = useParams();
8 const navigate = useNavigate();
9 const [agent, setAgent] = useState(null);
10 const [loading, setLoading] = useState(true);
11 const [error, setError] = useState(null);
12 const [form, setForm] = useState({ name: '', customer: '' });
13
14 useEffect(() => {
15 const token = localStorage.getItem('token');
16 let tenantId = '';
17 if (token) {
18 try {
19 const payload = token.split('.')[1];
20 const decoded = JSON.parse(atob(payload.replace(/-/g, '+').replace(/_/g, '/')));
21 tenantId = decoded.tenant_id || decoded.tenantId || '';
22 } catch {}
23 }
24 apiFetch(`/agent/${agentId}`, {
25 headers: {
26 'X-Tenant-Id': tenantId
27 }
28 })
29 .then(res => res.json())
30 .then(data => {
31 setAgent(data);
32 setForm({ name: data.name || '', customer: data.customer || '' });
33 setLoading(false);
34 })
35 .catch(() => {
36 setError('Failed to load agent');
37 setLoading(false);
38 });
39 }, [agentId]);
40
41 const handleChange = e => {
42 setForm({ ...form, [e.target.name]: e.target.value });
43 };
44
45 const handleSubmit = async e => {
46 e.preventDefault();
47 setLoading(true);
48 setError(null);
49 try {
50 const token = localStorage.getItem('token');
51 let tenantId = '';
52 if (token) {
53 try {
54 const payload = token.split('.')[1];
55 const decoded = JSON.parse(atob(payload.replace(/-/g, '+').replace(/_/g, '/')));
56 tenantId = decoded.tenant_id || decoded.tenantId || '';
57 } catch {}
58 }
59 const res = await apiFetch(`/agent/${agentId}`, {
60 method: 'PUT',
61 headers: {
62 'Content-Type': 'application/json',
63 'X-Tenant-Id': tenantId
64 },
65 body: JSON.stringify(form),
66 });
67 if (!res.ok) throw new Error('Failed to update agent');
68 navigate(`/agents/${agentId}`);
69 } catch (err) {
70 setError(err.message);
71 setLoading(false);
72 }
73 };
74
75 const handleDelete = async () => {
76 if (!window.confirm('Are you sure you want to delete and unenroll this agent? This action cannot be undone.')) return;
77 setLoading(true);
78 setError(null);
79 try {
80 const token = localStorage.getItem('token');
81 let tenantId = '';
82 if (token) {
83 try {
84 const payload = token.split('.')[1];
85 const decoded = JSON.parse(atob(payload.replace(/-/g, '+').replace(/_/g, '/')));
86 tenantId = decoded.tenant_id || decoded.tenantId || '';
87 } catch {}
88 }
89 const res = await apiFetch(`/agent/${agentId}`, {
90 method: 'DELETE',
91 headers: {
92 'Content-Type': 'application/json',
93 'X-Tenant-Id': tenantId
94 }
95 });
96 if (!res.ok) throw new Error('Failed to delete agent');
97 navigate('/agents');
98 } catch (err) {
99 setError(err.message);
100 setLoading(false);
101 }
102 };
103
104 if (loading) return <MainLayout><div>Loading...</div></MainLayout>;
105 if (error) return <MainLayout><div className="error">{error}</div></MainLayout>;
106
107 return (
108 <MainLayout>
109 <h2>Edit Agent</h2>
110 <form onSubmit={handleSubmit} style={{ maxWidth: 400 }}>
111 <div style={{ marginBottom: 16 }}>
112 <label>Name:</label>
113 <input
114 type="text"
115 name="name"
116 value={form.name}
117 onChange={handleChange}
118 style={{ width: '100%', padding: 8 }}
119 />
120 </div>
121 <div style={{ marginBottom: 16 }}>
122 <label>Customer:</label>
123 <input
124 type="text"
125 name="customer"
126 value={form.customer}
127 onChange={handleChange}
128 style={{ width: '100%', padding: 8 }}
129 />
130 </div>
131 <button type="submit" style={{ padding: '8px 20px', fontSize: '1rem' }}>Save</button>
132 <button
133 type="button"
134 onClick={handleDelete}
135 style={{ marginLeft: 16, padding: '8px 20px', fontSize: '1rem', background: '#d9534f', color: 'white', border: 'none', borderRadius: 4 }}
136 >
137 Delete Agent
138 </button>
139 </form>
140 </MainLayout>
141 );
142}
143
144export default EditAgent;