wrbapp/frontend/pages/news/newmessage.vue

88 lines
2.3 KiB
Vue
Raw Normal View History

2022-09-27 17:33:00 +02:00
<template>
2023-01-22 15:42:45 +01:00
<div class="flex flex-col gap-5 mx-auto p-2 w-full max-w-md">
<form @submit.prevent="sendNews" class="flex flex-col">
<label class="font-bold">Titel</label>
<input v-model="form.title" required="true" class="input mb-5" type="text">
<label class="font-bold">Beschrijving</label>
<textarea v-model="form.description" required="true" class="input mb-5" />
<label class="font-bold">Groep</label>
<select v-model="form.topic" required="true" class="input mb-5">
<option value="all">Iedereen</option>
<option value="test">Test</option>
</select>
<div class="w-full flex flex-wrap justify-between">
<input :disabled="disableButtons" type="submit" value="Stuur Bericht" class="btn w-full sm:w-40 mb-1">
<button @click="router.back()" class="hover:underline font-bold w-full sm:w-max sm:ml-auto">Annuleer</button>
</div>
</form>
</div>
2022-09-27 17:33:00 +02:00
</template>
<script setup>
2022-11-08 15:29:20 +01:00
import { addDoc, collection, serverTimestamp, Timestamp } from 'firebase/firestore'
2022-09-27 17:33:00 +02:00
import { useToast } from 'vue-toastification'
definePageMeta({
2023-01-22 15:42:45 +01:00
title: 'Nieuw Bericht',
key: 'back'
2022-09-27 17:33:00 +02:00
})
2023-01-21 21:36:22 +01:00
const { news, db, auth } = inject('firebase')
2022-09-27 17:33:00 +02:00
const router = useRouter()
const toast = useToast()
const disableButtons = ref(false)
const form = ref({
2023-01-22 15:42:45 +01:00
title: '',
description: '',
topic: ''
2022-09-27 17:33:00 +02:00
})
const sendNews = async () => {
2023-01-21 21:36:22 +01:00
disableButtons.value = true
try {
const idToken = await auth.value.currentUser.getIdToken(true)
console.log(idToken)
const { error } = await useFetch('/api/sendmessage', {
method: 'post',
2023-01-22 15:42:45 +01:00
body: { title: form.value.title, body: form.value.description, token: idToken, topic: form.value.topic }
2023-01-21 21:36:22 +01:00
})
if (error.value) {
console.log(error.value)
return toast.error('Error tijdens het versturen van het bericht')
}
2022-11-08 15:11:45 +01:00
2023-01-21 21:36:22 +01:00
await addDoc(collection(db, "news"), {
title: form.value.title,
description: form.value.description,
date: serverTimestamp()
});
if (news.value) {
news.value.unshift({
title: form.value.title,
description: form.value.description,
date: Timestamp.now()
})
}
toast.success('Bericht is verstuurd')
2022-09-27 17:33:00 +02:00
2023-01-21 21:36:22 +01:00
navigateTo('/news')
} catch (e) {
console.log(e)
2022-09-27 17:33:00 +02:00
2023-01-21 21:36:22 +01:00
toast.error('Error tijdens het berict sturen')
}
2022-09-27 17:33:00 +02:00
2023-01-21 21:36:22 +01:00
disableButtons.value = false
2022-09-27 17:33:00 +02:00
}
2023-01-21 21:36:22 +01:00
</script>