Refactor MapaCurricularPage: improve line editing functionality and enhance tooltip interactions
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
/* eslint-disable jsx-a11y/no-static-element-interactions */
|
|
||||||
/* eslint-disable jsx-a11y/label-has-associated-control */
|
/* eslint-disable jsx-a11y/label-has-associated-control */
|
||||||
import { createFileRoute } from '@tanstack/react-router'
|
import { createFileRoute } from '@tanstack/react-router'
|
||||||
import { Plus, AlertTriangle, Trash2, Pencil, Download } from 'lucide-react'
|
import { Plus, AlertTriangle, Trash2, Download } from 'lucide-react'
|
||||||
import * as Icons from 'lucide-react'
|
import * as Icons from 'lucide-react'
|
||||||
import { useMemo, useState, useEffect, Fragment } from 'react'
|
import { useMemo, useState, useEffect, Fragment } from 'react'
|
||||||
|
|
||||||
@@ -238,7 +237,7 @@ function AsignaturaCardItem({
|
|||||||
<EstadoIcon className="text-foreground/65 h-3.5 w-3.5" />
|
<EstadoIcon className="text-foreground/65 h-3.5 w-3.5" />
|
||||||
</div>
|
</div>
|
||||||
</TooltipTrigger>
|
</TooltipTrigger>
|
||||||
<TooltipContent side="left">
|
<TooltipContent side="right">
|
||||||
<span className="text-xs font-semibold">
|
<span className="text-xs font-semibold">
|
||||||
{estado.label}
|
{estado.label}
|
||||||
</span>
|
</span>
|
||||||
@@ -249,7 +248,7 @@ function AsignaturaCardItem({
|
|||||||
{/* titulo */}
|
{/* titulo */}
|
||||||
<div className="mt-4 min-h-18">
|
<div className="mt-4 min-h-18">
|
||||||
<h3
|
<h3
|
||||||
className="text-foreground text-md overflow-hidden leading-[1.08] font-bold"
|
className="text-foreground text-md overflow-hidden leading-[1.08]"
|
||||||
style={{
|
style={{
|
||||||
display: '-webkit-box',
|
display: '-webkit-box',
|
||||||
WebkitLineClamp: 3,
|
WebkitLineClamp: 3,
|
||||||
@@ -333,7 +332,6 @@ function MapaCurricularPage() {
|
|||||||
const { data } = usePlan(planId)
|
const { data } = usePlan(planId)
|
||||||
const [totalCiclos, setTotalCiclos] = useState(0)
|
const [totalCiclos, setTotalCiclos] = useState(0)
|
||||||
const [editingLineaId, setEditingLineaId] = useState<string | null>(null)
|
const [editingLineaId, setEditingLineaId] = useState<string | null>(null)
|
||||||
const [tempNombreLinea, setTempNombreLinea] = useState('')
|
|
||||||
const { mutate: createLinea } = useCreateLinea()
|
const { mutate: createLinea } = useCreateLinea()
|
||||||
const { mutate: updateLineaApi } = useUpdateLinea()
|
const { mutate: updateLineaApi } = useUpdateLinea()
|
||||||
const { mutate: deleteLineaApi } = useDeleteLinea()
|
const { mutate: deleteLineaApi } = useDeleteLinea()
|
||||||
@@ -397,11 +395,8 @@ function MapaCurricularPage() {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
const guardarEdicionLinea = (id: string, nuevoNombre?: string) => {
|
const guardarEdicionLinea = (id: string, nuevoNombre: string) => {
|
||||||
// Usamos el nombre que viene por parámetro o el del estado como fallback
|
const nombreAFijar = nuevoNombre.trim()
|
||||||
const nombreAFijar = (
|
|
||||||
nuevoNombre !== undefined ? nuevoNombre : tempNombreLinea
|
|
||||||
).trim()
|
|
||||||
|
|
||||||
if (!nombreAFijar) {
|
if (!nombreAFijar) {
|
||||||
setEditingLineaId(null)
|
setEditingLineaId(null)
|
||||||
@@ -421,7 +416,6 @@ function MapaCurricularPage() {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
setEditingLineaId(null)
|
setEditingLineaId(null)
|
||||||
setTempNombreLinea('')
|
|
||||||
},
|
},
|
||||||
onError: (err) => {
|
onError: (err) => {
|
||||||
console.error('Error al actualizar linea:', err)
|
console.error('Error al actualizar linea:', err)
|
||||||
@@ -632,38 +626,28 @@ function MapaCurricularPage() {
|
|||||||
[asignaturas],
|
[asignaturas],
|
||||||
)
|
)
|
||||||
|
|
||||||
const handleKeyDownLinea = (
|
const confirmarEdicionLinea = (id: string, nuevoNombreRaw: string) => {
|
||||||
e: React.KeyboardEvent<HTMLSpanElement>,
|
const nuevoNombre = nuevoNombreRaw.trim()
|
||||||
_id: string,
|
|
||||||
) => {
|
|
||||||
if (e.key === 'Enter') {
|
|
||||||
e.preventDefault()
|
|
||||||
e.currentTarget.blur()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleBlurLinea = (
|
|
||||||
e: React.FocusEvent<HTMLSpanElement>,
|
|
||||||
id: string,
|
|
||||||
) => {
|
|
||||||
const nuevoNombre = e.currentTarget.textContent.trim() || ''
|
|
||||||
|
|
||||||
// Buscamos la línea original para comparar
|
|
||||||
const lineaOriginal = lineas.find((l) => l.id === id)
|
const lineaOriginal = lineas.find((l) => l.id === id)
|
||||||
|
|
||||||
if (nuevoNombre !== lineaOriginal?.nombre) {
|
if (!nuevoNombre) {
|
||||||
// IMPORTANTE: Pasamos nuevoNombre directamente
|
|
||||||
guardarEdicionLinea(id, nuevoNombre)
|
|
||||||
} else {
|
|
||||||
setEditingLineaId(null)
|
setEditingLineaId(null)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (nuevoNombre !== lineaOriginal?.nombre) {
|
||||||
|
guardarEdicionLinea(id, nuevoNombre)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setEditingLineaId(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loadingAsig || loadingLineas)
|
if (loadingAsig || loadingLineas)
|
||||||
return <div className="p-10 text-center">Cargando mapa curricular...</div>
|
return <div className="p-10 text-center">Cargando mapa curricular...</div>
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container space-y-6 py-4">
|
<div className="space-y-6">
|
||||||
{/* Header */}
|
{/* Header */}
|
||||||
<div className="border-border bg-card/70 rounded-2xl border p-4 shadow-sm">
|
<div className="border-border bg-card/70 rounded-2xl border p-4 shadow-sm">
|
||||||
<div className="grid gap-4 lg:grid-cols-[minmax(0,1fr)_auto] lg:items-start">
|
<div className="grid gap-4 lg:grid-cols-[minmax(0,1fr)_auto] lg:items-start">
|
||||||
@@ -770,7 +754,7 @@ function MapaCurricularPage() {
|
|||||||
return (
|
return (
|
||||||
<Fragment key={linea.id}>
|
<Fragment key={linea.id}>
|
||||||
<div
|
<div
|
||||||
className={`group relative flex items-center justify-between rounded-xl border p-3 transition-all ${editingLineaId === linea.id ? 'ring-primary/30 ring-2' : ''}`}
|
className={`group relative flex items-start justify-between gap-2 rounded-xl border p-3 transition-all ${editingLineaId === linea.id ? 'ring-primary/30 ring-2' : 'cursor-text'}`}
|
||||||
style={{
|
style={{
|
||||||
borderColor: hexToRgba(linea.color || '#1976d2', 0.24),
|
borderColor: hexToRgba(linea.color || '#1976d2', 0.24),
|
||||||
backgroundColor:
|
backgroundColor:
|
||||||
@@ -779,42 +763,50 @@ function MapaCurricularPage() {
|
|||||||
: hexToRgba(linea.color || '#1976d2', 0.08),
|
: hexToRgba(linea.color || '#1976d2', 0.08),
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="min-w-0 flex-1 overflow-hidden">
|
<div className="min-w-0 flex-1">
|
||||||
<span
|
<Tooltip>
|
||||||
contentEditable={editingLineaId === linea.id}
|
<TooltipTrigger asChild>
|
||||||
suppressContentEditableWarning
|
<span
|
||||||
spellCheck={false}
|
contentEditable
|
||||||
onKeyDown={(e) => handleKeyDownLinea(e, linea.id)}
|
role="textbox"
|
||||||
onBlur={(e) => handleBlurLinea(e, linea.id)}
|
tabIndex={0}
|
||||||
onClick={() => {
|
aria-label={`Nombre de línea ${linea.nombre}`}
|
||||||
if (editingLineaId !== linea.id) {
|
suppressContentEditableWarning
|
||||||
setEditingLineaId(linea.id)
|
spellCheck={false}
|
||||||
setTempNombreLinea(linea.nombre)
|
onFocus={() => setEditingLineaId(linea.id)}
|
||||||
}
|
onKeyDown={(e) => {
|
||||||
}}
|
if (e.key === 'Enter') {
|
||||||
className={`text-foreground block w-full truncate text-sm font-bold wrap-break-word outline-none ${
|
e.preventDefault()
|
||||||
editingLineaId === linea.id
|
e.currentTarget.blur()
|
||||||
? 'border-primary/40 cursor-text border-b pb-1'
|
}
|
||||||
: 'cursor-pointer'
|
if (e.key === 'Escape') {
|
||||||
}`}
|
e.preventDefault()
|
||||||
>
|
e.currentTarget.textContent = linea.nombre
|
||||||
{linea.nombre}
|
e.currentTarget.blur()
|
||||||
</span>
|
}
|
||||||
|
}}
|
||||||
|
onBlur={(e) =>
|
||||||
|
confirmarEdicionLinea(
|
||||||
|
linea.id,
|
||||||
|
e.currentTarget.textContent,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
className="text-foreground hover:text-foreground/85 block w-full cursor-text text-sm leading-snug wrap-break-word transition-colors outline-none"
|
||||||
|
>
|
||||||
|
{linea.nombre}
|
||||||
|
</span>
|
||||||
|
</TooltipTrigger>
|
||||||
|
<TooltipContent side="top" className="max-w-xs text-sm">
|
||||||
|
{linea.nombre}
|
||||||
|
</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
</div>
|
</div>
|
||||||
<div className="ml-2 flex shrink-0 items-center gap-1">
|
<div className="ml-1 flex shrink-0 items-center gap-1">
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
aria-label="Editar línea"
|
|
||||||
onClick={() => setEditingLineaId(linea.id)}
|
|
||||||
className="text-foreground/65 hover:text-foreground inline-flex h-7 w-7 items-center justify-center rounded-md transition-colors"
|
|
||||||
>
|
|
||||||
<Pencil size={12} />
|
|
||||||
</button>
|
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
aria-label="Eliminar línea"
|
aria-label="Eliminar línea"
|
||||||
onClick={() => borrarLinea(linea.id)}
|
onClick={() => borrarLinea(linea.id)}
|
||||||
className="text-destructive/80 hover:text-destructive inline-flex h-7 w-7 items-center justify-center rounded-md transition-colors"
|
className="text-destructive/80 hover:text-destructive hover:bg-destructive/10 inline-flex h-8 w-8 items-center justify-center rounded-md transition-colors"
|
||||||
>
|
>
|
||||||
<Trash2 size={14} />
|
<Trash2 size={14} />
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
Reference in New Issue
Block a user