travel-steps/summer2024-frontend/src/stores/posts.js
Nicolas Froger b115ee3d5e
frontend: desperate attempt to fix fullpage issues when switching views
Signed-off-by: Nicolas Froger <nicolas@kektus.xyz>
2024-07-25 19:26:20 +02:00

34 lines
1,022 B
JavaScript

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'
export const usePostsStore = defineStore('posts', () => {
const postsApiPath = API_BASE_URL + '/posts'
const posts = ref([])
function fetchPosts() {
return fetch(postsApiPath)
.then((response) => {
return response.json()
})
.then(async (data) => {
posts.value = data.sort((a, b) => b.id - a.id) // highest ID (more recent) first
for (const post of posts.value) {
const postDate = new Date(post.date)
post.formatedDate = formatRelative(postDate, new Date(), { locale: fr })
post.formatedDescription = marked.parse(post.description)
}
return posts.value
})
.catch((error) => {
console.log('post list parsing failed with error: ' + error)
})
}
return { posts, fetchPosts }
})