Added nanostore logic to centralise data

This commit is contained in:
Alexis
2023-10-13 17:19:58 +02:00
parent 4e1927bf02
commit e654d3f96a
7 changed files with 98 additions and 30 deletions

View File

@@ -0,0 +1,30 @@
import { deepMap, onMount, task, action } from 'nanostores'
import type { MapCoords, MapMarker } from '../../types/Leaflet';
export const $world = deepMap({
markers: [] as MapMarker[],
players: {} as MapMarker,
lastCoords: {
y: 0,
x: 0,
}
})
export const flyTo = action($world, 'fly-to', (store, coords: MapCoords) => {
store.setKey('lastCoords', coords)
return store.get()
})
// Fetch initial data
onMount($world, () => {
// Fetch all markers
task(async () => {
const m = await fetch('http://localhost:4321/api/markers.json')
$world.setKey('markers', await m.json())
})
// Fetch all player data
task(async () => {
const p = await fetch('http://localhost:4321/api/players.json')
$world.setKey('players', await p.json())
})
})