Removed useless data and fix api req

This commit is contained in:
Alexis
2023-04-27 12:14:33 +02:00
parent 73a4ca7982
commit 3e24dffad6
8 changed files with 20 additions and 2896 deletions

View File

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

View File

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