Removed useless data and fix api req
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
import { useAsyncState } from "@vueuse/core";
|
||||
import { defineStore } from "pinia";
|
||||
|
||||
import * as GeoJsonGeometriesLookup from "geojson-geometries-lookup";
|
||||
|
||||
export const useCirStore = defineStore("cirStore", () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let gl: any = null;
|
||||
|
||||
const geoData = useAsyncState(
|
||||
fetch("/data/circonscriptions-legislatives.json.gz").then((t) => t.json()),
|
||||
{},
|
||||
{
|
||||
onSuccess(data) {
|
||||
gl = new GeoJsonGeometriesLookup(data);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
function getCounty(lon: number, lat: number): string | null {
|
||||
const pos = {
|
||||
type: "Point",
|
||||
coordinates: [lon, lat],
|
||||
};
|
||||
return gl.getContainers(pos).features[0]?.properties["REF"] as
|
||||
| string
|
||||
| null;
|
||||
}
|
||||
|
||||
return { geoData, getCounty };
|
||||
});
|
||||
11
src/stores/repStore.ts
Normal file
11
src/stores/repStore.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { defineStore } from "pinia";
|
||||
|
||||
export const useCirStore = defineStore("repStore", () => {
|
||||
async function getRep(lon: number, lat: number): Promise<any> {
|
||||
return fetch(`http://localhost:3000/rep?lat=${lat}&lon=${lon}`).then((t) =>
|
||||
t.json()
|
||||
);
|
||||
}
|
||||
|
||||
return { getRep };
|
||||
});
|
||||
@@ -1,15 +1,15 @@
|
||||
<script lang="ts" setup>
|
||||
import { useCirStore } from "@/stores/cir";
|
||||
import { useCirStore } from "@/stores/repStore";
|
||||
import { useGeolocation } from "@vueuse/core";
|
||||
import { ref } from "vue";
|
||||
|
||||
const { coords } = useGeolocation();
|
||||
const { geoData, getCounty } = useCirStore();
|
||||
const { getRep } = useCirStore();
|
||||
|
||||
const currentCountyCode = ref<string | null>("");
|
||||
const currentRepValue = ref<any>();
|
||||
|
||||
function handleGeolocClick() {
|
||||
currentCountyCode.value = getCounty(
|
||||
async function handleGeolocClick() {
|
||||
currentRepValue.value = await getRep(
|
||||
coords.value.longitude,
|
||||
coords.value.latitude
|
||||
);
|
||||
@@ -20,13 +20,14 @@ function handleGeolocClick() {
|
||||
<div>
|
||||
<main class="container py-4">
|
||||
<button
|
||||
v-if="geoData.isReady"
|
||||
class="py-2 px-4 rounded-sm bg-white text-sm text-slate-950"
|
||||
@click="handleGeolocClick"
|
||||
>
|
||||
Get Code
|
||||
Trouver mon député
|
||||
</button>
|
||||
{{ currentCountyCode }}
|
||||
<pre>
|
||||
{{ currentRepValue }}
|
||||
</pre>
|
||||
</main>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user