From 8078ec3fcb9a2d49d0405d1efcf258770aa537cb Mon Sep 17 00:00:00 2001 From: Jorge Bolois Guerrero Date: Sun, 30 Oct 2022 21:15:53 +0100 Subject: [PATCH] WIP helper for adding new locations. --- index.html | 50 +++++++++++++++++++++++++++++++++-- src/Location.js | 33 ++++++++++++++--------- src/webapp/App.js | 45 +++++++++++++++++++++++++++++++ src/webapp/GithubLocations.js | 40 ++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 15 deletions(-) create mode 100644 src/webapp/App.js create mode 100644 src/webapp/GithubLocations.js diff --git a/index.html b/index.html index bf42c2d..b2d4dfe 100644 --- a/index.html +++ b/index.html @@ -2,15 +2,61 @@ - Locations - Idealista Enhancer + Localidades - Idealista Enhancer + + + + + +
+ +
+
+
+
+ + +
+ +
+
+
+ +
+
+
+
+ Localidades +

Un total de 0 localicades.

+ + + + + + + + +
Nombre + extension de ficheroAcciones
+
+
+
+
+
- \ No newline at end of file diff --git a/src/Location.js b/src/Location.js index 037ade8..d82aecc 100644 --- a/src/Location.js +++ b/src/Location.js @@ -2,25 +2,32 @@ import Supermarket from "./Supermarket.js"; export default class Location { + static empty() { + return this.fromRaw(null); + } + static fromRaw(raw) { const location = new Location(); - location.name = raw.name; - location.altitude = raw.altitude; + location.name = raw?.name || ''; + location.altitude = raw?.altitude || 0; - location.train = raw.train; - location.bus = raw.bus; + location.train = raw?.train || false; + location.bus = raw?.bus || true; - location.gym = raw.gym; - location.pool = raw.pool; + location.gym = raw?.gym || false; + location.pool = raw?.pool || false; - location.smoke = raw.smoke; - location.pharmacy = raw.pharmacy; + location.pharmacy = raw?.pharmacy || true; + location.smoke = raw?.smoke || false; location.supermarkets = []; - for (const rawSupermarket of raw.supermarkets) { - const supermarket = new Supermarket(); - supermarket.name = rawSupermarket.type; - supermarket.url = rawSupermarket.url; - location.supermarkets.push(supermarket); + + if (raw.supermarkets) { + for (const rawSupermarket of raw.supermarkets) { + const supermarket = new Supermarket(); + supermarket.name = rawSupermarket?.type; + supermarket.url = rawSupermarket?.url; + location.supermarkets.push(supermarket); + } } return location; diff --git a/src/webapp/App.js b/src/webapp/App.js new file mode 100644 index 0000000..d0ac6b4 --- /dev/null +++ b/src/webapp/App.js @@ -0,0 +1,45 @@ +import Location from "../Location.js"; +import Event from "../Event.js"; + +import GithubLocations from "./GithubLocations.js"; +import Locations from "../Locations.js"; + + +export default class App { + + constructor() { + this._init(); + } + + _init() { + this.githubLocations = new GithubLocations('Midefos', 'idealista-enhancer'); + } + + async load() { + const newLocation = document.querySelector('textarea#newLocation'); + newLocation.textContent = JSON.stringify(Location.empty(), undefined, 4); + + const locations = await this.githubLocations.extractLocations(); + + const locationQuantity = document.querySelector('#locations-quantity'); + locationQuantity.textContent = locations.length; + const tableBody = document.querySelector('#locations-table tbody'); + for (const location of locations) { + tableBody.insertAdjacentHTML('afterend', ` + ${location.name} + Cargar + `); + } + Event.click('.load-location', async (element) => { + const rawName = element.getAttribute('data-name'); + const name = rawName.substring(0, rawName.indexOf('.json')); + const location = await Locations.get(name); + document.querySelector('#locationFilename').value = name; + newLocation.textContent = JSON.stringify(location, undefined, 4); + }) + } + +} + +const app = new App(); +app.load() \ No newline at end of file diff --git a/src/webapp/GithubLocations.js b/src/webapp/GithubLocations.js new file mode 100644 index 0000000..996505f --- /dev/null +++ b/src/webapp/GithubLocations.js @@ -0,0 +1,40 @@ + +export default class GithubLocations { + + constructor(user, repo) { + this.user = user; + this.repo = repo; + } + + get mainUrl() { + return `https://api.github.com/repos/${this.user}/${this.repo}/git/trees/main` + } + + async extractLocations() { + const locationTreeInfo = await this._extractLocationTreeData(); + const data = await fetch(locationTreeInfo.url); + const json = await data.json(); + + const locations = []; + for (const treeInfo of json.tree) { + locations.push({ name: treeInfo.path, url: treeInfo.url }); + } + return locations; + + } + + async _extractLocationTreeData() { + const data = await fetch(this.mainUrl); + const json = await data.json(); + console.log(json) + for (const treeInfo of json.tree) { + if (treeInfo.path === 'locations') { + return treeInfo; + } + } + } + +} + + +