refactor de admin usuarios

This commit is contained in:
2025-04-21 16:33:11 -03:00
parent 4e5e0a4f6f
commit 524a315dc1

View File

@@ -1,7 +1,7 @@
<script lang="ts">
import { onMount } from "svelte";
import NavBarAutocompletable from "../Componentes/NavBarAutocompletable.svelte";
import type { Cliente, Grupo, UpdateCliente } from "../types";
import type { Cliente, Grupo, GrupoDto, UpdateCliente } from "../types";
import { urlG } from "../stores/urlStore";
import ModalEstatico from "../Componentes/ModalEstatico.svelte";
import BarraHorizontalConTexto from "../Componentes/BarraHorizontalConTexto.svelte";
@@ -63,12 +63,15 @@
}
async function cargaGrupos(cli: Cliente) {
try {
const response = await fetch($urlG+"/api/admin/clientes/grupo?Dni="+cli.dni, {
const response = await fetch(
$urlG + "/api/admin/clientes/grupo?Dni=" + cli.dni,
{
method: "GET",
headers: {
"Auth": String(token),
Auth: String(token),
},
});
},
);
if (response.ok) {
let data = await response.json();
Grupos = data;
@@ -87,12 +90,15 @@
//WIP añadir una flag para que muestre que no se pudo dar se alta/baja
event.stopPropagation();
try {
const response = await fetch($urlG+"/api/admin/cliente?Dni="+cli.dni, {
const response = await fetch(
$urlG + "/api/admin/cliente?Dni=" + cli.dni,
{
method: "DELETE",
headers: {
"Auth": String(token),
}
});
Auth: String(token),
},
},
);
if (response.ok) {
let data = await response.json();
modaldata = data.message;
@@ -100,9 +106,7 @@
}
} catch {
modaldata = "";
}
}
async function añadirGrupo(e: Event, cli: Cliente, grupo: string) {
e.preventDefault();
@@ -112,14 +116,17 @@
}
const email = cli.email;
try {
const response = await fetch($urlG+"/api/admin/cliente/addGrupo", {
const response = await fetch(
$urlG + "/api/admin/cliente/addGrupo",
{
method: "PATCH",
headers: {
"Auth": String(token),
'Content-Type' : 'application/json',
Auth: String(token),
"Content-Type": "application/json",
},
body: JSON.stringify({email, grupo})
});
body: JSON.stringify({ email, grupo }),
},
);
if (response.ok) {
let data = await response.json();
@@ -141,16 +148,21 @@
}
const email = cli.email;
if (grupo === "Propietario") {
if (confirm("Sos propietario si te desactivas de ese rol tus propiedades se van a dar de baja, Estas seguro?") == false) return;
if (
confirm(
"Sos propietario si te desactivas de ese rol tus propiedades se van a dar de baja, Estas seguro?",
) == false
)
return;
}
try {
const response = await fetch($urlG + "/api/admin/cliente/rmGrupo", {
method: "PATCH",
headers: {
"Auth": String(token),
'Content-Type' : 'application/json',
Auth: String(token),
"Content-Type": "application/json",
},
body: JSON.stringify({email, grupo})
body: JSON.stringify({ email, grupo }),
});
if (response.ok) {
@@ -167,7 +179,13 @@
}
let showModificarCliente: boolean = $state(false);
let datoscli:UpdateCliente = $state({dni:0, apellido:"", celular:"", domicilio:"", nombre:""});
let datoscli: UpdateCliente = $state({
dni: 0,
apellido: "",
celular: "",
domicilio: "",
nombre: "",
});
async function abrirModalModificarCliente(e: Event, cli: Cliente) {
e.stopPropagation();
@@ -175,8 +193,8 @@
const r = await fetch($urlG + "/api/admin/cliente?dni=" + cli.dni, {
method: "GET",
headers: {
"Auth": token||"",
}
Auth: token || "",
},
});
let data = await r.json();
if (r.ok) {
@@ -192,7 +210,12 @@
}
async function patchCliente(a: UpdateCliente) {
if (a.apellido =="" || a.celular=="" || a.domicilio=="" || a.nombre==""){
if (
a.apellido == "" ||
a.celular == "" ||
a.domicilio == "" ||
a.nombre == ""
) {
modaldata = "Hay campos vacios";
return;
}
@@ -201,13 +224,13 @@
celular: a.celular,
domicilio: a.domicilio,
nombre: a.nombre,
}
};
try {
const r = await fetch($urlG + "/api/admin/cliente?dni=" + a.dni, {
method: "PATCH",
headers: {
"Auth": token||"",
"Content-Type": "application/json"
Auth: token || "",
"Content-Type": "application/json",
},
body: JSON.stringify(dto),
});
@@ -227,8 +250,11 @@
{/if}
{#if showModificarCliente}
<ModalModificarPropietarios datos={datoscli} onCancel={()=>!!(showModificarCliente = false)}
onConfirm={patchCliente}/>
<ModalModificarPropietarios
datos={datoscli}
onCancel={() => !!(showModificarCliente = false)}
onConfirm={patchCliente}
/>
{/if}
<div class="content-fluid align-items-start">
@@ -237,7 +263,9 @@
<BarraHorizontalConTexto text="Clientes" />
<div style="height:70vh; overflow-y: auto; max-width: 100%">
<table class="table table-responsive table-striped table-hover table-bordered">
<table
class="table table-responsive table-striped table-hover table-bordered"
>
<thead>
<tr>
<th>Dni</th>
@@ -254,15 +282,25 @@
<td>{cli.email}</td>
<td>
{#if cli.habilitado}
<button class="btn btn-warning" onclick={(e) => bajaCliente(e, cli)}>
<button
class="btn btn-warning"
onclick={(e) => bajaCliente(e, cli)}
>
Baja
</button>
{:else}
<button class="btn btn-success" onclick={(e) => bajaCliente(e, cli)}>
<button
class="btn btn-success"
onclick={(e) => bajaCliente(e, cli)}
>
Alta
</button>
{/if}
<button class="btn btn-secondary" onclick={(e)=>abrirModalModificarCliente(e, cli)}>
<button
class="btn btn-secondary"
onclick={(e) =>
abrirModalModificarCliente(e, cli)}
>
Modificar
</button>
</td>
@@ -274,7 +312,9 @@
</div>
<div class="col me-2 ps-0">
<BarraHorizontalConTexto text="Grupos del Cliente Seleccionado" />
<table class="table table-striped table-responsive table-sm table-bordered">
<table
class="table table-striped table-responsive table-sm table-bordered"
>
<thead>
<tr>
<th>Id</th>
@@ -284,12 +324,21 @@
</thead>
<tbody>
{#if Grupos.length > 0}
{#each Grupos as g}
<tr in:fade>
<td>{g.id}</td>
<td>{g.descripcion}</td>
<td><button class="btn btn-outline-danger" onclick={(e)=>BajaGrupo(e, SelCliente, g.descripcion)}>Baja</button></td>
<td
><button
class="btn btn-outline-danger"
onclick={(e) =>
BajaGrupo(
e,
SelCliente,
g.descripcion,
)}>Baja</button
></td
>
</tr>
{/each}
{:else if SelCliente != null}
@@ -298,7 +347,9 @@
</tr>
{:else}
<tr>
<td colspan="3">Seleccione un cliente para ver sus grupos</td>
<td colspan="3"
>Seleccione un cliente para ver sus grupos</td
>
</tr>
{/if}
</tbody>
@@ -334,4 +385,3 @@
</div>
</div>
</div>