funcionalidad de alta repuesto terminada

This commit is contained in:
fede committed 2026-07-01 01:00:12 -03:00
1 parent d0500dc764
commit 352973d2f5
15 files changed
+218 -38

No files matched your search

@@ -0,0 +1,24 @@
import { API_URL } from '$lib/env';
import type { CrearRepuestoDto } from '../../../types';
import { getToken } from '../token';
export async function crearRepuesto(repuesto: CrearRepuestoDto) {
try {
const req = await fetch(`${API_URL}/repuestos/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${getToken()}`
},
body: JSON.stringify(repuesto)
});
if (req.ok) {
return 'Ok';
}
const data = await req.json();
return data.error;
} catch {
return 'no se puso alcanzar el servidor';
}
}
@@ -4,7 +4,8 @@ import { getToken } from '../token';
export async function obtenerRepuestos(fetch2?: Function) {
const fetchfn = fetch2 ? fetch2 : fetch;
try {
const req = await fetchfn(`${API_URL}/repuestos`, {
const path = `${API_URL}/repuestos/`;
const req = await fetchfn(path, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
+2 -1
View File
@@ -1,7 +1,8 @@
import { obtenerRepuestos } from '$lib/hooks/repuestos/obtenerRepuestos';
export let ssr = false;
export async function load({ fetch }) {
export async function load({ fetch, depends }) {
depends('repuestos:load');
const repuestos = await obtenerRepuestos(fetch);
let error = '';
@@ -1,15 +1,17 @@
<script>
<script lang="ts">
import { Button, Modal, NumberInput, TextInput } from 'carbon-components-svelte';
import { Add } from 'carbon-icons-svelte';
import type { CrearRepuestoDto } from '../../types';
import { crearRepuesto } from '$lib/hooks/repuestos/crearRepuesto';
import { invalidate } from '$app/navigation';
let { open = $bindable(), onClose } = $props();
let error = $state('');
let repuesto = $state({
let repuesto: CrearRepuestoDto = $state({
nombre: '',
descripcion: '',
marca: '',
aliases: [],
aliases: [''],
precio: 0,
stockMinimo: 0
});
@@ -31,8 +33,21 @@
<form
id="crearRepuestoForm"
onsubmit={(e) => {
onsubmit={async (e) => {
e.preventDefault();
error = '';
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
if (repuesto.marca == '') error += 'Falta Marca, ';
if (repuesto.precio <= 0) error += 'El precio es 0 o menor, ';
if (repuesto.nombre == '') error += 'Falta Nombre';
if (error == '') {
error = await crearRepuesto(repuesto);
}
if (error == 'Ok') {
invalidate('repuestos:load');
handleClose();
}
}}
>
<Modal
@@ -62,20 +77,30 @@
labelText="Stock Minimo (es para los avisos): "
bind:value={repuesto.stockMinimo}
/>
<div class="my-2! flex justify-between">
<div class="my-2!">
<h5>Aliases</h5>
<Button
size="small"
icon={Add}
onclick={() => {
repuesto.aliases.push('');
}}>Añadir Alias</Button
>
</div>
<div class="flex flex-col gap-2">
{#each repuesto.aliases as _, index}
<TextInput name={`alias-${index}`} bind:value={repuesto.aliases[index]} />
{/each}
<table class="w-full">
<tbody>
{#each repuesto.aliases as _, index}
<tr>
<td>
<TextInput
maxlength={70}
name={`alias-${index}`}
bind:value={repuesto.aliases[index]}
on:input={() => {
if (index === repuesto.aliases.length - 1 && repuesto.aliases[index] !== '') {
repuesto.aliases.push('');
} else if (repuesto.aliases[index] == '') {
repuesto.aliases.pop();
}
}}
/>
</td>
</tr>
{/each}
</tbody>
</table>
</div>
</Modal>
</form>
+9
View File
@@ -4,6 +4,15 @@ export interface Usuario {
name: string;
}
interface CrearRepuestoDto {
nombre: string;
descripcion: string;
marca: string;
aliases: string[];
precio: number;
stockMinimo: number;
}
export interface Grupo {
id: number;
nombre: string;