From 926fbf0ec810111a82c3979731df816c14e75440 Mon Sep 17 00:00:00 2001 From: Francisco Sandoval Date: Wed, 12 Jul 2023 21:55:07 -0400 Subject: [PATCH] se agrega mantenedor de comunas --- src/layouts/Sidebar.svelte | 1 + src/pages/comunas/Admin.svelte | 128 +++++++++++++++++++++++++++++++++ src/pages/comunas/Form.svelte | 37 ++++++++++ src/pages/usuarios/Form.svelte | 2 +- src/routes/user.routes.js | 2 + src/services/comunas.js | 48 +++++++++++++ src/services/regiones.js | 48 +++++++++++++ 7 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 src/pages/comunas/Admin.svelte create mode 100644 src/pages/comunas/Form.svelte create mode 100644 src/services/comunas.js create mode 100644 src/services/regiones.js diff --git a/src/layouts/Sidebar.svelte b/src/layouts/Sidebar.svelte index fe70d6c..f61b91c 100644 --- a/src/layouts/Sidebar.svelte +++ b/src/layouts/Sidebar.svelte @@ -9,4 +9,5 @@ Aplicaciones Usuarios + Comunas diff --git a/src/pages/comunas/Admin.svelte b/src/pages/comunas/Admin.svelte new file mode 100644 index 0000000..b8781ad --- /dev/null +++ b/src/pages/comunas/Admin.svelte @@ -0,0 +1,128 @@ + + +Comunas + +
+ +
+ Volver +
+ +
+ + + + + + + + + + {#each comunas.results as row} + + + + + + {/each} + +
IDNombreRegión
onEditar(row)}>{row.id_comuna}{row.nombre_comuna}{row.id_region}
+
+ +
+ onPage(page)}>Refrescar + page = ev.detail} + /> +
+ +{#if !!comuna} +
+ comuna = null} + > + + + + + {#if !es_nuevo} + + {/if} + + +
+{/if} + + \ No newline at end of file diff --git a/src/pages/comunas/Form.svelte b/src/pages/comunas/Form.svelte new file mode 100644 index 0000000..ababa2f --- /dev/null +++ b/src/pages/comunas/Form.svelte @@ -0,0 +1,37 @@ + + +
+
ID
+ +
+ +
+
Comuna
+ +
+ +
+
Región
+ +
\ No newline at end of file diff --git a/src/pages/usuarios/Form.svelte b/src/pages/usuarios/Form.svelte index 0228216..23e3f0c 100644 --- a/src/pages/usuarios/Form.svelte +++ b/src/pages/usuarios/Form.svelte @@ -11,7 +11,7 @@
- +
diff --git a/src/routes/user.routes.js b/src/routes/user.routes.js index 2a683e6..2c09e22 100644 --- a/src/routes/user.routes.js +++ b/src/routes/user.routes.js @@ -5,6 +5,7 @@ import PageAplicaciones from '$/pages/aplicaciones/Admin.svelte' import PageUsuarios from '$/pages/usuarios/Admin.svelte' import PageUsuarioCreate from '$/pages/usuarios/Create.svelte' import PageUsuarioModifica from '$/pages/usuarios/Create.svelte' +import PageComunas from '$/pages/comunas/Admin.svelte' export const routes = [ { path: '/', component: PageHome }, @@ -13,5 +14,6 @@ export const routes = [ { path: '/usuarios', component: PageUsuarios }, { path: '/usuarios/nuevo', component: PageUsuarioCreate }, { path: '/usuarios/:login', component: PageUsuarioModifica }, + { path: '/comunas', component: PageComunas }, { path: '*', component: PageError }, ] \ No newline at end of file diff --git a/src/services/comunas.js b/src/services/comunas.js new file mode 100644 index 0000000..e211f96 --- /dev/null +++ b/src/services/comunas.js @@ -0,0 +1,48 @@ + +import { base, getToken } from './_config' + +export async function getComunas(params) { + const query = !params ? '' : '?' + (new URLSearchParams(params).toString()); + const res = await fetch(`${base}/comunas/${query}`, { + headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } + }) + if (!res.ok) throw await res.text() + return res.json() +} + +export async function getComuna(id) { + const res = await fetch(`${base}/comunas/${id}/`, { + headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } + }) + if (!res.ok) throw await res.text() + return res.json() +} + +export async function createComuna(data) { + const res = await fetch(`${base}/comunas/`, { + method: 'POST', + body: JSON.stringify(data), + headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } + }) + if (!res.ok) throw await res.text() + return res.json() +} + +export async function updateComuna({ id_comuna: id, ...data }) { + const res = await fetch(`${base}/comunas/${id}/`, { + method: 'PATCH', + body: JSON.stringify(data), + headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } + }) + if (!res.ok) throw await res.text() + return res.json() +} + +export async function deleteComuna(id) { + const res = await fetch(`${base}/comunas/${id}/`, { + method: 'DELETE', + headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } + }) + if (!res.ok) throw await res.text() + return res.text() +} \ No newline at end of file diff --git a/src/services/regiones.js b/src/services/regiones.js new file mode 100644 index 0000000..3f3b34a --- /dev/null +++ b/src/services/regiones.js @@ -0,0 +1,48 @@ + +import { base, getToken } from './_config' + +export async function getRegiones(params) { + const query = !params ? '' : '?' + (new URLSearchParams(params).toString()); + const res = await fetch(`${base}/regiones/${query}`, { + headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } + }) + if (!res.ok) throw await res.text() + return res.json() +} + +export async function getRegion(id) { + const res = await fetch(`${base}/regiones/${id}/`, { + headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } + }) + if (!res.ok) throw await res.text() + return res.json() +} + +export async function createRegion(data) { + const res = await fetch(`${base}/regiones/`, { + method: 'POST', + body: JSON.stringify(data), + headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } + }) + if (!res.ok) throw await res.text() + return res.json() +} + +export async function updateRegion({ id, ...data }) { + const res = await fetch(`${base}/regiones/${id}/`, { + method: 'PATCH', + body: JSON.stringify(data), + headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } + }) + if (!res.ok) throw await res.text() + return res.json() +} + +export async function deleteRegion(id) { + const res = await fetch(`${base}/regiones/${id}/`, { + method: 'DELETE', + headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } + }) + if (!res.ok) throw await res.text() + return res.json() +} \ No newline at end of file