feat: añadido eliminar repuestos y un modal para ver el objeto entero

This commit is contained in:
fede committed 2026-07-01 02:12:30 -03:00
1 parent 72790ac9a3
commit 2549b7d6ba
7 files changed
+168 -12

No files matched your search

@@ -1,9 +1,29 @@
package repuestos
import (
"net/http"
r "repuestera_back/repositorios"
"strconv"
"github.com/gin-gonic/gin"
)
func EliminarRepuesto(c *gin.Context){
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalido"})
return
}
if (id<1){
c.JSON(http.StatusBadRequest, gin.H{"error": "ID no puede ser menor que 1"})
return
}
if err := r.ObtenerRepositorios().Repuesto.Eliminar(uint(id)); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err})
return
}
c.Status(http.StatusOK)
}
+1
View File
@@ -14,6 +14,7 @@ func RepuestoRoute(r *gin.Engine) {
repuestoGrupo.GET("/", repuestos.ObtenerRepuestos)
repuestoGrupo.GET("/:id", repuestos.ObtenerRepuesto)
repuestoGrupo.POST("/", repuestos.CrearRepuesto)
repuestoGrupo.DELETE("/:id", repuestos.EliminarRepuesto)
}
}
@@ -0,0 +1,39 @@
<script>
import {
StructuredList,
StructuredListBody,
StructuredListRow,
StructuredListCell,
Modal
} from 'carbon-components-svelte';
let { objeto = $bindable(), open = $bindable() } = $props();
function handleClose() {
objeto = null;
open = false;
}
</script>
<Modal
bind:open
modalHeading="Info"
secondaryButtonText="Cerrar"
on:click:button--secondary={handleClose}
primaryButtonDisabled={true}
>
{#if objeto != null}
<StructuredList>
<StructuredListBody>
{#each Object.entries(objeto) as [campo, valor]}
<StructuredListRow>
<StructuredListCell>{campo}</StructuredListCell>
<StructuredListCell>
{JSON.stringify(valor)}
</StructuredListCell>
</StructuredListRow>
{/each}
</StructuredListBody>
</StructuredList>
{/if}
</Modal>
@@ -0,0 +1,20 @@
import { API_URL } from '$lib/env';
import { getToken } from '../token';
export async function eliminarRepuesto(id: number) {
try {
const req = await fetch(`${API_URL}/repuestos/${id}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${getToken()}`
}
});
if (req.ok) {
return 'Ok';
}
const data = await req.json();
return data.error;
} catch {
return 'no se puso alcanzar el servidor';
}
}
+37 -3
View File
@@ -12,14 +12,21 @@
TableHeader,
TableRow
} from 'carbon-components-svelte';
import { Add, Edit, TrashCan } from 'carbon-icons-svelte';
import { Add, Edit, Information, TrashCan } from 'carbon-icons-svelte';
import CrearRepuestoForm from './CrearRepuestoForm.svelte';
import EliminarRepuestoForm from './EliminarRepuestoForm.svelte';
import ModalStructured from '$lib/components/ModalStructured.svelte';
let error = $state('');
let loading = $state('');
let { data } = $props();
let openCrear = $state(false);
let openEliminar = $state(false);
let openInfo = $state(false);
let repuestoEliminar = $state({ id: 0 });
let repuestoInfo = $state();
$effect(() => {
if (data.error) {
@@ -72,8 +79,24 @@
<!-- onclick={() => abrirModalEditar(p)} -->
<Button kind="ghost" size="small" icon={Edit}>Editar</Button>
<Button kind="ghost" size="small">Ver Existencias</Button>
<!-- onclick={() => abrirModalEliminar(p)} -->
<Button kind="ghost" size="small" icon={TrashCan}>Eliminar</Button>
<Button
onclick={() => {
repuestoInfo = p;
openInfo = true;
}}
kind="ghost"
size="small"
icon={Information}>Ver registro</Button
>
<Button
onclick={() => {
repuestoEliminar = p;
openEliminar = true;
}}
kind="ghost"
size="small"
icon={TrashCan}>Eliminar</Button
>
</TableCell>
</TableRow>
{/each}
@@ -90,3 +113,14 @@
openCrear = false;
}}
></CrearRepuestoForm>
<EliminarRepuestoForm
bind:show={openEliminar}
onclose={() => {
openEliminar = false;
repuestoEliminar = { id: 0 };
}}
repuesto={repuestoEliminar}
></EliminarRepuestoForm>
<ModalStructured objeto={repuestoInfo} bind:open={openInfo} />
@@ -0,0 +1,50 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { eliminarRepuesto } from '$lib/hooks/repuestos/eliminarRepuesto';
import { Modal } from 'carbon-components-svelte';
let {
show = $bindable(),
onclose,
repuesto
}: { show?: boolean; onclose: () => void; repuesto: { id: number } } = $props();
let error = $state('');
async function handleSubmit(e: Event) {
e.preventDefault();
error = '';
if (!repuesto.id) {
error = 'No se pudo obtener la id del Repuesto? recarga la pagina.';
return;
}
const result = await eliminarRepuesto(repuesto.id);
if (result !== 'Ok') {
error = result;
return;
}
handleClose();
}
function handleClose() {
error = '';
invalidate('repuestos:load');
onclose();
}
</script>
<form id="eliminarRepuestoForm" onsubmit={handleSubmit}>
<Modal
formId="eliminarRepuestoForm"
bind:open={show}
modalHeading="Eliminar Repuesto"
primaryButtonText="Eliminar"
secondaryButtonText="Cerrar"
on:click:button--secondary={handleClose}
>
{#if error}
<div>{error}</div>
{/if}
Esta seguro que quiere borrar este Repuesto?
</Modal>
</form>
@@ -41,16 +41,8 @@
on:click:button--secondary={handleClose}
>
{#if error}
<div class="error-msg">{error}</div>
<div>{error}</div>
{/if}
Esta seguro que quiere borrar este usuario?
</Modal>
</form>
<style>
.error-msg {
color: #da1e28;
margin-bottom: 1rem;
font-size: 0.875rem;
}
</style>