|
| 1 | +import { useState, useEffect } from "react"; |
| 2 | +import axios from "axios"; |
| 3 | + |
| 4 | +const TerminalModalEditContent = ({ onClose, setToast, terminal }) => { |
| 5 | + const [form, setForm] = useState({ |
| 6 | + name: "", |
| 7 | + host: "", |
| 8 | + port: 22, |
| 9 | + username: "", |
| 10 | + password: "", |
| 11 | + use_private_key: false, |
| 12 | + private_key: "", |
| 13 | + description: "", |
| 14 | + }); |
| 15 | + |
| 16 | + const [errors, setErrors] = useState({}); |
| 17 | + const [loading, setLoading] = useState(true); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (!terminal) return; |
| 21 | + |
| 22 | + axios.get(`/terminal/${terminal}/show`) |
| 23 | + .then((res) => { |
| 24 | + setForm({ |
| 25 | + name: res.data.name || "", |
| 26 | + host: res.data.host || "", |
| 27 | + port: res.data.port || 22, |
| 28 | + username: res.data.username || "", |
| 29 | + password: res.data.password || "", |
| 30 | + use_private_key: !!res.data.use_private_key, |
| 31 | + private_key: res.data.private_key || "", |
| 32 | + description: res.data.description || "", |
| 33 | + }); |
| 34 | + setLoading(false); |
| 35 | + }) |
| 36 | + .catch((err) => { |
| 37 | + console.error("Error al cargar la terminal:", err); |
| 38 | + setToast({ |
| 39 | + show: true, |
| 40 | + success: false, |
| 41 | + message: "Error al cargar el terminal.", |
| 42 | + }); |
| 43 | + onClose(); |
| 44 | + }); |
| 45 | + }, [terminal]); |
| 46 | + |
| 47 | + const handleChange = (e) => { |
| 48 | + const { name, value, type, checked } = e.target; |
| 49 | + setForm((prev) => ({ |
| 50 | + ...prev, |
| 51 | + [name]: type === "checkbox" ? checked : value, |
| 52 | + })); |
| 53 | + }; |
| 54 | + |
| 55 | + const handleSubmit = async (e) => { |
| 56 | + e.preventDefault(); |
| 57 | + setErrors({}); |
| 58 | + setToast({ show: false, success: false, message: "" }); |
| 59 | + |
| 60 | + try { |
| 61 | + const res = await axios.put(`/terminal/${terminal}/edit`, form); |
| 62 | + setToast({ |
| 63 | + show: true, |
| 64 | + success: true, |
| 65 | + message: res.data?.message || "Terminal actualizada correctamente.", |
| 66 | + }); |
| 67 | + onClose(); |
| 68 | + } catch (err) { |
| 69 | + if (err.response?.status === 422) { |
| 70 | + setErrors(err.response.data.errors); |
| 71 | + setToast({ |
| 72 | + show: true, |
| 73 | + success: false, |
| 74 | + message: "Error al actualizar la Terminal.", |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + setTimeout(() => setToast((prev) => ({ ...prev, show: false })), 3000); |
| 80 | + }; |
| 81 | + |
| 82 | + if (loading) { |
| 83 | + return <p className="p-4">Cargando datos de la terminal...</p>; |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <> |
| 88 | + <h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-2"> |
| 89 | + Editar Terminal |
| 90 | + </h3> |
| 91 | + <form onSubmit={handleSubmit} className="space-y-4"> |
| 92 | + {[ |
| 93 | + { label: "Nombre", name: "name", type: "text" }, |
| 94 | + { label: "Host", name: "host", type: "text" }, |
| 95 | + { label: "Puerto", name: "port", type: "number" }, |
| 96 | + { label: "Usuario", name: "username", type: "text" }, |
| 97 | + { label: "Contraseña", name: "password", type: "password" }, |
| 98 | + ].map(({ label, name, type }) => ( |
| 99 | + <div key={name}> |
| 100 | + <label className="block text-sm font-medium text-gray-700 dark:text-gray-300"> |
| 101 | + {label} |
| 102 | + </label> |
| 103 | + <input |
| 104 | + type={type} |
| 105 | + name={name} |
| 106 | + value={form[name]} |
| 107 | + onChange={handleChange} |
| 108 | + className="w-full mt-1 rounded-md p-2 border border-gray-300 dark:bg-gray-700 dark:border-gray-600 dark:text-white" |
| 109 | + /> |
| 110 | + {errors[name] && ( |
| 111 | + <p className="text-sm text-red-600 mt-1">{errors[name][0]}</p> |
| 112 | + )} |
| 113 | + </div> |
| 114 | + ))} |
| 115 | + |
| 116 | + <div> |
| 117 | + <label className="inline-flex items-center gap-2 text-sm font-medium text-gray-700 dark:text-gray-300"> |
| 118 | + <input |
| 119 | + type="checkbox" |
| 120 | + name="use_private_key" |
| 121 | + checked={form.use_private_key} |
| 122 | + onChange={handleChange} |
| 123 | + /> |
| 124 | + Usar clave privada |
| 125 | + </label> |
| 126 | + </div> |
| 127 | + |
| 128 | + {form.use_private_key && ( |
| 129 | + <div> |
| 130 | + <label className="block text-sm font-medium text-gray-700 dark:text-gray-300"> |
| 131 | + Clave privada (PEM) |
| 132 | + </label> |
| 133 | + <textarea |
| 134 | + name="private_key" |
| 135 | + value={form.private_key} |
| 136 | + onChange={handleChange} |
| 137 | + rows={4} |
| 138 | + className="w-full mt-1 rounded-md p-2 border border-gray-300 dark:bg-gray-700 dark:border-gray-600 dark:text-white" |
| 139 | + placeholder="-----BEGIN RSA PRIVATE KEY----- ..." |
| 140 | + /> |
| 141 | + {errors.private_key && ( |
| 142 | + <p className="text-sm text-red-600 mt-1">{errors.private_key[0]}</p> |
| 143 | + )} |
| 144 | + </div> |
| 145 | + )} |
| 146 | + |
| 147 | + <div> |
| 148 | + <label className="block text-sm font-medium text-gray-700 dark:text-gray-300"> |
| 149 | + Descripción |
| 150 | + </label> |
| 151 | + <textarea |
| 152 | + name="description" |
| 153 | + value={form.description} |
| 154 | + onChange={handleChange} |
| 155 | + rows={2} |
| 156 | + className="w-full mt-1 rounded-md p-2 border border-gray-300 dark:bg-gray-700 dark:border-gray-600 dark:text-white" |
| 157 | + /> |
| 158 | + {errors.description && ( |
| 159 | + <p className="text-sm text-red-600 mt-1">{errors.description[0]}</p> |
| 160 | + )} |
| 161 | + </div> |
| 162 | + |
| 163 | + <div className="flex justify-end gap-2 pt-2"> |
| 164 | + <button |
| 165 | + type="button" |
| 166 | + onClick={onClose} |
| 167 | + className="bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 text-gray-700 dark:text-gray-200 px-4 py-2 rounded-md text-sm" |
| 168 | + > |
| 169 | + Cancelar |
| 170 | + </button> |
| 171 | + <button |
| 172 | + type="submit" |
| 173 | + className="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-md text-sm font-medium" |
| 174 | + > |
| 175 | + Guardar cambios |
| 176 | + </button> |
| 177 | + </div> |
| 178 | + </form> |
| 179 | + </> |
| 180 | + ); |
| 181 | +}; |
| 182 | + |
| 183 | +export default TerminalModalEditContent; |
0 commit comments