diff --git a/summer2024-frontend/src/stores/goals.js b/summer2024-frontend/src/stores/goals.js new file mode 100644 index 0000000..741dc1c --- /dev/null +++ b/summer2024-frontend/src/stores/goals.js @@ -0,0 +1,35 @@ +import { defineStore } from 'pinia' +import { ref } from 'vue' +import { formatRelative } from 'date-fns' +import { fr } from 'date-fns/locale' +import { marked } from 'marked' +import { API_BASE_URL } from '@/config.js' +import { fromLonLat } from 'ol/proj.js' + +export const useGoalsStore = defineStore('goals', () => { + const goalsApiPath = API_BASE_URL + '/goals' + + const goals = ref([]) + + function fetchGoals() { + return fetch(goalsApiPath) + .then((response) => { + return response.json() + }) + .then(async (data) => { + goals.value = data.sort((a, b) => a.id - b.id) // lower ID (older) first + for (const goal of goals.value) { + const goalDate = new Date(goal.date) + goal.formatedDate = formatRelative(goalDate, new Date(), { locale: fr }) + goal.projectedCoordinates = fromLonLat([goal.longitude, goal.latitude]) + } + + return goals.value + }) + .catch((error) => { + console.log('goal list parsing failed with error: ' + error) + }) + } + + return { goals, fetchGoals } +}) diff --git a/summer2024-frontend/src/views/MapView.vue b/summer2024-frontend/src/views/MapView.vue index 88d18ae..8e02ba8 100644 --- a/summer2024-frontend/src/views/MapView.vue +++ b/summer2024-frontend/src/views/MapView.vue @@ -1,6 +1,7 @@ @@ -116,7 +118,8 @@ const goalLocations = ref([ - Objectif : {{ loc.name }} + Objectif : {{ loc.name }} + Arrivée : {{ loc.formatedDate }}
Objectif : {{ loc.name }}
Arrivée : {{ loc.formatedDate }}