apuntes:backend_frontend
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| apuntes:backend_frontend [2025/03/24 08:59] – [MariaDB como base de datos] Santiago Faci | apuntes:backend_frontend [2025/03/24 09:34] (current) – [Backend: API] Santiago Faci | ||
|---|---|---|---|
| Line 611: | Line 611: | ||
| </ | </ | ||
| + | ==== Frontend: Utilidades ==== | ||
| + | |||
| + | <file javascript documentUtils.js> | ||
| + | const icons = new Map(); | ||
| + | icons.set(' | ||
| + | '< | ||
| + | '< | ||
| + | '</ | ||
| + | ); | ||
| + | icons.set(' | ||
| + | '< | ||
| + | '< | ||
| + | '< | ||
| + | '</ | ||
| + | ); | ||
| + | // TODO Añadir más iconos | ||
| + | |||
| + | const el = function (elementId) { | ||
| + | return document.getElementById(elementId); | ||
| + | }; | ||
| + | |||
| + | const icon = function(iconName) { | ||
| + | return icons.get(iconName); | ||
| + | }; | ||
| + | |||
| + | const td = function(text) { | ||
| + | return '< | ||
| + | } | ||
| + | |||
| + | module.exports = { | ||
| + | el, | ||
| + | icon, | ||
| + | td | ||
| + | }; | ||
| + | </ | ||
| ==== Frontend: carga de datos en una tabla ==== | ==== Frontend: carga de datos en una tabla ==== | ||
| + | |||
| + | <file html index.html> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <link href=" | ||
| + | <script type=" | ||
| + | </ | ||
| + | <body onload=" | ||
| + | <div class=" | ||
| + | <ul class=" | ||
| + | <li class=" | ||
| + | <a class=" | ||
| + | </li> | ||
| + | <li class=" | ||
| + | <a class=" | ||
| + | </li> | ||
| + | </ul> | ||
| + | |||
| + | < | ||
| + | | ||
| + | <table class=" | ||
| + | < | ||
| + | <tr> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <th>. . .</ | ||
| + | </tr> | ||
| + | </ | ||
| + | <tbody id=" | ||
| + | </ | ||
| + | </ | ||
| + | </ul> | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <file javascript index.js> | ||
| + | import axios from ' | ||
| + | import { el, icon, td } from ' | ||
| + | import { notifyOk } from ' | ||
| + | |||
| + | window.readCities = function() { | ||
| + | axios.get(' | ||
| + | .then((response) => { | ||
| + | const cityList = response.data; | ||
| + | const cityTable = el(' | ||
| + | | ||
| + | cityList.forEach(city => { | ||
| + | const row = document.createElement(' | ||
| + | row.id = ' | ||
| + | row.innerHTML = td(city.name) + | ||
| + | td(city.population) + | ||
| + | td(city.area) + | ||
| + | '<a class=" | ||
| + | icon(' | ||
| + | '</ | ||
| + | '<a class=" | ||
| + | icon(' | ||
| + | '</ | ||
| + | cityTable.appendChild(row); | ||
| + | }) | ||
| + | }); | ||
| + | }; | ||
| + | |||
| + | window.removeCity = function(id) { | ||
| + | if (confirm(' | ||
| + | axios.delete(' | ||
| + | .then((response) => { | ||
| + | if (response.status == 204) { | ||
| + | notifyOk(' | ||
| + | el(' | ||
| + | } | ||
| + | }); | ||
| + | } | ||
| + | }; | ||
| + | </ | ||
| + | ==== Frontend: Diálogos ==== | ||
| + | |||
| + | <file javascript dialogUtils.js> | ||
| + | import Toastify from ' | ||
| + | import ' | ||
| + | |||
| + | const notifyError = function(message) { | ||
| + | Toastify({ | ||
| + | text: message, | ||
| + | duration: 3000, | ||
| + | gravity: ' | ||
| + | position: ' | ||
| + | style: { | ||
| + | background: " | ||
| + | }, | ||
| + | }).showToast(); | ||
| + | }; | ||
| + | |||
| + | const notifyOk = function(message) { | ||
| + | Toastify({ | ||
| + | text: message, | ||
| + | duration: 3000, | ||
| + | gravity: ' | ||
| + | position: ' | ||
| + | style: { | ||
| + | background: " | ||
| + | }, | ||
| + | }).showToast(); | ||
| + | }; | ||
| + | |||
| + | module.exports = { | ||
| + | notifyError, | ||
| + | notifyOk | ||
| + | }; | ||
| + | </ | ||
| ==== Frontend: formulario de registro ==== | ==== Frontend: formulario de registro ==== | ||
| + | <file html register.html> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <link href=" | ||
| + | <script type=" | ||
| + | </ | ||
| + | < | ||
| + | <div class=" | ||
| + | <ul class=" | ||
| + | <li class=" | ||
| + | <a class=" | ||
| + | </li> | ||
| + | <li class=" | ||
| + | <a class=" | ||
| + | </li> | ||
| + | </ul> | ||
| + | | ||
| + | < | ||
| + | <form action=" | ||
| + | <div class=" | ||
| + | <label for=" | ||
| + | <input class=" | ||
| + | </ | ||
| + | <div class=" | ||
| + | <label for=" | ||
| + | <input class=" | ||
| + | </ | ||
| + | <div class=" | ||
| + | <label for=" | ||
| + | <input class=" | ||
| + | </ | ||
| + | <button type=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <file javascript register.js> | ||
| + | import axios from ' | ||
| + | import { notifyError, | ||
| + | import { el } from ' | ||
| + | |||
| + | window.addCity = function() { | ||
| + | const name = el(' | ||
| + | const population = el(' | ||
| + | const area = el(' | ||
| + | |||
| + | // TODO Validación de datos | ||
| + | if (name === '' | ||
| + | notifyError(' | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | axios.post(' | ||
| + | name: name, | ||
| + | population: population, | ||
| + | area: area | ||
| + | }); | ||
| + | |||
| + | // TODO Confirmar al usuario que todo ha ido bien (o mal) | ||
| + | notifyOk(' | ||
| + | |||
| + | // TODO Limpiar el formulario | ||
| + | el(' | ||
| + | el(' | ||
| + | el(' | ||
| + | }; | ||
| + | </ | ||
| ===== Funciones de utilidad ===== | ===== Funciones de utilidad ===== | ||
| Line 679: | Line 898: | ||
| <code javascript> | <code javascript> | ||
| + | const knex = require(' | ||
| + | . . . | ||
| const { config } = require(' | const { config } = require(' | ||
apuntes/backend_frontend.1742806767.txt.gz · Last modified: by Santiago Faci
