import { useState } from "react";
import { Link, useLocation } from "wouter";
import { desenvolver } from "../../../api/http";
{% if access.update_roles or access.delete_roles -%}
import { useAuth } from "../../../auth/auth";
{% endif -%}
import { AlertaError } from "../../../compartido/ui/alerta-error";
import { DialogoConfirmar } from "../../../compartido/ui/dialogo-confirmar";
import { useCarga } from "../../../compartido/use-carga";
import { useDependencias } from "../../../dependencias";
import { VistaDetalle{{ pascal }} } from "../vistas/detalle-{{ kebab }}";
export function ContenedorDetalle{{ pascal }}({ id }: { id: string }) {
const { api } = useDependencias();
{%- if access.update_roles or access.delete_roles %}
const { puede } = useAuth();
{%- endif %}
{%- if detail_load_inline %}
const {{ ident }} = useCarga(() => desenvolver(api.GET("/{{ plural }}/{id}", { params: { path: { id } } })), [api, id]);
{%- else %}
const {{ ident }} = useCarga(
() => desenvolver(api.GET("/{{ plural }}/{id}", { params: { path: { id } } })),
[api, id],
);
{%- endif %}
const [, navegar] = useLocation();
const [confirmando, setConfirmando] = useState(false);
const [fallo, setFallo] = useState<unknown>(null);
const borrar = async () => {
setConfirmando(false);
try {
await desenvolver(api.DELETE("/{{ plural }}/{id}", { params: { path: { id } } }));
navegar("/{{ plural }}");
} catch (error) {
setFallo(error);
}
};
{%- for t in targets %}
const etiqueta{{ t.pascal }} = async (objetivo: string) => {
const params = { path: { id: objetivo } };
const encontrado = await desenvolver(api.GET("/{{ t.plural }}/{id}", { params }));
return encontrado.{{ t.label }};
};
{%- endfor %}
return (
<section className="space-y-4">
<h1 className="text-2xl font-bold">{{ label }}</h1>
{fallo !== null && <AlertaError error={fallo} />}
{{ "{" }}{{ ident }}.estado === "cargando" && <span className="loading loading-spinner" />}
{{ "{" }}{{ ident }}.estado === "error" && <AlertaError error={{ "{" }}{{ ident }}.error} />}
{{ "{" }}{{ ident }}.estado === "listo" && (
<div className="card bg-base-100">
<div className="card-body">
{%- if detail_view_inline %}
<VistaDetalle{{ pascal }} {{ ident }}={{ "{" }}{{ ident }}.datos}{% for t in targets %} etiqueta{{ t.pascal }}={etiqueta{{ t.pascal }}}{% endfor %} />
{%- else %}
<VistaDetalle{{ pascal }}
{{ ident }}={{ "{" }}{{ ident }}.datos}
{%- for t in targets %}
etiqueta{{ t.pascal }}={etiqueta{{ t.pascal }}}
{%- endfor %}
/>
{%- endif %}
<div className="card-actions justify-end">
{%- if access.update_roles %}
{{ "{" }}{% for r in access.update_roles %}puede("{{ r }}") && {% endfor %}(
<Link href={`/{{ plural }}/${id}/editar`} className="btn">
Editar
</Link>
)}
{%- else %}
<Link href={`/{{ plural }}/${id}/editar`} className="btn">
Editar
</Link>
{%- endif %}
{%- if access.delete_roles %}
{{ "{" }}{% for r in access.delete_roles %}puede("{{ r }}") && {% endfor %}(
<button
type="button"
className="btn btn-error"
onClick={() => setConfirmando(true)}
>
Borrar
</button>
)}
{%- else %}
<button type="button" className="btn btn-error" onClick={() => setConfirmando(true)}>
Borrar
</button>
{%- endif %}
</div>
</div>
</div>
)}
<Link href="/{{ plural }}" className="link">
Volver a {{ words_plural }}
</Link>
<DialogoConfirmar
abierto={confirmando}
mensaje="¿Borrar {{ words }}?"
alConfirmar={borrar}
alCancelar={() => setConfirmando(false)}
/>
</section>
);
}