backend y front estables ses creo modal para permisos de checklist backend 1.0.31 y frontend 1.0.33
This commit is contained in:
@@ -1344,8 +1344,10 @@ function QuestionsManagerModal({ checklist, onClose }) {
|
||||
function ChecklistsTab({ checklists, user, onChecklistCreated, onStartInspection }) {
|
||||
const [showCreateModal, setShowCreateModal] = useState(false)
|
||||
const [showQuestionsModal, setShowQuestionsModal] = useState(false)
|
||||
const [showEditPermissionsModal, setShowEditPermissionsModal] = useState(false)
|
||||
const [selectedChecklist, setSelectedChecklist] = useState(null)
|
||||
const [creating, setCreating] = useState(false)
|
||||
const [updating, setUpdating] = useState(false)
|
||||
const [mechanics, setMechanics] = useState([])
|
||||
const [formData, setFormData] = useState({
|
||||
name: '',
|
||||
@@ -1354,6 +1356,9 @@ function ChecklistsTab({ checklists, user, onChecklistCreated, onStartInspection
|
||||
scoring_enabled: true,
|
||||
mechanic_ids: []
|
||||
})
|
||||
const [editPermissionsData, setEditPermissionsData] = useState({
|
||||
mechanic_ids: []
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
loadMechanics()
|
||||
@@ -1417,6 +1422,39 @@ function ChecklistsTab({ checklists, user, onChecklistCreated, onStartInspection
|
||||
}
|
||||
}
|
||||
|
||||
const handleEditPermissions = async (e) => {
|
||||
e.preventDefault()
|
||||
setUpdating(true)
|
||||
|
||||
try {
|
||||
const token = localStorage.getItem('token')
|
||||
const API_URL = import.meta.env.VITE_API_URL || ''
|
||||
|
||||
const response = await fetch(`${API_URL}/api/checklists/${selectedChecklist.id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(editPermissionsData),
|
||||
})
|
||||
|
||||
if (response.ok) {
|
||||
setShowEditPermissionsModal(false)
|
||||
setSelectedChecklist(null)
|
||||
setEditPermissionsData({ mechanic_ids: [] })
|
||||
onChecklistCreated() // Reload checklists
|
||||
} else {
|
||||
alert('Error al actualizar permisos')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error:', error)
|
||||
alert('Error al actualizar permisos')
|
||||
} finally {
|
||||
setUpdating(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{user.role === 'admin' && (
|
||||
@@ -1432,14 +1470,24 @@ function ChecklistsTab({ checklists, user, onChecklistCreated, onStartInspection
|
||||
|
||||
{checklists.length === 0 ? (
|
||||
<div className="text-center py-12">
|
||||
<p className="text-gray-500">No hay checklists activos</p>
|
||||
{user.role === 'admin' && (
|
||||
<button
|
||||
onClick={() => setShowCreateModal(true)}
|
||||
className="mt-4 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition"
|
||||
>
|
||||
Crear tu primer checklist
|
||||
</button>
|
||||
{user.role === 'admin' ? (
|
||||
<>
|
||||
<p className="text-gray-500">No hay checklists activos</p>
|
||||
<button
|
||||
onClick={() => setShowCreateModal(true)}
|
||||
className="mt-4 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition"
|
||||
>
|
||||
Crear tu primer checklist
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div className="text-4xl mb-3">🔒</div>
|
||||
<p className="text-gray-700 font-semibold">No tienes checklists disponibles</p>
|
||||
<p className="text-sm text-gray-500 mt-2">
|
||||
Contacta con el administrador para que te asigne permisos a los checklists que necesites usar.
|
||||
</p>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
@@ -1477,6 +1525,19 @@ function ChecklistsTab({ checklists, user, onChecklistCreated, onStartInspection
|
||||
<div className="flex gap-2">
|
||||
{user.role === 'admin' && (
|
||||
<>
|
||||
<button
|
||||
onClick={() => {
|
||||
setSelectedChecklist(checklist)
|
||||
setEditPermissionsData({
|
||||
mechanic_ids: checklist.allowed_mechanics || []
|
||||
})
|
||||
setShowEditPermissionsModal(true)
|
||||
}}
|
||||
className="px-3 py-2 bg-gradient-to-r from-orange-500 to-amber-500 text-white rounded-lg hover:from-orange-600 hover:to-amber-600 transition-all transform hover:scale-105 shadow-lg text-sm"
|
||||
title="Editar permisos"
|
||||
>
|
||||
🔐 Permisos
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
setSelectedChecklist(checklist)
|
||||
@@ -1702,6 +1763,106 @@ function ChecklistsTab({ checklists, user, onChecklistCreated, onStartInspection
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Modal Editar Permisos */}
|
||||
{showEditPermissionsModal && selectedChecklist && (
|
||||
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
|
||||
<div className="bg-white rounded-lg max-w-2xl w-full max-h-[90vh] overflow-y-auto">
|
||||
<div className="p-6">
|
||||
<h2 className="text-2xl font-bold text-gray-900 mb-2">Editar Permisos de Checklist</h2>
|
||||
<p className="text-sm text-gray-600 mb-4">
|
||||
{selectedChecklist.name}
|
||||
</p>
|
||||
|
||||
<form onSubmit={handleEditPermissions} className="space-y-4">
|
||||
<div className="border-t pt-4">
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
🔐 Mecánicos Autorizados
|
||||
</label>
|
||||
<div className="bg-gray-50 border border-gray-300 rounded-lg p-3 max-h-96 overflow-y-auto">
|
||||
{mechanics.length === 0 ? (
|
||||
<p className="text-sm text-gray-500">No hay mecánicos disponibles</p>
|
||||
) : (
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center pb-2 border-b">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={editPermissionsData.mechanic_ids.length === 0}
|
||||
onChange={(e) => {
|
||||
if (e.target.checked) {
|
||||
setEditPermissionsData({ mechanic_ids: [] })
|
||||
}
|
||||
}}
|
||||
className="w-4 h-4 text-green-600 border-gray-300 rounded focus:ring-green-500"
|
||||
/>
|
||||
<label className="ml-2 text-sm font-semibold text-green-700">
|
||||
🌍 Todos los mecánicos (acceso global)
|
||||
</label>
|
||||
</div>
|
||||
{mechanics.map((mechanic) => (
|
||||
<div key={mechanic.id} className="flex items-center">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={editPermissionsData.mechanic_ids.includes(mechanic.id)}
|
||||
onChange={(e) => {
|
||||
if (e.target.checked) {
|
||||
setEditPermissionsData({
|
||||
mechanic_ids: [...editPermissionsData.mechanic_ids, mechanic.id]
|
||||
})
|
||||
} else {
|
||||
setEditPermissionsData({
|
||||
mechanic_ids: editPermissionsData.mechanic_ids.filter(id => id !== mechanic.id)
|
||||
})
|
||||
}
|
||||
}}
|
||||
className="w-4 h-4 text-indigo-600 border-gray-300 rounded focus:ring-indigo-500"
|
||||
/>
|
||||
<label className="ml-2 text-sm text-gray-700">
|
||||
{mechanic.full_name || mechanic.username} ({mechanic.email})
|
||||
</label>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<p className="mt-2 text-xs text-gray-500">
|
||||
💡 Si no seleccionas ningún mecánico, todos podrán usar este checklist.
|
||||
Si seleccionas mecánicos específicos, solo ellos tendrán acceso.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="bg-blue-50 border border-blue-200 rounded-lg p-4">
|
||||
<p className="text-sm text-blue-800">
|
||||
ℹ️ Los cambios se aplicarán inmediatamente. Los mecánicos que pierdan acceso ya no verán este checklist.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-3 pt-4">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setShowEditPermissionsModal(false)
|
||||
setSelectedChecklist(null)
|
||||
setEditPermissionsData({ mechanic_ids: [] })
|
||||
}}
|
||||
className="flex-1 px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition"
|
||||
disabled={updating}
|
||||
>
|
||||
Cancelar
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
className="flex-1 px-4 py-2 bg-orange-600 text-white rounded-lg hover:bg-orange-700 transition disabled:opacity-50"
|
||||
disabled={updating}
|
||||
>
|
||||
{updating ? 'Guardando...' : 'Guardar Permisos'}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user