WIP helper for adding new locations.

This commit is contained in:
Jorge Bolois Guerrero 2022-10-30 21:15:53 +01:00
parent fa8a62053f
commit 8078ec3fcb
4 changed files with 153 additions and 15 deletions

View File

@ -2,15 +2,61 @@
<html>
<head>
<title>Locations - Idealista Enhancer</title>
<title>Localidades - Idealista Enhancer</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script type="module" src="/src/webapp/App.js"></script>
</head>
<body>
<nav>
<div class="nav-wrapper">
<a href="#" class="brand-logo">Localidades - Idealista Enhancer</a>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col m12">
<div class="card blue-grey darken-1">
<div class="card-content white-text">
<input id="locationFilename" type="text" class="materialize-input"
placeholder="Location.json" />
<textarea id="newLocation" class="materialize-textarea"></textarea>
</div>
<!--<div class="card-action">
<a href="#">Validar</a>
<a href="#">Guardar</a>
</div>-->
</div>
</div>
</div>
<div class="row">
<div class="col m12">
<div class="card blue-grey darken-1">
<div class="card-content white-text">
<span class="card-title">Localidades</span>
<p>Un total de <strong id="locations-quantity">0</strong> localicades.</p>
<table id="locations-table">
<tr>
<th>Nombre + extension de fichero</th>
<th>Acciones</th>
</tr>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- TODO: Please integrate a way to add/update/remove locations from a GUI. -->
</body>
</html>

View File

@ -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;

45
src/webapp/App.js Normal file
View File

@ -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', `<tr>
<td>${location.name}</td>
<td><a data-name="${location.name}" class="load-location waves-effect waves-light btn">Cargar</a></td>
</tr>`);
}
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()

View File

@ -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;
}
}
}
}