diff --git a/src/helpers/Api.ts b/src/helpers/Api.ts index e69de29..fe574aa 100644 --- a/src/helpers/Api.ts +++ b/src/helpers/Api.ts @@ -0,0 +1,44 @@ +import ApiResponse from "../interfaces/ApiResponse"; +import Embalse from "../interfaces/Embalse"; +import Env from "./Env"; + +export default class Api { + static async embalses(): Promise { + return Api._makeReq('/embalses'); + } + + static async _makeReq(endpoint: string): Promise { + let hasMore = true; + + let url = Env.api_base() + endpoint; + + let data: T[] = []; + while (hasMore) { + const res = await fetch(url); + + if (!res.ok) { + throw new Error("Api error"); + } + + const json = await res.json() as ApiResponse; + + data.push(...json.items); + + hasMore = json.hasMore; + + if (hasMore) { + let found = false; + let i = 0; + while (!found) { + if (json.links[i].rel === 'next') { + url = json.links[i].href; + found = true; + } + i++; + } + } + } + + return data; + } +} diff --git a/src/helpers/Env.ts b/src/helpers/Env.ts new file mode 100644 index 0000000..fd17509 --- /dev/null +++ b/src/helpers/Env.ts @@ -0,0 +1,5 @@ +export default class Env { + static api_base(): string { + return import.meta.env.VITE_API_BASE; + } +} diff --git a/src/interfaces/ApiResponse.ts b/src/interfaces/ApiResponse.ts new file mode 100644 index 0000000..7a273b8 --- /dev/null +++ b/src/interfaces/ApiResponse.ts @@ -0,0 +1,13 @@ +export default interface ApiResponse { + items: T[]; + hasMore: boolean; + limit: number; + offset: number; + count: number; + links: Link[]; +} + +export interface Link { + rel: string; + href: string; +} diff --git a/src/interfaces/Embalse.ts b/src/interfaces/Embalse.ts index b89bfbc..f741314 100644 --- a/src/interfaces/Embalse.ts +++ b/src/interfaces/Embalse.ts @@ -1,27 +1,23 @@ export default interface Embalse { - // EMBALSES - ID: number; - AMBITO_NOMBRE: string; - EMBALSE_NOMBRE: string; - AGUA_TOTAL: number; - ELECTRICO_FLAG: boolean; - // LISTADO_EMBALSES - CODIGO: number; - NOMBRE: string; - EMBALSE: string; - X: string; - Y: string; - DEMARC: string; - CAUCE: string; - GOOGLE: string; - OPENSTREETMAP: string; - WIKIDATA: string; - PROVINCIA: string; - CCAA: string; - TIPO: string; - TITULAR: string; - USO: string; - COTA_CORON: string; - ALT_CIMIEN: string; - INFORME: string; + id: number + ambito_nombre: string + embalse_nombre: string + agua_total: number + electrico_flag: number + codigo: number + nombre: string + embalse: string + x: string + y: string + demarc: string + cauce: string + google: string | null + openstreetmap: string | null + wikidata: string | null + provincia: string + ccaa: string + tipo: string + cota_coron: string + alt_cimien: string + informe: string } diff --git a/src/partials/Loader.tsx b/src/partials/Loader.tsx new file mode 100644 index 0000000..e69de29 diff --git a/src/routes.ts b/src/routes.ts index 0232f85..d359203 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -1,6 +1,7 @@ import { RouteDefinition } from "@solidjs/router"; import Home from "./views/Home"; import Finder from "./views/Finder"; +import Viewer from "./views/Viewer"; const routes: RouteDefinition[] = [ { @@ -10,6 +11,10 @@ const routes: RouteDefinition[] = [ { path: '/finder', component: Finder + }, + { + path: '/viewer', + component: Viewer } ]; diff --git a/src/views/Embalse.tsx b/src/views/Embalse.tsx new file mode 100644 index 0000000..aca0d82 --- /dev/null +++ b/src/views/Embalse.tsx @@ -0,0 +1,16 @@ +import { createSignal } from "solid-js"; +import EmbalseType from "../interfaces/Embalse"; +import Navbar from "../partials/Navbar"; + +function Embalse() { + const [embalse, setEmbalse] = createSignal(null); + + return ( + <> + +
+ +
+ + ) +} diff --git a/src/views/Viewer.tsx b/src/views/Viewer.tsx new file mode 100644 index 0000000..b306273 --- /dev/null +++ b/src/views/Viewer.tsx @@ -0,0 +1,38 @@ +import { onMount } from "solid-js"; +import L from 'leaflet'; +import Embalse from "../interfaces/Embalse"; +import Api from "../helpers/Api"; + +function Viewer() { + let map: L.Map; + + let embalses: Embalse[] = []; + + onMount(() => { + map = L.map('map'); + L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', { + maxZoom: 19, + attribution: '© OpenStreetMap' + }).addTo(map); + + getEmbalses(); + }); + + const getEmbalses = async () => { + embalses = await Api.embalses(); + + embalses.forEach(embalse => { + L.marker([parseFloat(embalse.x), parseFloat(embalse.y)]).addTo(map); + }); + } + + return ( +
+
+
+ ); +} + +export default Viewer;