2022-09-27 17:33:00 +02:00
|
|
|
<template>
|
|
|
|
<div class="flex flex-col gap-5 mx-auto p-2 w-full max-w-md">
|
2022-11-14 14:46:59 +01:00
|
|
|
<div v-if="loadedNews && news" class="flex flex-col gap-3">
|
2022-09-27 17:33:00 +02:00
|
|
|
<NuxtLink to="/news/newmessage" v-if="userData.sendNews" class="item-hover border-dashed border-2 container text-center font-bold text-xl border-neutral-500 mb-3">
|
|
|
|
Nieuw Bericht
|
|
|
|
</NuxtLink>
|
2022-11-14 14:46:59 +01:00
|
|
|
<div v-if="news[0]" v-for="(item, index) in news" :key="index">
|
2022-09-27 17:33:00 +02:00
|
|
|
<div class="item container flex flex-col relative">
|
|
|
|
<h3 class="text-sm">{{ longEventDate(item.date.toDate()) }}</h3>
|
|
|
|
<h2 class="text-2xl font-bold">{{ item.title }}</h2>
|
|
|
|
<p>{{ item.description }}</p>
|
2022-10-01 13:06:54 +02:00
|
|
|
<Icon v-if="userData.sendNews" @click="deleteItem(item, index)" size="1.5em" name="ion:trash-sharp" class="absolute top-3 right-3 hover:cursor-pointer text-red-500" />
|
2022-09-27 17:33:00 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-11-14 14:46:59 +01:00
|
|
|
<h2 v-else class="font-bold text-center text-xl mt-3">
|
|
|
|
Er is geen nieuws
|
|
|
|
</h2>
|
2022-09-27 17:33:00 +02:00
|
|
|
</div>
|
2022-11-14 14:21:59 +01:00
|
|
|
<div class="w-full flex flex-col justify-center items-center" v-else>
|
|
|
|
<Icon size="2em" name="ion:load-c" class="animate-spin" />
|
|
|
|
<h2 class="mt-2 font-bold">Loading...</h2>
|
|
|
|
</div>
|
2022-09-27 17:33:00 +02:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
import { getDocs, collection, deleteDoc, doc } from 'firebase/firestore'
|
|
|
|
import { useToast } from 'vue-toastification'
|
|
|
|
|
|
|
|
definePageMeta({
|
|
|
|
title: 'Nieuws'
|
|
|
|
})
|
|
|
|
|
|
|
|
const { news, userData, db } = inject('firebase')
|
|
|
|
|
|
|
|
const toast = useToast()
|
|
|
|
|
2022-11-14 14:46:59 +01:00
|
|
|
const loadedNews = ref(false)
|
|
|
|
|
2022-09-27 17:33:00 +02:00
|
|
|
onMounted(() => {
|
|
|
|
if (!news.value) getNews()
|
|
|
|
})
|
|
|
|
|
|
|
|
const longEventDate = (eventDate) => {
|
|
|
|
const date = new Date(eventDate)
|
|
|
|
|
|
|
|
return date.toLocaleString('nl-NL', {
|
|
|
|
weekday: 'short',
|
|
|
|
day: 'numeric',
|
|
|
|
year: 'numeric',
|
|
|
|
month: 'long',
|
|
|
|
hour: 'numeric',
|
|
|
|
minute: 'numeric'
|
|
|
|
}
|
|
|
|
)}
|
|
|
|
|
|
|
|
const getNews = async () => {
|
|
|
|
news.value = []
|
|
|
|
try {
|
|
|
|
const querySnapshot = await getDocs(collection(db, "news"));
|
|
|
|
querySnapshot.forEach((doc) => {
|
|
|
|
let data = doc.data()
|
|
|
|
data.id = doc.id
|
|
|
|
news.value.push(data)
|
|
|
|
});
|
|
|
|
|
|
|
|
news.value.sort((a, b) => b.date.seconds - a.date.seconds)
|
|
|
|
|
2022-11-14 14:46:59 +01:00
|
|
|
loadedNews.value = true
|
|
|
|
|
2022-09-27 17:33:00 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteItem = async (item, index) => {
|
2022-11-08 15:31:27 +01:00
|
|
|
if (!item.id) return toast.error('Refresh eerst voordat je dit bericht kan verwijderen')
|
|
|
|
|
2022-09-27 17:33:00 +02:00
|
|
|
try {
|
|
|
|
news.value.splice(index, 1)
|
|
|
|
|
|
|
|
await deleteDoc(doc(db, "news", item.id));
|
|
|
|
toast.success('Bericht verwijderd')
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
|
|
|
|
toast.error('Error tijdens bericht verwijderen')
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
</script>
|