import * as backend from './backend'; const store = { locations: [], locationLabels: new Set(), setLocations(newLocations) { this.locations = newLocations; this.locations.sort((a, b) => a.label > b.label); for (let loc of newLocations) { this.locationLabels.add(loc.label); } }, async addLocation(loc) { if (this.locationLabels.has(loc.label)) { return; } let newLoc = null; try { newLoc = await backend.create(loc); } catch (err) { alert(`Error when saving a new place: ${err.toString()}`); return; } this.locations.push(newLoc); this.locations.sort((a, b) => a.label > b.label); this.locationLabels.add(newLoc.label); }, mapObject: null, setMapObject(mapObject) { this.mapObject = mapObject; }, openPopup(label, lat, lng) { L.popup() .setLatLng([lat, lng]) .setContent(label) .openOn(this.mapObject); }, }; export default store;